[ruby] List of installed gems?

Is there a Ruby method I can call to get the list of installed gems?

I want to parse the output of gem list. Is there a different way to do this?

This question is related to ruby rubygems

The answer is


This lists all the gems I have installed.

gem query --local

http://guides.rubygems.org/command-reference/#gem-list

See 2.7 Listing all installed gems


Maybe you can get the files (gems) from the gems directory?

gemsdir = "gems directory"
gems = Dir.new(gemsdir).entries

Gem::Specification.map {|a| a.name}

However, if your app uses Bundler it will return only list of dependent local gems. To get all installed:

def all_installed_gems
   Gem::Specification.all = nil    
   all = Gem::Specification.map{|a| a.name}  
   Gem::Specification.reset
   all
end

use this code (in console mode):

Gem::Specification.all_names

Try it in the terminal:

ruby -S gem list --local

A more modern version would be to use something akin to the following...

require 'rubygems'
puts Gem::Specification.all().map{|g| [g.name, g.version.to_s].join('-') }

NOTE: very similar the first part of an answer by Evgeny... but due to page formatting, it's easy to miss.


From within your debugger type $LOAD_PATH to get a list of your gems. If you don't have a debugger, install pry:

gem install pry
pry
Pry(main)> $LOAD_PATH

This will output an array of your installed gems.


There's been a method for this for ages:

ruby -e 'puts Gem::Specification.all_names'

Here's a really nice one-liner to print all the Gems along with their version, homepage, and description:

Gem::Specification.sort{|a,b| a.name <=> b.name}.map {|a| puts "#{a.name} (#{a.version})"; puts "-" * 50; puts a.homepage; puts a.description; puts "\n\n"};nil

Both

gem query --local

and

 ruby -S gem list --local

list 69 entries

While

ruby -e 'puts Gem::Specification.all_names'

gives me 82

I used wc -l to get the numbers. Not sure if that is the right way to check. Tried to redirect the output to text files and diff'ed but that didn't help - will need to compare manually one by one.