You can install Ruby on Linux with your distribution's package manager (apt on Ubuntu and Debian, dnf on RHEL, Rocky, AlmaLinux and Fedora). If you need a specific or newer version than the distribution ships, use a version manager such as rbenv. This guide adds current commands to the quick steps above, explains how to choose between the two approaches, and covers verification, gems and common installation errors.
How to Install Ruby on CentOS 7
$ sudo yum install ruby
$ ruby –version
How to Install Ruby on Ubuntu
$ sudo apt-get install ruby-full
$ ruby –version
How to test and verify with ruby with Hello World Program?
# Content of Hello.rb
puts "Hello World"
# How to run Hello.rb
$ ruby Hello.rb
Code language: PHP (php)

Choosing an installation method
The commands above install whatever Ruby version your distribution packages. That works for running scripts and system tools, but application projects often pin a specific Ruby version in .ruby-version or a Gemfile, and distribution packages can be several releases behind. Pick the method that fits how you plan to use Ruby.
| Method | Best for | Trade-off |
|---|---|---|
Distribution package (apt, dnf) | Scripts, system tools, quick setup | Version is fixed by the OS release and is often older |
dnf module streams (RHEL/Rocky/Alma 8 and 9) | Servers that need a newer supported Ruby from the vendor | Only the streams the vendor publishes are available |
| rbenv + ruby-build | Development and apps that need an exact version | Compiles Ruby from source, so it needs build dependencies and takes several minutes |
Corrections to the steps above
CentOS 7 reached end of life on 30 June 2024. Its mirrors have been retired, so sudo yum install ruby usually fails on an unmodified CentOS 7 system. Even when it does work, it installs Ruby 2.0, which has been unsupported for many years and cannot run current gems. Use a supported Enterprise Linux release, such as Rocky Linux, AlmaLinux or RHEL 8/9, with the dnf commands in the next section.
On Ubuntu, refresh the package index before you install. Without that step, apt can fail with Unable to locate package or 404 Not Found errors on a freshly provisioned machine. Install build-essential too, because many gems compile native extensions:
sudo apt update
sudo apt install -y ruby-full build-essential
ruby --version
ruby-full installs the interpreter, headers (ruby-dev) and documentation. The version you get depends on the Ubuntu release. For example, 22.04 ships Ruby 3.0 and 24.04 ships Ruby 3.2.
Installing Ruby on RHEL, Rocky Linux, AlmaLinux and Fedora
On Enterprise Linux 8 and 9, Ruby comes from the AppStream repository. Several versions are offered as module streams, so first list what is available:
# Show the available Ruby streams and which one is the default
dnf module list ruby
# Option A: install the default Ruby
sudo dnf install -y ruby ruby-devel gcc make redhat-rpm-config
# Option B: switch to a newer stream first (replace 3.3 with a stream from the list)
sudo dnf module reset -y ruby
sudo dnf module enable -y ruby:3.3
sudo dnf install -y ruby ruby-devel gcc make redhat-rpm-config
ruby --versionCode language: PHP (php)
dnf module reset clears any stream that is already enabled, and dnf module enable selects the stream you want before the install runs. ruby-devel, gcc and make are needed to build gems with C extensions. On Fedora there are no module streams, so sudo dnf install ruby ruby-devel installs the current packaged version.
Note: If you switch streams on a server where Ruby is already installed, run sudo dnf distro-sync after enabling the new stream so that the installed packages move to it.
Installing a specific version with rbenv
rbenv switches between Ruby versions for each user and each project, and its ruby-build plugin compiles those versions. Install the build dependencies first, then rbenv itself. Run everything as your normal user, not root.
# Ubuntu / Debian build dependencies
sudo apt update
sudo apt install -y git curl build-essential autoconf libssl-dev libyaml-dev \
libreadline-dev zlib1g-dev libffi-dev libgdbm-dev libncurses-dev
# RHEL / Rocky / AlmaLinux 9 build dependencies
# (libyaml-devel is in the CRB repository on EL9)
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --set-enabled crb
sudo dnf install -y git gcc make bzip2 openssl-devel libyaml-devel \
libffi-devel readline-devel zlib-devel gdbm-devel ncurses-develCode language: PHP (php)
Note: On RHEL, the CodeReady Builder repository is enabled with subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms instead of dnf config-manager. On Rocky and AlmaLinux 8, the repository is called powertools.
# Install rbenv and the ruby-build plugin
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
# Load rbenv in every new bash shell, then reload the current one
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc
exec "$SHELL"
# List the latest stable releases and install one
rbenv install -l
rbenv install 3.3.6 # use a version from the list above
rbenv global 3.3.6 # default for this user
ruby --version
which ruby # should print ~/.rbenv/shims/rubyCode language: PHP (php)
Inside a project directory, rbenv local 3.3.6 writes a .ruby-version file so everyone on the team uses the same interpreter. The compile step usually takes a few minutes. If it fails, ruby-build prints the path to a log file that names the missing library.
Verifying the installation
The Hello World test above is a good check. These commands also confirm that RubyGems and the interactive shell work:
ruby --version # e.g. ruby 3.x.x (...) [x86_64-linux]
gem --version # RubyGems version
ruby -e 'puts "Hello World"' # one-line test, prints: Hello World
irb # interactive Ruby shell; type exit to leaveCode language: PHP (php)
To run a script directly instead of through ruby hello.rb, add a shebang line and make the file executable. Lowercase file names such as hello.rb are the Ruby convention.
cat > hello.rb <<'EOF'
#!/usr/bin/env ruby
puts "Hello World from Ruby #{RUBY_VERSION}"
EOF
chmod +x hello.rb
./hello.rbCode language: JavaScript (javascript)
Installing gems and Bundler safely
With rbenv, gems go into your home directory, so gem install bundler works without sudo. With the system Ruby, do not use sudo gem install. It writes files into directories that the package manager owns and can break OS tools that depend on Ruby. Install gems for your user instead:
gem install --user-install bundler
# Add the user gem bin directory to PATH
echo 'export PATH="$(ruby -e "puts Gem.user_dir")/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
bundle --versionCode language: PHP (php)
For applications, list your dependencies in a Gemfile and run bundle install. Bundler then resolves and locks the exact gem versions in Gemfile.lock.
Common errors and fixes
| Error | Cause | Fix |
|---|---|---|
Could not retrieve mirrorlist http://mirrorlist.centos.org on CentOS 7 | CentOS 7 is end of life and its mirrors are offline | Migrate to a supported release such as Rocky, AlmaLinux or RHEL |
E: Unable to locate package ruby-full | Package index is stale or empty | Run sudo apt update first |
Gem::FilePermissionError ... /var/lib/gems | gem install run against the system Ruby without root | Use --user-install or rbenv, not sudo |
mkmf.rb can't find header files for ruby | Ruby development headers are missing | Install ruby-dev (Debian/Ubuntu) or ruby-devel (RHEL family) |
The Ruby openssl extension was not compiled during rbenv install | OpenSSL headers are missing | Install libssl-dev or openssl-devel, then rerun the install |
ruby: command not found or the wrong version after rbenv setup | Shell has not loaded rbenv | Run exec "$SHELL", then check rbenv versions and which ruby |
Best practices
- Use a Ruby version that is still supported upstream. End-of-life versions no longer receive security fixes.
- Keep the system Ruby for OS tools, and use rbenv for application development, so each project controls its own version.
- Commit
.ruby-version,GemfileandGemfile.lockso that CI and production run the same interpreter and gems. - In containers, start from the official
rubyimage tag that matches your version instead of compiling Ruby in every build. - Run
bundle audit(from thebundler-auditgem) or a similar tool in CI to catch gems with known vulnerabilities.
For more distribution-specific guides, see the Linux tutorials. If you are connecting to a remote server from Windows to run these commands, how to log in to a Linux server using PuTTY covers that step.
Frequently asked questions
Which Ruby version does apt install on Ubuntu?
It depends on the Ubuntu release. Ubuntu 22.04 ships Ruby 3.0 and Ubuntu 24.04 ships Ruby 3.2. Run apt policy ruby to see the exact candidate version on your system.
How do I install the latest Ruby version on Linux?
Distribution packages rarely carry the newest release. Install rbenv with the ruby-build plugin, run rbenv install -l to list the latest stable versions, then install one and set it with rbenv global.
Should I use sudo with gem install?
No. With the system Ruby, use gem install --user-install instead, so you do not overwrite files that the package manager owns. With rbenv, gems install into your home directory and sudo is never needed.
Can I still install Ruby on CentOS 7?
CentOS 7 is end of life and its default mirrors are offline, so yum install ruby usually fails. Even when it works, it installs the obsolete Ruby 2.0. Move to Rocky Linux, AlmaLinux or RHEL and install Ruby with dnf.
How do I check where Ruby is installed?
Run which ruby to see which executable your shell uses. The system package is usually /usr/bin/ruby, and rbenv uses ~/.rbenv/shims/ruby. Run ruby --version to confirm the version.
Related tutorials
- More Linux tutorials
- How to Login to Linux Server using Putty?
- How to Install and Configure pre-commit on macOS, Windows, and Linux: Complete Step-by-Step Guide
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals