How to find the path a Ruby Gem is installed at (i.e. Gem.lib_path c.f. Gem.bin_path) How to find the path a Ruby Gem is installed at (i.e. Gem.lib_path c.f. Gem.bin_path) ruby ruby

How to find the path a Ruby Gem is installed at (i.e. Gem.lib_path c.f. Gem.bin_path)


The problem with the checked answer is that you must "require" the rubygem or it won't work. Often this is undesireable because if you're working with an executable gem, you don't want to "require" it or you'll get a bunch of warnings.

This is a universal solution for executables and libs:

spec = Gem::Specification.find_by_name("cucumber")gem_root = spec.gem_dirgem_lib = gem_root + "/lib"

If you want to get really technical, there isn't just one lib directory. The gemspec has a "require_paths" array of all the directorys to search (added to $LOAD_PATH). So, if you want an array of the require_paths, use this:

gem_lib = gem_root + "/" + spec.require_paths[0]

No need for bundler.


After the gem has been loaded with require, you find the lib path using Gem.loaded_specs as follows:

require 'rubygems'require 'cucumber'gem_root = Gem.loaded_specs['cucumber'].full_gem_pathgem_lib = File.join(gem_root, 'lib')


I'm not sure why you're doing this, but if you're on the command line, use gem env.