Question
I'm trying to install a gem with gem install mygem or update RubyGems with gem update --system, but the command fails with this error:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
How can I fix this permissions error and successfully install the gem or update RubyGems?
Short Answer
By the end of this page, you will understand why RubyGems sometimes fails with a permissions error, what that error means, and the safest ways to install gems. You will also learn the difference between using sudo, installing gems in a user-owned Ruby environment, and using version managers such as rbenv or rvm.
Concept
RubyGems installs packages, called gems, into a directory managed by your Ruby installation. The error:
Gem::FilePermissionError
means Ruby is trying to write files into a location your current user does not own.
A common example is the system Ruby that comes preinstalled on macOS:
/Library/Ruby/Gems/2.0.0
That folder is usually protected because it belongs to the operating system. Regular users can read from it, but not write to it.
Why this happens
When you run a command like:
gem install mygem
RubyGems decides where to place the gem files. If your current Ruby installation is the system Ruby, the default gem path may point to a protected system directory.
This creates a conflict:
gem installneeds to write files- your user account lacks permission to write there
- RubyGems raises a permission error
Why this matters
Understanding this error is important because it affects how you manage Ruby tools on your machine.
In real development, installing gems the wrong way can cause:
- broken system Ruby setups
- conflicts between projects
- hard-to-debug environment issues
- dependence on
sudofor everyday development
The long-term best practice is usually to use a Ruby version manager and install Ruby in your home directory, where you have full control.
Mental Model
Think of your computer like an apartment building.
- The system Ruby directory is the building's maintenance room.
- Your normal user account is a resident.
- Residents are not allowed to change tools in the maintenance room.
gem installis trying to put a new tool into that locked room.
That is why the system says: you do not have permission.
You have three main options:
- Ask the building manager using
sudo.- This gives temporary admin access.
- It works, but it is not always the safest long-term choice.
- Keep your own toolbox in your home directory.
- This means installing gems where your user owns the files.
- Get your own workshop with
rbenvorrvm.- This is usually the cleanest developer setup.
The key idea is simple: the error is not about the gem itself; it is about where Ruby is trying to write files.
Syntax and Examples
Common commands
1. Install with administrator privileges
sudo gem install mygem
2. Update RubyGems with administrator privileges
sudo gem update --system
This may work if you intentionally want to modify the system Ruby installation.
Safer long-term approach: use a Ruby version manager
With a version manager such as rbenv, you install Ruby in your own home directory.
Example workflow:
rbenv install 3.3.0
rbenv global 3.3.0
gem install bundler
Because this Ruby installation belongs to your user, gem installation usually works without sudo.
Install gems only for your user
Another approach is installing gems into a user-owned path.
gem install mygem --user-install
This installs the gem into a directory inside your home folder instead of the system directory.
You may also need to add the gem bin directory to your PATH.
Example:
PATH=
Step by Step Execution
Consider this command:
gem install colorize
Now walk through what happens.
Step 1: RubyGems starts the install
RubyGems downloads or locates the colorize gem.
Step 2: RubyGems checks the installation path
It looks at your current Ruby environment and decides where the gem should be installed.
Example target path:
/Library/Ruby/Gems/2.0.0
Step 3: The operating system checks permissions
Your user tries to write files into that directory.
Step 4: Permission is denied
Because the directory is owned by the system, the write operation fails.
Step 5: RubyGems raises an error
You see:
Gem::FilePermissionError
Example fix using a user install
gem install colorize --user-install
Now the flow changes.
- RubyGems starts installation.
- It chooses your user gem directory instead of the system directory.
- Your user owns that directory.
- The gem installs successfully.
Example fix using sudo
Real World Use Cases
Installing command-line tools
Many Ruby gems provide executable tools.
Examples:
bundlerjekyllcocoapodsrubocop
If these tools install into a protected directory, permission errors appear immediately.
Setting up a local development machine
Developers often install Ruby and gems for:
- Rails apps
- automation scripts
- static site generators
- testing tools
A user-managed Ruby setup avoids repeated permission problems.
Continuous integration and containers
In CI or Docker images, developers usually control the full environment. They install Ruby in writable paths or as root during image setup, so permission issues are handled intentionally.
Multi-project development
Different projects may require different Ruby versions and gem versions. Version managers help isolate those environments so one project's gems do not interfere with another's.
Real Codebase Usage
In real projects, developers usually avoid relying on the system Ruby for application work.
Common patterns
Use Bundler for project dependencies
Instead of manually installing every gem globally:
bundle install
Bundler reads the Gemfile and installs the exact dependencies needed for that project.
Use a version manager
Teams often standardize on tools like:
rbenvrvmasdf
This makes Ruby installations reproducible and user-owned.
Avoid sudo in day-to-day app development
Using sudo for every gem install can lead to mixed permissions and confusing environments.
Check environment before debugging
Developers often inspect:
ruby -v
which ruby
gem env
which gem
These commands help answer:
- Which Ruby is running?
- Is it the system Ruby?
- Where are gems being installed?
Common Mistakes
1. Using the system Ruby for development without realizing it
Broken assumption:
gem install rails
If ruby points to the OS-provided Ruby, this may fail or create a fragile setup.
How to avoid it
Run:
which ruby
which gem
If you see system paths like /usr/bin/ruby, you are probably using the system Ruby.
2. Using sudo for everything
This works:
sudo gem install mygem
But overusing it can create file ownership problems and hide environment issues.
How to avoid it
Prefer a version manager for development work.
3. Installing with --user-install but forgetting PATH
The gem may install correctly, but its command is not found:
mygem: command not found
How to avoid it
Comparisons
| Approach | How it works | Pros | Cons | Best for |
|---|---|---|---|---|
sudo gem install ... | Installs into the system Ruby directory with admin rights | Quick fix | Can affect system Ruby, may cause permission confusion | One-off system-level installs |
gem install --user-install | Installs gems into your home directory | No admin rights needed, safer than system install | May require PATH changes, still tied to current Ruby | Personal tools and lightweight setups |
rbenv or rvm | Installs Ruby and gems in a user-owned environment | Clean, flexible, best for development | Requires initial setup | App development and multiple Ruby versions |
| System Ruby |
Cheat Sheet
Diagnose the problem
which ruby
which gem
ruby -v
gem env
Quick fixes
Install a gem with admin rights
sudo gem install mygem
Update RubyGems with admin rights
sudo gem update --system
Install only for your user
gem install mygem --user-install
Common system path that causes the error
/Library/Ruby/Gems/2.0.0
Add user gem executables to PATH
export PATH="$HOME/.gem/ruby/3.0.0/bin:$PATH"
Best practice
- Avoid using system Ruby for app development
- Prefer
rbenv,rvm, orasdf
FAQ
Why does gem install say I do not have write permissions?
Because RubyGems is trying to install into a system-owned directory that your user cannot modify.
Is using sudo gem install safe?
It can work, but it is best used only when you intentionally want to install into the system Ruby. For normal development, a user-owned Ruby setup is better.
What is the best way to avoid RubyGems permission errors?
Use a Ruby version manager like rbenv, rvm, or asdf so Ruby and gems are installed in your home directory.
What does --user-install do?
It installs gems into a user-specific directory instead of the system gem directory.
Why is the gem installed but the command is still not found?
The gem's executable directory is probably not in your PATH.
Should I update the system RubyGems with gem update --system?
Usually not unless you understand the consequences for your operating system's Ruby. Installing your own Ruby is often the safer choice.
How can I see where RubyGems installs packages?
Run:
gem env
Mini Project
Description
Create a small Ruby environment diagnostic checklist for your machine. The purpose is to identify whether you are using system Ruby, where gems are installed, and which fix is most appropriate. This is useful because permission errors are often caused by environment confusion rather than the gem command itself.
Goal
Build and run a simple shell script that prints your Ruby and RubyGems environment details so you can diagnose gem permission problems quickly.
Requirements
- Print the path to the current
rubyexecutable. - Print the path to the current
gemexecutable. - Print the active Ruby version.
- Print the RubyGems environment.
- Add a short message telling the user whether they may be using a system Ruby.
Keep learning
Related questions
How to Call Shell Commands from Ruby and Capture Output
Learn how to run shell commands in Ruby, capture output, check exit status, and choose the right method for scripts and apps.
How to Check Whether a String Contains a Substring in Ruby
Learn how to check if a string contains a substring in Ruby using include?, match, and multiline string examples.
How to Check if a Hash Key Exists in Ruby
Learn how to check whether a specific key exists in a Ruby hash using key?, has_key?, and include? with clear examples.