Last Updated: February 25, 2016
· samhalicke

quickly find where a gem is installed

Sometimes you need to know where a gem is installed, or at least, where Ruby thinks a gem is installed, from within Ruby.

Use the find_by_name method of Gem::Specification:

[1] pry(main)> Gem::Specification::find_by_name('puppet').gem_dir
=> "/usr/lib/ruby/gems/1.9.1/gems/puppet-3.1.1"


Use find_by_name without .gem_dir for a lot more info.

Additional tip: from the commandline, gem which might be what you need, but note that this will return the full path to the library, not the containing directory.

2 Responses
Add your response


Why not use gem which pry?

over 1 year ago ·

@hanjianwei gem which returns results that include the full name of the gem (e.g. gem which bundler returns a path ending in bundler.rb where in this case i want only the directory name)

In this particular case, I want to know the gem's directory from inside Ruby, not necessarily from the command line. I use pry here to demonstrate interactively.

over 1 year ago ·