List of Installed Gems

List of installed gems?

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

require 'rubygems'

name = /^/i
dep = Gem::Dependency.new(name, Gem::Requirement.default)
specs = Gem.source_index.search(dep)
puts specs[0..5].map{ |s| "#{s.name} #{s.version}" }
# >> Platform 0.4.0
# >> abstract 1.0.0
# >> actionmailer 3.0.5
# >> actionpack 3.0.5
# >> activemodel 3.0.5
# >> activerecord 3.0.5

Here's an updated way to get a list:

require 'rubygems'

def local_gems
Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
end

Because local_gems relies on group_by, it returns a hash of the gems, where the key is the gem's name, and the value is an array of the gem specifications. The value is an array of the instances of that gem that is installed, sorted by the version number.

That makes it possible to do things like:

my_local_gems = local_gems()

my_local_gems['actionmailer']
# => [Gem::Specification.new do |s|
# s.authors = ["David Heinemeier Hansson"]
# s.date = Time.utc(2013, 12, 3)
# s.dependencies = [Gem::Dependency.new("actionpack",
# Gem::Requirement.new(["= 4.0.2"]),
# :runtime),
# Gem::Dependency.new("mail",
# Gem::Requirement.new(["~> 2.5.4"]),
# :runtime)]
# s.description = "Email on Rails. Compose, deliver, receive, and test emails using the familiar controller/view pattern. First-class support for multipart email and attachments."
# s.email = "david@loudthinking.com"
# s.homepage = "http://www.rubyonrails.org"
# s.licenses = ["MIT"]
# s.name = "actionmailer"
# s.require_paths = ["lib"]
# s.required_ruby_version = Gem::Requirement.new([">= 1.9.3"])
# s.requirements = ["none"]
# s.rubygems_version = "2.0.14"
# s.specification_version = 4
# s.summary = "Email composition, delivery, and receiving framework (part of Rails)."
# s.version = Gem::Version.new("4.0.2")
# end]

And:

puts my_local_gems.map{ |name, specs| 
[
name,
specs.map{ |spec| spec.version.to_s }.join(',')
].join(' ')
}
# >> actionmailer 4.0.2
...
# >> arel 4.0.1,5.0.0
...
# >> ZenTest 4.9.5
# >> zucker 13.1

The last example is similar to the gem query --local command-line, only you have access to all the information for a particular gem's specification.

How to check if a gem is installed?

General solution

To get the full list of gems that are installed:

gem list

To test for a particular gem, you can use -i with a regex:

gem list -i "^gem_name$"

(Credit to Timo in the comments for this technique.)

Particular solution for OP

If you can't find data_mapper, it may be that the gem name is different from what you expect.

Also, if you're just doing which brew to find brew, you aren't finding the gem called brew, you're finding the location of the brew executable. Try gem which brew instead.

EDIT:

If you're looking for data_mapper by doing which data_mapper, you probably won't find it. which is a unix program for finding unix executables, and data_mapper probably doesn't have one.

Since your goal is to verify a gem is installed with the correct version, use gem list. You can limit to the specific gem by using gem list data_mapper.

To verify that it's installed and working, you'll have to try to require the gem and then use it in your code.

How can I find all ruby gems installed with sudo gem install?

If your system comes with a built-in ruby, it (and the gems) are in protected folder. To install gems, you need sudo.

Normally, though, you leave the system ruby alone and install newer version with one of the ruby version switchers (RVM, chruby/ruby-install, rbenv/ruby-build). But before you discovered version switchers, you may have installed some gems with sudo into the system ruby.

If which gem and sudo which gem return different paths (which likely means that under sudo you use system ruby and not your custom one), you can safely uninstall all of the gems you installed with sudo.

How to find where gem files are installed

Use gem environment to find out about your gem environment:

RubyGems Environment:
- RUBYGEMS VERSION: 2.1.5
- RUBY VERSION: 2.0.0 (2013-06-27 patchlevel 247) [x86_64-darwin12.4.0]
- INSTALLATION DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
- RUBY EXECUTABLE: /Users/ttm/.rbenv/versions/2.0.0-p247/bin/ruby
- EXECUTABLE DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/bin
- SPEC CACHE DIRECTORY: /Users/ttm/.gem/specs
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-12
- GEM PATHS:
- /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
- /Users/ttm/.gem/ruby/2.0.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/ttm/.rbenv/versions/2.0.0-p247/bin
- /Users/ttm/.rbenv/libexec
- /Users/ttm/.rbenv/plugins/ruby-build/bin
- /Users/ttm/perl5/perlbrew/bin
- /Users/ttm/perl5/perlbrew/perls/perl-5.18.1/bin
- /Users/ttm/.pyenv/shims
- /Users/ttm/.pyenv/bin
- /Users/ttm/.rbenv/shims
- /Users/ttm/.rbenv/bin
- /Users/ttm/bin
- /usr/local/mysql-5.6.12-osx10.7-x86_64/bin
- /Users/ttm/libsmi/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /usr/local/bin

Notice the two sections for:

  • INSTALLATION DIRECTORY
  • GEM PATHS

Gem list not listing all installed gems

This has happened to me in the past when installing rubygems via macports/apt-get and then upgrading it either manually or with update --system. You should be able to find your original list of installed gems somewhere under /opt/local; use locate to find it. You can find the new path with locate as well, but I didn't have any luck when I tried to move the them folders to the new location...so you need to manually re-install all the gems.

Gem list not listing all installed gems

This has happened to me in the past when installing rubygems via macports/apt-get and then upgrading it either manually or with update --system. You should be able to find your original list of installed gems somewhere under /opt/local; use locate to find it. You can find the new path with locate as well, but I didn't have any luck when I tried to move the them folders to the new location...so you need to manually re-install all the gems.

How do I find licenses of all installed ruby gems?

From the Rails console:

For some Gems, which have its license information included inside its spec, you can display them running this from the rails console:

Gem.loaded_specs.each do |name, spec|
puts "#{name}: #{spec.license}"
end

or From your linux bash terminal:

for i in `gem list | cut -d" " -f1`; do echo "$i :" ; gem spec $i license; done

RVM: List all gems in current gemset ignoring global & default

for global:

rvm @global do gem list

for other gemsets:

GEM_PATH=$GEM_HOME gem list

@global is a gemset that all other gemsets inherit for given ruby, it does not inherit for m itself so it's safe to select it and run gem list in it's context.

For all other gemsets you can use the fact that gem list displays gems from all paths available in GEM_HOME and GEM_PATH, resetting GEM_PATH to be equal GEM_HOME will make only one path available - the one from GEM_HOME so gem list will only show gems in the selected gemset, ignoring all other gemsets (at this time the @global, but RVM 2.0 will support multiple gemsets inheritance).

How do I get the local version of a gem from Ruby?

You can try this:

puts Gem.loaded_specs["gem_name"].version


Related Topics



Leave a reply



Submit