Ruby remains one of the most popular programming languages for web development, scripting, and automation. Whether you need it for Rails applications, Chef configuration management, or general-purpose scripting, getting the right Ruby version installed on Ubuntu matters. This guide covers three installation methods on Ubuntu 24.04 and 22.04: the quick APT install to version-managed setups with rbenv and RVM.
The default Ruby packages in Ubuntu repositories are stable but lag behind upstream releases. Ubuntu 24.04 ships Ruby 3.2, while Ubuntu 22.04 provides Ruby 3.0. If your project needs Ruby 4.0.x or a specific 3.x version, rbenv or RVM gives you full control over which Ruby version runs in your environment.
Prerequisites
Before starting, make sure you have:
- Ubuntu 24.04 or 22.04 server/desktop with sudo privileges
- A stable internet connection for downloading packages
- At least 2 GB of free disk space (more if compiling from source via rbenv/RVM)
Update your package index before proceeding with any installation method.
sudo apt update && sudo apt upgrade -y
Method 1: Install Ruby from Ubuntu APT Repositories
The fastest way to get Ruby running is through the default Ubuntu repositories. This method works well for system-level scripting, running tools that depend on Ruby, or situations where you don’t need the absolute latest version.
Install Ruby and its essential dependencies with a single command.
sudo apt install -y ruby-full
The ruby-full meta-package pulls in the Ruby interpreter, standard library, RDoc documentation generator, and IRB interactive console. On Ubuntu 24.04 this installs Ruby 3.2, while Ubuntu 22.04 provides Ruby 3.0.
Confirm the installed version.
ruby --version
On Ubuntu 24.04, you should see output similar to this:
ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [x86_64-linux-gnu]
On Ubuntu 22.04, the version will be:
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu]
If you need a newer Ruby version for your project, continue to Method 2 (rbenv) or Method 3 (RVM) below.
Method 2: Install Ruby Using rbenv (Recommended)
rbenv is the recommended approach for development work. It lets you install multiple Ruby versions side by side and switch between them per-project or globally. Unlike system-wide installations, rbenv keeps everything in your home directory. No sudo required for gem installs, and no risk of breaking system Ruby dependencies.
Install build dependencies
Ruby needs to be compiled from source when using rbenv. Install the required build tools and libraries first.
sudo apt install -y build-essential libssl-dev libreadline-dev zlib1g-dev libsqlite3-dev libyaml-dev libffi-dev libgdbm-dev libncurses-dev curl git
These packages cover the compiler toolchain and libraries that Ruby’s extensions require during compilation. Missing any of these leads to cryptic build failures.
Install rbenv and ruby-build
Clone rbenv directly from GitHub into your home directory.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Add rbenv to your shell PATH and initialize it. This ensures rbenv is available in every new terminal session.
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
source ~/.bashrc
Next, install the ruby-build plugin, which provides the rbenv install command for downloading and compiling Ruby versions.
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Verify the rbenv installation is working correctly.
rbenv --version
You should see the version output confirming rbenv is ready:
rbenv 1.3.2
Install a specific Ruby version
List all available Ruby versions to find the one you need.
rbenv install -l
This shows only the latest stable versions. For the full list including patch releases, use rbenv install -l --all. As of March 2026, the latest stable Ruby release is 4.0.2.
Install Ruby 4.0.2. This compiles Ruby from source, so it takes a few minutes depending on your hardware.
rbenv install 4.0.2
Once the build completes, set it as your default Ruby version system-wide for your user account.
rbenv global 4.0.2
To use a different Ruby version for a specific project, navigate to the project directory and set a local version. This creates a .ruby-version file that rbenv reads automatically.
cd /path/to/your/project
rbenv local 3.4.9
Verify that rbenv is serving the correct Ruby version.
ruby --version
The output should confirm Ruby 4.0.2 (or whichever version you set):
ruby 4.0.2 (2026-03-12 revision abc123) [x86_64-linux]
Run rbenv rehash after installing gems that provide executables. This regenerates the shim files so the new commands become available in your PATH.
Method 3: Install Ruby Using RVM
RVM (Ruby Version Manager) is an alternative to rbenv that has been around longer. It takes a more heavyweight approach, modifying your shell functions and managing gemsets. Some teams prefer RVM for its built-in gemset isolation, though most modern Ruby projects use Bundler for dependency management instead.
Import GPG keys and install RVM
RVM requires GPG key verification for security. Import the maintainer keys first.
sudo apt install -y curl gnupg2
Import the RVM project GPG keys used to verify the installation script.
gpg2 --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
If the keyserver is unreachable (corporate firewalls sometimes block it), use this fallback:
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
Now install RVM with the stable branch.
curl -sSL https://get.rvm.io | bash -s stable
Load RVM into your current shell session.
source ~/.rvm/scripts/rvm
Verify RVM is working by checking its version.
rvm --version
You should see output showing the installed RVM version and release date.
Install Ruby with RVM
List the Ruby versions RVM knows about.
rvm list known
Install Ruby 4.0.2 using RVM. Like rbenv, this compiles from source.
rvm install 4.0.2
Set it as the default Ruby for all new shell sessions.
rvm use 4.0.2 --default
Confirm the active Ruby version.
ruby --version
The output confirms Ruby is managed by RVM:
ruby 4.0.2 (2026-03-12 revision abc123) [x86_64-linux]
Verify Ruby Installation
Regardless of which installation method you used, run these checks to confirm everything is working properly.
Check the Ruby interpreter version.
ruby --version
Check the RubyGems package manager version. This ships with Ruby and is used to install third-party libraries (gems).
gem --version
You should see a version number like 3.6.2 or newer.
Test the interactive Ruby console (IRB) to make sure the interpreter runs correctly.
ruby -e 'puts "Ruby #{RUBY_VERSION} is working on #{RUBY_PLATFORM}"'
The output confirms Ruby is operational:
Ruby 4.0.2 is working on x86_64-linux
Check where the Ruby binary is located. This is useful for debugging PATH issues.
which ruby
For APT installations, you’ll see /usr/bin/ruby. For rbenv, it will be ~/.rbenv/shims/ruby. For RVM, it will be ~/.rvm/rubies/ruby-4.0.2/bin/ruby.
Install Bundler Gem Manager
Bundler is the standard dependency manager for Ruby projects. It reads a Gemfile in your project directory and installs the exact gem versions specified, ensuring consistent environments across development, staging, and production.
Install Bundler using the gem command.
gem install bundler
If you installed Ruby via APT (system Ruby), you’ll need sudo:
sudo gem install bundler
Verify Bundler is installed and accessible.
bundler --version
You should see the installed Bundler version:
Bundler version 2.6.5 (2026-02-20 commit abc1234)
If you use rbenv, run rbenv rehash after installing Bundler so the bundle command becomes available.
Install Ruby Development Headers
Some Ruby gems contain native C extensions that need to be compiled during installation. Gems like nokogiri, pg, mysql2, and ffi will fail to install without the proper development headers.
If you installed Ruby from APT, install the development package.
sudo apt install -y ruby-dev
You also need the core build tools if they are not already installed.
sudo apt install -y build-essential
For rbenv and RVM users, the development headers are included automatically when Ruby is compiled from source, so no extra packages needed. However, individual gems may require their own system libraries. Here are the most common ones:
sudo apt install -y libpq-dev libmysqlclient-dev libxml2-dev libxslt1-dev libcurl4-openssl-dev
This installs development libraries for PostgreSQL (pg gem), MySQL (mysql2 gem), XML parsing (nokogiri), and curl (typhoeus, curb gems).
Create and Run a Simple Ruby Script
With Ruby installed and working, create a quick script to test the full workflow from writing code to execution.
Create a new file called hello.rb.
vi hello.rb
Add the following Ruby code:
#!/usr/bin/env ruby
# System information script
puts "Ruby Version: #{RUBY_VERSION}"
puts "Ruby Platform: #{RUBY_PLATFORM}"
puts "Hostname: #{`hostname`.strip}"
puts "Current Time: #{Time.now}"
puts ""
# Simple array operation
distros = ["Ubuntu 24.04", "Ubuntu 22.04", "Debian 13", "Rocky Linux 10"]
puts "Supported Linux distributions:"
distros.each_with_index do |distro, index|
puts " #{index + 1}. #{distro}"
end
Run the script with the Ruby interpreter.
ruby hello.rb
You should see output confirming Ruby executes correctly:
Ruby Version: 4.0.2
Ruby Platform: x86_64-linux
Hostname: ubuntu-server
Current Time: 2026-03-24 10:30:15 +0000
Supported Linux distributions:
1. Ubuntu 24.04
2. Ubuntu 22.04
3. Debian 13
4. Rocky Linux 10
You can also make the script directly executable without calling ruby explicitly.
chmod +x hello.rb
./hello.rb
The #!/usr/bin/env ruby shebang line at the top tells the system which interpreter to use. Using env ruby instead of a hardcoded path ensures it works with rbenv, RVM, and system Ruby installations.
Managing Ruby Versions: Quick Reference
Here is a quick comparison of common version management tasks across both tools.
List installed Ruby versions with rbenv:
rbenv versions
List installed Ruby versions with RVM:
rvm list
Uninstall a Ruby version with rbenv:
rbenv uninstall 3.3.10
Uninstall a Ruby version with RVM:
rvm remove 3.3.10
Update the list of available Ruby versions in rbenv. This pulls the latest ruby-build definitions so new releases appear.
cd ~/.rbenv/plugins/ruby-build && git pull
Troubleshooting Common Issues
rbenv: ruby command not found after installation
This usually means rbenv is not initialized in your shell. Verify your ~/.bashrc contains the init lines, then reload it.
source ~/.bashrc
rbenv versions
If you’re using Zsh instead of Bash, add the rbenv init lines to ~/.zshrc instead.
Build fails with OpenSSL errors
Older Ruby versions (below 3.1) may not compile with OpenSSL 3.x which ships on Ubuntu 22.04 and 24.04. If you see OpenSSL-related errors during rbenv install, install OpenSSL 1.1 compatibility headers.
sudo apt install -y libssl-dev
For Ruby versions older than 3.1, you may need to specify the OpenSSL directory explicitly:
RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/lib/ssl" rbenv install 2.7.8
Permission denied when installing gems
If you’re using system Ruby (APT install) and see permission errors with gem install, you have two options. Either install gems system-wide with sudo:
sudo gem install rails
Or configure gem to install to your home directory by adding these lines to ~/.bashrc:
echo 'export GEM_HOME="$HOME/.gems"' >> ~/.bashrc
echo 'export PATH="$HOME/.gems/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
With rbenv or RVM, you never need sudo for gem installs since everything runs in your home directory.
gem install fails with “mkmf.rb cannot find header files”
This means the Ruby development headers are missing. Install them to fix native extension compilation.
sudo apt install -y ruby-dev build-essential
Conclusion
You now have Ruby installed on Ubuntu 24.04 or 22.04 using one of three methods. For production servers running a single application, the APT package is the simplest to maintain. For development machines where you work on multiple projects with different Ruby requirements, rbenv gives you the cleanest version management with minimal shell modifications. RVM remains a solid alternative if your team already uses it.
With Bundler installed, your next step is setting up a project with a Gemfile and running bundle install to manage your dependencies. For web development, you can now proceed to install Ruby on Rails or Sinatra on top of your Ruby installation.