Determine Location Of Ruby Gems
Asked Answered
S

4

59

How do I determine the location of my ruby gems?

Snowfield answered 19/6, 2012 at 20:54 Comment(2)
See also #5107641Pepita
possible duplicate of Easy Question : Where do gems install to?Pepita
M
67

you can try

gem which rails

to fetch location for particular gem, or

echo $GEM_HOME

to fetch home dir of your gems

Maleki answered 19/6, 2012 at 20:56 Comment(4)
from within a rails program, use ENV['GEM_HOME']Swampy
ENV['GEM_HOME'] return nil in irbMultifaceted
If the gem you are looking for can't be found, this might help: #20602484Shurwood
So for example with rbenv, my gems are stored in ~/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems.Dorindadorine
S
61
gem environment

...should give you all the info you need.

Snowfield answered 19/6, 2012 at 20:55 Comment(1)
this is better for who doesn't have railsSulphurize
M
16

With budler you can do:

bundle info <gem>

Result:

  * pg (1.4.5)
        Summary: Pg is the Ruby interface to the PostgreSQL RDBMS
        Homepage: https://github.com/ged/ruby-pg
        Path: /home/user/.rvm/gems/ruby-3.1.3/gems/pg-1.4.5

Reference: https://bundler.io/v2.4/man/bundle-info.1.html

Monocyclic answered 24/11, 2016 at 13:23 Comment(2)
Deprecated. use bundle info instead of bundle showWeatherworn
This can also find gems installed from version control (e.g.: git)Hodge
N
0

The trivial solution would be:

gem environment

However, it shows a large output with many informations that we do not want to see, such as RUBYGEMS VERSION, RUBY VERSION, INSTALLATION DIRECTORY, USER INSTALLATION DIRECTORY, RUBY EXECUTABLE, GIT EXECUTABLE, EXECUTABLE DIRECTORY, SPEC CACHE DIRECTORY, SYSTEM CONFIGURATION DIRECTORY, RUBYGEMS PLATFORMS, GEM CONFIGURATION, REMOTE SOURCES and SHELL PATH.

We just want the GEM PATH!

I have a solution for this with grep + awk.

gem environment | grep -A 1 "GEM PATHS:" | awk 'NR==2 { sub(/^[[:space:]]+- /, ""); print }'

Explanation 💡

  1. gem environment: This command is used to display information about the RubyGems environment, including details about gem installations, paths, and configurations.

  2. grep -A 1 "GEM PATHS:": This part of the command is used to search for the line containing "GEM PATHS:" in the output of gem environment and include the line that follows it (-A 1) in the output. In other words, it extracts the line with "GEM PATHS:" and the line immediately below it.

  3. awk 'NR==2 { sub(/^[[:space:]]+- /, ""); print }':

    • awk: This is a powerful text processing tool that allows you to manipulate and process text files line by line.

    • 'NR==2': This is an AWK condition that specifies that the following actions should be performed only for the second line of input (the line below "GEM PATHS:").

    • { sub(/^[[:space:]]+- /, ""); print }:

      • sub(/^[[:space:]]+- /, ""): This AWK function (sub) is used to substitute (replace) a pattern with another pattern. In this case, it's used to remove the leading spaces and hyphen (^[[:space:]]+- ) from the line.
      • print: This command is used to print the modified line to the standard output.

So, in summary, the command extracts the line below "GEM PATHS:" from the gem environment output, removes the leading spaces and hyphen from that line, and then prints the resulting file path.

Necrolatry answered 24/8, 2023 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.