Skip to content

Installing Ruby with rbenv on Ubuntu and Debian

Install rbenv with ruby-build, compile a project-pinned Ruby, and structure the install so a deploy user gets predictable Ruby versions.

Ruby installed via apt on Ubuntu is invariably the version Ubuntu shipped two years ago, which is invariably not the version your Rails app needs. rbenv solves this the same way nvm and pyenv do for their languages — it installs Rubies side-by-side under the user’s home and shims ruby, gem, bundle, and friends to dispatch to the right one. This article installs rbenv on Ubuntu and pins a Ruby per project.

How to verify

Check the current Ruby situation on the host:

command -v ruby && ruby -v
command -v rbenv && rbenv versions
ls -la "$HOME/.rbenv" 2>/dev/null
dpkg -l | grep -E "^ii\s+ruby" | head
which gem bundle

If apt ruby is the only one and rbenv is missing, plan to remove the apt Ruby after rbenv is in place — keeping both creates PATH ambiguity that will eventually bite a deploy script.

What’s happening

rbenv installs Ruby versions under ~/.rbenv/versions/X.Y.Z/ (the actual compilation is done by the ruby-build plugin). It then puts a directory of “shims” — small executables named ruby, gem, bundle, etc. — earlier on PATH than the system binaries. Each shim, when invoked, asks rbenv “which version should I dispatch to?”, then execs the real binary from the matching versions/X.Y.Z/bin/.

The version-resolution order is RBENV_VERSION env var, the nearest .ruby-version file walking up from the current directory, then the global default. That means a project with .ruby-version committed at the root pins itself regardless of which user runs ruby from where.

The shims are why the integration with systemd takes a moment of thought. systemd will not source .bashrc — to make a Rails service unit see the right Ruby, either invoke the binary by its absolute path under ~/.rbenv/versions/X.Y.Z/bin/ruby or set PATH=/home/deploy/.rbenv/shims:... in the unit explicitly.

The procedure

  1. Install build prerequisites — ruby-build compiles Ruby from source, so it needs the toolchain:

    sudo apt update
    sudo apt install -y git curl build-essential libssl-dev libreadline-dev \
      zlib1g-dev libyaml-dev libxml2-dev libxslt1-dev libffi-dev libgmp-dev \
      libpq-dev libsqlite3-dev autoconf bison
  2. As the deploy user (not root), clone rbenv and ruby-build:

    sudo -iu deploy
    git clone https://github.com/rbenv/rbenv.git ~/.rbenv
    git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(rbenv init -)"' >> ~/.bashrc
    source ~/.bashrc
    rbenv -v
  3. Install a Ruby. The compile takes 3-5 minutes on a 2-vCPU box:

    rbenv install --list 2>&1 | grep -E '^\s*3\.' | tail
    rbenv install 3.3.6
    rbenv global 3.3.6
    ruby -v
  4. Per-project pinning. Drop a .ruby-version in the project root:

    echo "3.3.6" > /var/www/app/.ruby-version
    cd /var/www/app && ruby -v
  5. For systemd, do not rely on the shims — call the binary directly. /etc/systemd/system/app.service:

    [Service]
    User=deploy
    WorkingDirectory=/var/www/app
    Environment=RAILS_ENV=production
    ExecStart=/home/deploy/.rbenv/versions/3.3.6/bin/bundle exec puma -C config/puma.rb
    Restart=on-failure

    If you must use shims (for rbenv local to win at runtime), set Environment=PATH=/home/deploy/.rbenv/shims:/usr/local/bin:/usr/bin:/bin and call ruby / bundle plainly.

Operational notes

  • rbenv install builds against the system OpenSSL, libreadline, and libyaml at the time of install. Years later, a sudo apt upgrade that bumps OpenSSL major can break the Ruby — rebuild after distribution upgrades that touch the linked libs.
  • gem install without bundle writes to ~/.rbenv/versions/X.Y.Z/lib/ruby/gems/... and is per-Ruby. Use Bundler with a Gemfile.lock in production — global gems are a foot-gun.
  • For multi-app boxes, prefer rbenv local 3.3.6 per project rather than rbenv global — the global Ruby is what scripts and cron jobs without a .ruby-version will use, which is often confusing.
  • RBENV_GEMSETS and other gemset features are an rbenv plugin, not built in. The common confusion is expecting rbenv to behave like RVM (which has gemsets built in) — it does not, and Bundler’s grouping fills that role anyway.
  • After upgrading rbenv itself or ruby-build, run rbenv rehash if newly-installed gems with executables (like rake) do not appear in PATH. New Ruby installs auto-rehash, gem installs into an existing Ruby do not.

For deploying a Rails app on this Ruby, see Rails deployment with Passenger or Puma. Stack Harbor’s managed operations practice keeps Ruby on rbenv per-deploy-user with .ruby-version committed — the same machine that runs your staging Rails also runs the production one, and the version mismatches that plagued early-2020s Ruby deploys do not happen.