rbenv and RVM are Ruby version managers: they install Ruby into your home directory and let each project use the version it needs, independently of the Ruby package that Ubuntu ships. In this tutorial you will install rbenv with the ruby-build plugin on Ubuntu 24.04, compile a current Ruby release with YJIT, set global and per-project versions, and install gems with Bundler. A separate section shows how to do the same with RVM if you prefer it.
Prerequisites
To follow this guide you need:
- A server running Ubuntu 24.04 LTS, for example a CubePath VPS.
- A non-root user with
sudoprivileges. Ruby will be installed for this user. - At least 1 GB of RAM and about 1 GB of free disk space. Compiling Ruby on a 512 MB server needs swap.
Choose one version manager and use only that one. rbenv and RVM both modify your PATH and your shell startup files, and installing both for the same user leads to confusing results.
| rbenv | RVM | |
|---|---|---|
| How it switches versions | Shims in PATH, reads .ruby-version | Overrides cd and shell functions |
| Installing Ruby | Through the ruby-build plugin | Built in (rvm install) |
| Gem isolation | Per Ruby version, Bundler per project | Per Ruby version plus named gemsets |
| Footprint | Small, few moving parts | Larger, more features |
rbenv is the lighter choice and works well with Bundler, which already isolates gems per project. The main part of this guide uses rbenv.
Step 1 - Installing build dependencies
Both rbenv (through ruby-build) and RVM compile Ruby from source, so they need a compiler and the development headers of the libraries Ruby links against. rustc is needed to build YJIT, Ruby's just-in-time compiler:
sudo apt update
sudo apt install git curl autoconf patch build-essential rustc libssl-dev libyaml-dev libreadline-dev zlib1g-dev libgmp-dev libncurses-dev libffi-dev libgdbm-dev uuid-dev
Step 2 - Installing rbenv
Ubuntu has an rbenv package, but its ruby-build definitions are too old to know about current Ruby releases. Clone rbenv from its official repository instead:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Add rbenv to your shell. The init command puts its shims directory at the front of PATH and sets up the rbenv shell function:
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc
source ~/.bashrc
Verify that the command is available:
type rbenv
rbenv is a function
Step 3 - Installing ruby-build
ruby-build is the rbenv plugin that downloads and compiles Ruby. Install it into rbenv's plugin directory:
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
List the latest stable release of each Ruby series:
rbenv install -l
3.2.9
3.3.10
3.4.7
jruby-...
truffleruby-...
Only latest stable releases for each Ruby implementation are shown.
The version numbers on your server will be newer. The rest of this guide uses 3.4.7; replace it with the latest CRuby version from your list (the plain numbers at the top).
Step 4 - Installing Ruby
Compile and install Ruby. This takes several minutes, depending on the CPU:
rbenv install 3.4.7
==> Downloading ruby-3.4.7.tar.gz...
==> Installing ruby-3.4.7...
==> Installed ruby-3.4.7 to /home/your_user/.rbenv/versions/3.4.7
Make it the default Ruby for your user:
rbenv global 3.4.7
Verify which Ruby is active and where it comes from:
ruby -v
which ruby
ruby 3.4.7 (...) +PRISM [x86_64-linux]
/home/your_user/.rbenv/shims/ruby
Confirm that YJIT was compiled in. It is off by default and is enabled per process with --yjit or the RUBY_YJIT_ENABLE=1 environment variable:
ruby --yjit -e 'puts RubyVM::YJIT.enabled?'
true
If this prints an error that YJIT is not available, rustc was missing when Ruby was built; install it and run rbenv install again.
Step 5 - Configuring gems and Bundler
Skip generating local documentation for every gem you install. It saves time and disk space on a server:
echo "gem: --no-document" > ~/.gemrc
Bundler ships with Ruby, so you can use it right away:
bundle -v
Bundler version 2.6.9
Gems install into the active Ruby version's directory under ~/.rbenv/versions/, so you never need sudo gem install. When a gem provides a command, rbenv creates a shim for it automatically.
Step 6 - Pinning a Ruby version per project
rbenv chooses the Ruby version from a .ruby-version file in the current directory or any parent directory, and falls back to the global version. Create a project and pin its version:
mkdir ~/myapp
cd ~/myapp
rbenv local 3.4.7
cat .ruby-version
3.4.7
Commit .ruby-version with your code so every developer and server uses the same Ruby. Now create a Gemfile and add a gem to test Bundler:
bundle init
bundle add rake
bundle exec rake --version
rake, version 13.3.0
bundle add writes the gem to Gemfile, resolves it and records the exact version in Gemfile.lock. On another machine, bundle install reproduces the same set of gems.
To see which version is active and why, run:
rbenv version
3.4.7 (set by /home/your_user/myapp/.ruby-version)
Step 7 - Upgrading Ruby and rbenv
To get new Ruby versions, update rbenv and ruby-build with Git, then install the new release next to the old one:
git -C ~/.rbenv pull
git -C "$(rbenv root)"/plugins/ruby-build pull
rbenv install -l
After you move your projects to the new version (update .ruby-version and run bundle install), remove the old one:
rbenv uninstall 3.4.6
Alternative: Installing Ruby with RVM
Skip this section if you installed rbenv. If you want RVM instead, start from a user without rbenv, after completing Step 1.
RVM releases are signed. Import the maintainers' public keys:
gpg --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
Download the installer, review it, and run it to install the stable release:
curl -sSL -o rvm-installer https://get.rvm.io
less rvm-installer
bash rvm-installer stable
Load RVM into the current shell and check it:
source ~/.rvm/scripts/rvm
type rvm | head -n 1
rvm is a function
Install Ruby and make it the default:
rvm install 3.4.7
rvm use 3.4.7 --default
ruby -v
RVM's distinctive feature is gemsets, named gem collections within one Ruby version. Create one per project and pin both the Ruby version and the gemset with project files:
cd ~/myapp
rvm --ruby-version use 3.4.7@myapp --create
This writes .ruby-version and .ruby-gemset, and RVM switches to that Ruby and gemset whenever you cd into the directory. If you use Bundler, a separate gemset is optional, since Gemfile.lock already pins the gems for each project.
Troubleshooting
rbenv: command not found or ruby still points to /usr/bin/ruby. The shell initialization is missing or was not loaded. Check that ~/.bashrc contains the rbenv init line from Step 2, then open a new SSH session. Also make sure the Ubuntu ruby package is not shadowing rbenv: which -a ruby should list ~/.rbenv/shims/ruby first.
BUILD FAILED during rbenv install. ruby-build prints the path of a log file such as /tmp/ruby-build.20260925103000.12345.log. Open it and look at the last lines; the cause is almost always a missing -dev package from Step 1, for example libyaml-dev (psych) or libssl-dev (openssl).
Compilation is killed or the server becomes unresponsive. The build ran out of memory. Add a swap file or limit parallel jobs with MAKE_OPTS="-j 1" rbenv install 3.4.7.
A command installed by a gem is not found. Run rbenv rehash to regenerate the shims, then hash -r to clear your shell's command cache.
You don't have write permissions for the /var/lib/gems directory. You are using the system Ruby, not rbenv's. Fix the PATH as described above instead of running gem install with sudo.
Conclusion
You installed rbenv and ruby-build on Ubuntu 24.04, compiled Ruby with YJIT support, set a global version, pinned a version per project with .ruby-version and managed gems with Bundler. You also saw how to reach the same result with RVM and its gemsets.
As next steps you can:
- Create a Rails application with
gem install railsandrails new. - Run your app under systemd with
RUBY_YJIT_ENABLE=1set in the unit's environment. - Add
bundler-auditto your workflow to checkGemfile.lockfor gems with known vulnerabilities.
