# Autoinstall gems

**URL:** <https://www.ruby-forum.com/t/autoinstall-gems/131791>\
**Category:** Ruby\
**Created:** [March 13, 2008, 9:22am UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791 "2008-03-13T09:22:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 13, 2008, 9:22am UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791/1 "2008-03-13T09:22:31Z")

</div>

Is there a way to autoinstall a gem when it is “required” in an .rb  
file?

say I have:

* * *

#!/usr/bin/ruby

require ‘rubygems’  
require ‘HOTP’

## puts HOTP.new.calculate\_hotp(‘12345’, 1, 6)

Is there a way to make rubygems autoinstall the HOTP gem if it is not  
already installed?

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 13, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791/2 "2008-03-13T13:07:08Z")

</div>

S2 wrote:

> Is there a way to make rubygems autoinstall the HOTP gem if it is not  
> already installed?

I think there is a right problem there. Installing gems _normally_  
require super user rights (on modern OS).

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 13, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791/3 "2008-03-13T14:11:33Z")

</div>

Joachim G. wrote:

> S2 wrote:
> 
> > Is there a way to make rubygems autoinstall the HOTP gem if it is not  
> > already installed?
> 
> I think there is a right problem there. Installing gems _normally_  
> require super user rights (on modern OS).

Yes, that is true. It would eventually yield a Permission denied error.  
But that is an OS configuration problem, not a rubygems one 🙂

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 13, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791/4 "2008-03-13T17:58:16Z")

</div>

Tom C. wrote:

> > 
> 
> Well, if I may add my very amateur voice here, it seems to me that one  
> could certainly trap an error and conditionally issue a system call to  
> engage the installation process at the super user level. The process  
> would then have to worked through manually, since there often are one or  
> more interactions which need to occur to complete the installation of  
> some gems. This would work fine if the person running the programmer has  
> access to superuser passwords.

Yes, this is a good alternative.

> Don’t know this is the sort of thing you have in mind or not.

I thought that rubygems had his way of doing this automagically, but  
after reading your response it looks like this is not the case and it’s  
also not really feasible to do easily.

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 13, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791/5 "2008-03-13T17:44:46Z")

</div>

S2 wrote:

> error. But that is an OS configuration problem, not a rubygems one 🙂

Well, if I may add my very amateur voice here, it seems to me that one  
could certainly trap an error and conditionally issue a system call to  
engage the installation process at the super user level. The process  
would then have to worked through manually, since there often are one or  
more interactions which need to occur to complete the installation of  
some gems. This would work fine if the person running the programmer has  
access to superuser passwords.

Don’t know this is the sort of thing you have in mind or not.

t.

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 13, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791/6 "2008-03-13T18:18:38Z")

</div>

S2 wrote:

> > > error. But that is an OS configuration problem, not a rubygems one 🙂  
> > > Yes, this is a good alternative.
> 
> > Don’t know this is the sort of thing you have in mind or not.
> 
> I thought that rubygems had his way of doing this automagically, but  
> after reading your response it looks like this is not the case and  
> it’s also not really feasible to do easily.

Well, its apparent that when one manually installs a gem (“sudo install  
slideshow”, for example), those already installed are checked to see if  
they need to be updated, but normally calling programmatically upon an  
uninstalled gem, in any way, produces an error.

t.

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 21, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791/7 "2008-03-21T22:56:53Z")

</div>

On Mar 13, 2008, at 01:20 AM, S2 wrote:

> require ‘HOTP’
> 
> ## puts HOTP.new.calculate\_hotp(‘12345’, 1, 6)
> 
> Is there a way to make rubygems autoinstall the HOTP gem if it is  
> not already installed?

No, as the list of files in a gem is only available after installation.

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 16, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/autoinstall-gems/131791/8 "2008-03-16T17:27:36Z")

</div>

On 13 Mar 2008, at 08:20, S2 wrote:

> Is there a way to autoinstall a gem when it is “required” in an .rb  
> file?

Kind of, but not really. The already discussed stuff is important, but  
also is the fact that for some gems (actually quite a number of gems),  
the gem name does not match the require. Anyway, enough reasoning,  
here’s solutioning:

> * * *

#!/usr/bin/env ruby  
require ‘rubygems’

%w[HOTP].each |gemname|  
begin  
require gemname  
rescue LoadError  
win = Gem.win\_platform? rescue RUBY\_PLATFORM =~ /(ms|cyg)win|mingw/  
command = “#{‘sudo’ unless win} gem#{’.bat’ if win} install  
#{gemname}”  
system command  
require gemname  
end  
end

> ## puts HOTP.new.calculate\_hotp(‘12345’, 1, 6)

Untested, and written in the mail client, anyway, you should get the  
idea. N.B. Gem.win\_platform? is rubygems 0.9.5 (iirc) and above. The  
platform match is _ok_, but far from ideal.

Another odd problem that can come up from this is to ensure that  
you’re working on the same drive as rubygems. Sadly, some stuff still  
doesn’t work correctly in different places. That is not really a  
problem on \*nix, which has a single root (maybe).
