Question
I can list installed gems with gem list, but that command does not show where each gem is installed.
How can I find the installation location of Ruby gems, and how can I determine in advance where a gem will be installed before installing it?
Short Answer
By the end of this page, you will understand how RubyGems decides where gems are stored, how to inspect the current gem installation paths, how to find the directory for a specific installed gem, and how environment settings such as user installs, Ruby version managers, and custom gem homes affect gem locations.
Concept
Ruby gems are packages of Ruby code that RubyGems installs into one or more directories on your system. The key idea behind this question is gem path resolution: RubyGems uses configuration and environment settings to decide where gems are stored and where Ruby looks for them.
A few important parts are involved:
- System gem directory: a shared location tied to a Ruby installation
- User gem directory: a per-user location, often used when you do not have system write access
- GEM_HOME: the main directory where new gems are installed
- GEM_PATH: the list of directories Ruby searches for installed gems
- Ruby version managers: tools like
rbenv,rvm, orchrubycan change gem locations by switching Ruby installations
This matters because in real development you often need to:
- inspect gem source files
- debug version conflicts
- understand why a gem command is not found
- know whether a gem was installed globally or only for one user
- verify where CI, Docker, or deployment environments install dependencies
RubyGems provides commands to inspect these locations. Instead of only asking “Is this gem installed?”, you often need to ask:
- Where is Ruby installing gems right now?
- Which gem directories is Ruby searching?
- Where is one specific gem located?
Understanding those questions makes Ruby environments much easier to troubleshoot.
Mental Model
Think of Ruby gems like books in a library system.
gem installis like adding a new book.GEM_HOMEis the main shelf where new books are placed.GEM_PATHis the list of shelves Ruby checks when looking for a book.gem listtells you which books exist, but not exactly which shelf each one is on.gem envis the librarian report that tells you where the shelves are.
So if you want the exact location of a gem, you need to inspect the shelf information, not just the catalog of titles.
Syntax and Examples
To inspect gem installation paths, RubyGems provides a few useful commands.
Show the full RubyGems environment
gem env
This prints details such as:
- RubyGems version
- Ruby version
- installation directory
- user installation directory
- gem search paths
- executable directory
Show the main gem installation directory
gem env home
Example output:
/Users/alex/.gem/ruby/3.2.0
This is typically where new gems will be installed for the current setup.
Show all gem search paths
gem env path
Example output:
/Users/alex/.gem/ruby/3.2.0:/usr/local/lib/ruby/gems/3.2.0
Ruby will search these directories for installed gems.
Find the directory of a specific gem from Ruby
ruby -e 'puts Gem::Specification.find_by_name("rake").gem_dir'
Example output:
/Users/alex/.gem/ruby/3.2.0/gems/rake-13.1.0
This gives the exact folder for the installed gem.
Inspect a gem's specification
gem specification rake gem_dir
Depending on your environment, this can also help inspect gem metadata.
See where executables go
gem env | grep "EXECUTABLE DIRECTORY"
This is useful because the gem's files and its command-line executables may be stored in related but different directories.
Example: check before installing
gem env home
gem env path
If gem env home points to your user gem directory, a normal gem install will usually install there. If you are using sudo, a different Ruby, Bundler, or a version manager, the target location may change.
Step by Step Execution
Consider this command:
ruby -e 'puts Gem::Specification.find_by_name("rake").gem_dir'
Here is what happens step by step:
ruby -eruns a short Ruby script directly from the command line.Gem::Specificationaccesses RubyGems metadata about installed gems.find_by_name("rake")searches for the installed gem namedrake.- It returns the gem specification object for that gem.
.gem_dirasks that object for the directory where the gem is installed.putsprints that path.
For example, if the output is:
/Users/alex/.gem/ruby/3.2.0/gems/rake-13.1.0
then:
- the gem name is
rake - the installed version is
13.1.0 - the gem lives inside the
gemssubdirectory of your gem home
Now compare that with:
gem env home
If this prints:
/Users/alex/.gem/ruby/3.2.0
then RubyGems is using that as the main install base, and individual gems will usually be inside:
/Users/alex/.gem/ruby/3.2.0/gems/
with one folder per gem version.
Real World Use Cases
Knowing gem installation paths is useful in many practical situations.
Debugging a gem issue
If a gem behaves unexpectedly, you may want to open its source code:
ruby -e 'puts Gem::Specification.find_by_name("rails").gem_dir'
Then inspect the files directly.
Fixing PATH or executable issues
A gem may install a command like rubocop or bundler, but your shell cannot find it. In that case, inspect:
- the gem installation directory
- the executable directory from
gem env - your shell
PATH
Understanding user vs system installs
On a machine with limited permissions, gems may install into a user directory instead of a system directory. This helps explain why one user can access a gem while another cannot.
Working in CI, containers, or servers
In Docker images or CI pipelines, it is common to verify exactly where dependencies were installed, especially when caching gem directories.
Using multiple Ruby versions
If you switch between Ruby versions, each one often has its own gem set. Checking gem env helps confirm which environment you are actually using.
Real Codebase Usage
In real projects, developers rarely guess gem locations. They inspect the environment and rely on repeatable patterns.
Common patterns
Environment inspection
Before troubleshooting, developers often run:
gem env
ruby -v
which ruby
which gem
This confirms which Ruby installation and RubyGems executable are active.
Guarding against the wrong Ruby
A common issue is having multiple Rubies installed. A project may work in one shell but fail in another because gem belongs to a different Ruby. Checking which ruby and which gem is a simple guard clause before deeper debugging.
Bundler-managed dependencies
In real applications, gems are often installed through Bundler rather than plain gem install. Even then, understanding gem paths helps when:
- debugging native extension builds
- locating cached dependencies
- verifying deployment paths
- diagnosing lockfile and environment mismatches
Configuration-driven installs
Teams may set:
GEM_HOMEGEM_PATH- Bundler path settings
- Ruby version manager configuration
This keeps gem installs predictable across development, CI, and production.
Early validation in scripts
Automation scripts may validate the environment first:
gem env home
If the result is unexpected, the script can stop early instead of installing gems into the wrong location.
Common Mistakes
Beginners often run into a few predictable issues.
Mistake 1: Assuming gem list shows file locations
gem list
This only lists installed gems and versions. It does not tell you their directories.
Better approach
Use:
gem env
or for one gem:
ruby -e 'puts Gem::Specification.find_by_name("rake").gem_dir'
Mistake 2: Confusing gem directory with executable directory
A gem's Ruby files may be in one place, while its executable scripts are linked or stored in another.
Check both
gem env home
gem env | grep "EXECUTABLE DIRECTORY"
Mistake 3: Forgetting about multiple Ruby installations
Broken assumption:
gem env home
You may think this is the only gem location on your machine, but if you switch Ruby versions, the result can change.
Avoid it
Also inspect:
which ruby
which gem
ruby -v
Mistake 4: Using sudo without understanding the effect
Installing with sudo can place gems into a system-wide directory instead of your user gem path.
Why this matters
- permissions change
- another user may or may not see the gem
- later installs may go to a different place
Mistake 5: Expecting one fixed path on every machine
Broken assumption:
/usr/lib/ruby/gems/...
That path is not universal. It depends on:
- operating system
- Ruby installation method
- Ruby version manager
- user installation settings
Mistake 6: Ignoring GEM_HOME and GEM_PATH
Environment variables can override defaults.
echo $GEM_HOME
echo $GEM_PATH
If these are set, they strongly influence where gems are installed and searched for.
Comparisons
Here is a quick comparison of related RubyGems commands and concepts.
| Tool or Concept | What it tells you | Best use |
|---|---|---|
gem list | Installed gem names and versions | Check whether a gem exists |
gem env | Full RubyGems environment | Troubleshoot gem configuration |
gem env home | Main installation directory | See where new gems will usually install |
gem env path | All gem search paths | See where Ruby looks for gems |
Gem::Specification.find_by_name(...).gem_dir | Exact directory of one installed gem | Inspect source files for a specific gem |
which gem | Path to the gem executable | Verify which RubyGems installation you are using |
which ruby | Path to the Ruby executable | Confirm the active Ruby environment |
System install vs user install
| Install type | Typical location | When it happens |
|---|---|---|
| System install | Inside the Ruby installation directories | When using a system Ruby with sufficient permissions |
| User install | Inside a home-directory-based gem path | When using --user-install or limited permissions |
| Version-managed install | Inside directories owned by rbenv, rvm, or similar tools | When using a Ruby version manager |
GEM_HOME vs GEM_PATH
| Variable | Purpose |
|---|---|
GEM_HOME | The primary directory where gems are installed |
GEM_PATH | A list of directories Ruby searches for installed gems |
Cheat Sheet
# Show full RubyGems environment
gem env
# Show main gem install directory
gem env home
# Show all gem search paths
gem env path
# Find exact directory of one installed gem
ruby -e 'puts Gem::Specification.find_by_name("rake").gem_dir'
# Check which Ruby and gem are active
which ruby
which gem
ruby -v
# Check environment overrides
echo $GEM_HOME
echo $GEM_PATH
Rules to remember
gem listshows names and versions, not install directories.gem env homeusually tells you where new gems will be installed.gem env pathshows every directory RubyGems searches.- A specific gem folder is usually inside
GEM_HOME/gems/gem-name-version. - Different Ruby versions often have different gem directories.
sudo, version managers, and environment variables can change install location.
Common path pattern
<gem-home>/gems/<gem-name>-<version>
Example:
/Users/alex/.gem/ruby/3.2.0/gems/rake-13.1.0
FAQ
How do I find the folder for a specific installed gem?
Use RubyGems metadata:
ruby -e 'puts Gem::Specification.find_by_name("rake").gem_dir'
How can I see where Ruby will install gems before installing one?
Run:
gem env home
This usually shows the main target directory for new gem installations.
Why does my gem install to a different location than expected?
Common reasons include:
- a different active Ruby version
GEM_HOMEorGEM_PATHbeing set- using
sudo - using a Ruby version manager
- using a user install instead of a system install
What is the difference between GEM_HOME and GEM_PATH?
GEM_HOMEis the primary install directory.GEM_PATHis the list of directories searched for gems.
Does gem list show where gems are installed?
No. It only shows gem names and versions.
Can different Ruby versions have different gem directories?
Yes. Each Ruby installation typically has its own gem location.
Where do gem executables go?
They may go to a separate executable directory. Check it with:
gem env
and look for the executable directory entry.
Mini Project
Description
Build a small command-line troubleshooting checklist for your Ruby environment. The goal is to inspect where gems are installed, which Ruby is active, and where a specific gem lives. This mirrors the kind of investigation developers perform when a gem is installed but not behaving as expected.
Goal
Create a Ruby environment inspection script that prints the active Ruby, gem home, gem paths, and the install directory for a gem name you provide.
Requirements
- Accept a gem name from the command line
- Print the active Ruby version and executable paths
- Print the gem home and gem search paths
- Print the exact install directory for the requested gem if it exists
- Show a friendly message if the gem is not installed
Keep learning
Related questions
Add a Default Value to an Existing Column in a Rails Migration
Learn how to add or change a default value on an existing database column using Rails Active Record migrations safely.
Calling a Class Method from an Instance in Ruby
Learn how to call a class method from an instance in Ruby using self.class, with examples, pitfalls, and practical usage patterns.
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.