5

is there some ruby code I can use to install a gem from a local file, if that gem is not installed?

i'm thinking it would look something like:

if !gem_installed("some gem name")
  system "gem install -l local_copy.gem"
end

i don't know if anything exists that lets me check for gems like this or not...

Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
  • Possible duplicate of [Check for Ruby Gem availability](http://stackoverflow.com/questions/1032114/check-for-ruby-gem-availability) – Nic Mar 31 '16 at 17:03

2 Answers2

6

Checking availability is covered in this previous StackOverflow Quesiton

begin
  gem "somegem"
  # with requirements
  gem "somegem", ">=2.0"
rescue Gem::LoadError
  # not installed
end

or

matches = Gem.source_index.find_name(gem.name, gem.version_requirements)

As for the install, it looks like rails uses the system for gem install also

 puts %x(#{cmd})
Community
  • 1
  • 1
jrhicks
  • 14,759
  • 9
  • 42
  • 57
  • 1
    `GEM::LoadError` should be `Gem::LoadError` – Anna Apr 22 '13 at 16:51
  • 1
    You might want to use `Gem.clear_paths` after the system call to `gem install ...`, to actually load the gem. See http://stackoverflow.com/questions/9384756/after-installing-a-gem-within-a-script-how-do-i-load-the-gem – ejoubaud Jul 18 '13 at 01:03
0

This is my way of doing this

['json','date','mail'].each { |req|
    begin
        gem req
    rescue Gem::LoadError
        puts " -> install gem " + req
        Gem.install(req)
        gem req
    end
    require req
}
Xavius Pupuss
  • 160
  • 1
  • 6