Bruno
Bruno

Reputation: 6449

Determine Location Of Ruby Gems

How do I determine the location of my ruby gems?

Upvotes: 59

Views: 41653

Answers (4)

Victor Cordeiro Costa
Victor Cordeiro Costa

Reputation: 2184

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.

Upvotes: 0

hakunin
hakunin

Reputation: 4231

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

Upvotes: 16

fl00r
fl00r

Reputation: 83680

you can try

gem which rails

to fetch location for particular gem, or

echo $GEM_HOME

to fetch home dir of your gems

Upvotes: 67

Bruno
Bruno

Reputation: 6449

gem environment

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

Upvotes: 62

Related Questions