How to Install RubyGems on Linux (Ubuntu 24.04, Debian 12, AlmaLinux 9)
Get the gem command on Ubuntu, Debian and AlmaLinux, upgrade RubyGems or install it manually from the tarball, and install gems safely without sudo.
RubyGems is Ruby's package manager and provides the gem command. This guide shows how to get a working gem on Ubuntu 24.04, Debian 12 and AlmaLinux 9 / Rocky Linux 9 / CentOS Stream 9, how to upgrade RubyGems itself, how to install it manually from the upstream tarball, and how to install gems without breaking the system Ruby.
Older tutorials on this topic downloaded RubyGems 1.5.0 and ran setup.rb on Ubuntu 14.04 and CentOS 7. Both systems are end-of-life, and that step is rarely needed now: RubyGems ships with Ruby, so installing Ruby gives you gem.
Check whether RubyGems is already installed
ruby --version
gem --version
If both commands print a version, you already have RubyGems and can skip to the usage section. If gem is missing, install Ruby with one of the methods below.
Install Ruby and RubyGems on Ubuntu or Debian
sudo apt update
sudo apt install -y ruby-full build-essential
ruby-full pulls in the interpreter, the gem command, the ri documentation tool and the development headers. build-essential provides the compiler that gems with native extensions need. Ubuntu 24.04 installs Ruby 3.2 this way.
gem --version
Install Ruby and RubyGems on AlmaLinux, Rocky Linux or CentOS Stream 9
sudo dnf install -y ruby rubygems ruby-devel gcc make
The default Ruby in release 9 of these systems is older than what the newest RubyGems needs. Newer Ruby versions are offered as AppStream module streams. List them and, on a system where Ruby is not installed yet, enable a newer one before installing:
sudo dnf module list ruby
sudo dnf module enable -y ruby:3.3
sudo dnf install -y ruby rubygems ruby-devel gcc make
Use a stream that dnf module list ruby actually shows on your system. If Ruby from the default stream is already installed, switch with sudo dnf module reset ruby, enable the new stream and run sudo dnf distro-sync.
Upgrade RubyGems
On a Ruby you installed yourself (from source or with a version manager), RubyGems updates itself:
gem update --system
On a distribution Ruby, RubyGems is owned by the package manager. Update it with sudo apt upgrade or sudo dnf upgrade. Debian and Ubuntu deliberately block gem update --system for the packaged Ruby, and overwriting package-managed files on any distribution leads to conflicts at the next system update. If you need a newer RubyGems than your distribution ships, use a version manager (see below) instead of forcing it.
Install RubyGems manually from the tarball
This is the method older tutorials used. It is still supported upstream and is useful for a Ruby built from source without RubyGems, or to repair a broken installation. The release current at the time of writing is 4.0.24, which requires Ruby 3.2 or newer; check the RubyGems download page for the latest version.
cd /tmp
curl -LO https://rubygems.org/rubygems/rubygems-4.0.24.tgz
tar -xzf rubygems-4.0.24.tgz
cd rubygems-4.0.24
Run the installer with the Ruby you want to equip. Use sudo only when that Ruby lives in a system-wide location such as /usr/local:
sudo ruby setup.rb
gem --version
ruby setup.rb --help lists further options. Do not run this against the Ruby installed by apt or dnf, for the reason given in the previous section.
Use the gem command
Search for a gem, install it, list what is installed and remove it:
gem search '^rails$' -d
gem install drip
gem list
gem uninstall drip
Other common operations:
| Command | What it does |
|---|---|
gem install rails --no-document | Installs without generating documentation, which is faster |
gem install rails -v 7.1.3 | Installs a specific version |
gem update | Updates all installed gems |
gem outdated | Lists gems with newer versions available |
gem cleanup | Removes old versions of installed gems |
gem environment | Shows paths, Ruby version and configured sources |
gem fetch malice | Downloads the .gem file without installing it |
Install gems without sudo
With a system Ruby, gem install writes to a system directory and therefore needs sudo, which mixes gems into paths the package manager also uses. Installing into your home directory avoids that:
gem install --user-install bundler
Executables go to a bin directory below your user gem directory. Find it and add it to your PATH:
echo 'export PATH="$(ruby -e "puts Gem.user_dir")/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
To make this the default for your user, add the option to ~/.gemrc:
echo 'gem: --user-install' >> ~/.gemrc
For applications, use Bundler with a Gemfile so each project records and installs its own gem versions.
Troubleshooting
- "You don't have write permissions": you ran
gem installagainst the system Ruby as a normal user. Use--user-installor a version manager rather thansudo. - "Failed to build gem native extension": the compiler or Ruby headers are missing. Install
build-essentialandruby-full(Debian/Ubuntu) orgcc,makeandruby-devel(RHEL family), plus the development package of the library the gem wraps. - Command not found after installing a gem: the gem's
bindirectory is not inPATH.gem environmentshows the executable directory. - RubyGems refuses to install or update: each RubyGems release has a minimum Ruby version. Upgrade Ruby first.
Reference: the RubyGems download page and the RubyGems Basics guide.
Frequently asked questions
Do I need to install RubyGems separately from Ruby?
Normally not. RubyGems is included with Ruby. Some distributions split it into its own package (rubygems on the RHEL family), but it is installed together with Ruby or with one extra package name, not from a tarball.
Should I use the distribution's Ruby or a version manager?
The distribution's Ruby is fine for running packaged software and simple scripts. For application development or deployment, the RubyGems and Ruby projects both point to version managers such as rbenv (with ruby-build), chruby, RVM or mise: you get a current Ruby in your home directory, gem install works without sudo and gem update --system is allowed.
What is the difference between RubyGems and Bundler?
RubyGems installs and loads individual gems. Bundler resolves the full set of gems an application needs from its Gemfile and locks the exact versions in Gemfile.lock, so every machine runs the same code. Both are developed in the same repository and ship with Ruby.
Where are gems installed?
Run gem environment. "INSTALLATION DIRECTORY" is the default target and "USER INSTALLATION DIRECTORY" is where --user-install puts gems.