On this page
- Why install Ruby through rbenv on a server?
- Which build packages does Ruby need?
- How do you install rbenv and ruby-build?
- How do you compile and select a Ruby version?
- How do you check that the right Ruby is active?
- How do you pin versions per project and install gems?
- How do you upgrade ruby-build and remove old versions?
- What goes wrong most often with rbenv?
- Questions
Why install Ruby through rbenv on a server?
rbenv gives you a current Ruby without waiting for the distribution, and it lets each application pin the exact version it was tested with. The system packages are perfectly usable for scripts, yet Ubuntu 22.04 still ships Ruby 3.0, Debian 12 ships 3.1 and Rocky or Alma 9 default to 3.0 with newer module streams, while frameworks such as Rails track the latest releases closely. With rbenv the interpreter lives under your user, gems install without sudo, and upgrading one app never breaks another.
rbenv works by putting small shim scripts first on your PATH. When you type ruby or bundle, the shim checks for a .ruby-version file in the current directory or its parents, falls back to the global setting, and runs the matching interpreter.
Which build packages does Ruby need?
ruby-build compiles Ruby from source, so it needs a C toolchain plus headers for OpenSSL, YAML, zlib, readline and libffi. Including rustc enables YJIT, the just-in-time compiler that speeds up most Ruby web apps.
# Ubuntu 24.04 / 22.04 and Debian 12 / 13
sudo apt update
sudo apt install -y git curl build-essential autoconf patch rustc \
libssl-dev libyaml-dev zlib1g-dev libffi-dev libgmp-dev \
libreadline-dev libncurses-dev libgdbm-dev uuid-dev
# Rocky Linux and AlmaLinux 9
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --set-enabled crb
sudo dnf install -y git curl gcc gcc-c++ make patch autoconf bzip2 rust \
openssl-devel libyaml-devel zlib-devel libffi-devel gmp-devel \
readline-devel ncurses-devel gdbm-develThe CRB repository supplies libyaml-devel on the RHEL family; without it the psych extension fails and Ruby installs without YAML support. Work as your normal user from here on, for example deploy, never as root. If you still need one, the sudo user guide shows how.
How do you install rbenv and ruby-build?
Clone both projects from Git and add the init line to your shell startup file. The init command sets up the shims and shell completion.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc
source ~/.bashrc
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
rbenv --versionIf you use zsh, put the same line in ~/.zshrc with zsh in place of bash. A quick health check is available from the rbenv project: curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-doctor | bash reports whether the shims are on PATH and ruby-build is found.
How do you compile and select a Ruby version?
List the stable versions ruby-build knows, install the one you want and make it the global default. The snippet below picks the newest plain release number automatically; you can also type a version such as one listed in your project files.
rbenv install -l
RUBY_VER=$(rbenv install -l | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | tail -n1)
echo "$RUBY_VER"
RUBY_CONFIGURE_OPTS="--disable-install-doc" MAKE_OPTS="-j$(nproc)" rbenv install "$RUBY_VER"
rbenv global "$RUBY_VER"Compiling takes a few minutes on a 2 vCPU plan and somewhat longer on one core. If the build dies with a killed compiler on a 1 GB instance, add a swap file and try again. Skipping the documentation saves time and disk space on a server where nobody reads ri pages.
How do you check that the right Ruby is active?
Confirm the version, the path the shim resolves to and where gems will go. Everything should point into ~/.rbenv, not /usr/bin.
ruby -v
which ruby
rbenv which ruby
ruby --yjit -e 'puts RubyVM::YJIT.enabled?'
gem env home
ruby -ropenssl -ryaml -e 'puts OpenSSL::OPENSSL_VERSION'The YJIT line prints true when Rust was present at build time. The last command loads OpenSSL and YAML; an error there means a header package was missing during the build, so install it and run rbenv install --force for the same version.
How do you pin versions per project and install gems?
Run rbenv local inside an app directory to write a .ruby-version file; commit that file so every machine uses the same interpreter. Gems then install into that version without sudo.
cd ~/apps/blog
rbenv local 3.4.7
cat .ruby-version
gem install bundler
bundle config set --local path vendor/bundle
bundle installReplace 3.4.7 with a version you have installed. Bundler ships with Ruby, and gem install bundler only upgrades it. Setting a local install path keeps each project in its own folder, which makes deploys and clean-ups predictable. Older rbenv setups needed rbenv rehash after installing gems with executables; current versions do that automatically. To run an app as a service, call the shim from systemd with a login-free shell: ExecStart=/home/deploy/.rbenv/shims/bundle exec puma plus WorkingDirectory, as shown in deploying Ruby on Rails.
How do you upgrade ruby-build and remove old versions?
New Ruby releases only appear after you update ruby-build, so pull both repositories before you look for a new version.
git -C ~/.rbenv pull
git -C "$(rbenv root)"/plugins/ruby-build pull
rbenv install -l
rbenv versions
rbenv uninstall 3.3.6Patch releases inside one minor line, such as moving from one 3.4 patch to the next, are safe for most applications: install the new one, update .ruby-version, run bundle install and restart the service. Jumping a minor or major version deserves a test run first, since gems with native extensions must be rebuilt for the new interpreter. Remove a version only after no project file mentions it any more.
What goes wrong most often with rbenv?
Most problems come from PATH order or missing build headers. These checks cover nearly every case:
- ruby -v shows the system version: the init line is missing from your shell file, or a non-interactive script does not read
~/.bashrc. Use full shim paths in cron jobs and units. - Build fails on psych or yaml: install
libyaml-devorlibyaml-develfrom CRB. - openssl extension not built: install
libssl-devoropenssl-devel. - Compiler killed: out of memory during the build; add swap or lower
MAKE_OPTSto-j1. - Permission denied writing gems: you ran gem with sudo once; fix ownership with
sudo chown -R deploy:deploy ~/.rbenv.
Logs from a failed compile are kept in /tmp/ruby-build.*.log; read the last lines before searching anywhere else.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Install Ruby with rbenv
Is rbenv better than RVM or asdf?
They solve the same problem. rbenv is small and only touches PATH through shims, which keeps servers predictable. If your team already uses another manager, staying consistent matters more than the tool.
Can I install rbenv for all users?
You can clone it to /opt/rbenv and set RBENV_ROOT in /etc/profile.d, but per-user installs are simpler and avoid permission problems. Most servers run one app user, such as deploy, so a per-user install is enough.
How long does rbenv install take?
Usually a few minutes on two vCPUs and longer on one. Passing MAKE_OPTS="-j$(nproc)" uses every core.
Should I remove the system Ruby package?
No need. rbenv shims sit first on your PATH, so the system Ruby is only used by system tools that call /usr/bin/ruby directly.
Does rbenv work on FreeBSD?
rbenv itself runs there, but the build dependencies differ and this page only covers Linux images. On FreeBSD 14 the packaged ruby from pkg is the simpler route.