Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby: code to install gem if missing

Tags:

ruby

rubygems

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...

like image 261
Derick Bailey Avatar asked Oct 27 '09 19:10

Derick Bailey


People also ask

How do I install missing gems?

Install gemsIn the invoked popup, start typing bundler, select bundle install and press Enter . Select Tools | Bundler | Install from the main menu. Open the Gemfile, place the caret at any highlighted gem missing in the project SDK and press Alt+Enter . Select Install missing gems using 'bundler' and press Enter .

Is gem installed with Ruby?

The Gem command is included with Ruby 1.9+ now, and is a standard addition to Ruby pre-1.9.

How do I install a specific gem?

Use `gem install -v` You may already be familiar with gem install , but if you add the -v flag, you can specify the version of the gem to install. Using -v you can specify an exact version or use version comparators.


1 Answers

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})
like image 100
jrhicks Avatar answered Oct 17 '22 20:10

jrhicks