# Auto installing a gem

**URL:** <https://www.ruby-forum.com/t/auto-installing-a-gem/65210>\
**Category:** Ruby\
**Created:** [August 16, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/auto-installing-a-gem/65210 "2006-08-16T20:24:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 16, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/auto-installing-a-gem/65210/1 "2006-08-16T20:24:42Z")

</div>

I thought it would be clever to require a gem – and then if it wasn’t  
found – to install it automatically. I tried this for both a local gem  
file and for a remote install. I wasn’t able to get this to work in  
either case and am now of the opinion that the Gem library isn’t  
designed to support this use case.

Here is one version of my code.

workspace\_dir = File.join(File.dirname( **FILE** ), ‘…’, ‘…’)  
package\_dir = File.join(File.dirname( **FILE** ), ‘…’, ‘packages’)  
watir\_dir = File.join(workspace\_dir, ‘watir’)  
gem\_file = File.join(package\_dir, ‘watir-’+watir\_version+’.gem’)  
if File.exists?(watir\_dir) && use\_local\_watir  
$LOAD\_PATH.unshift File.join(workspace\_dir, ‘watir’)  
else  
require ‘rubygems’  
begin  
require\_gem ‘watir’, '\>= ’ + watir\_version  
rescue Gem::LoadError  
require ‘rubygems/installer’  
specs = Gem::Installer.new(gem\_file).install  
puts “Gem installed: #{specs}”  
end

At this point, i’m just sharing what i’ve learned. If any one has  
reached other conclusions, i would be happy to hear about it.

Bret
