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
-
Install build prerequisites —
ruby-buildcompiles 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 -
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 -
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 -
Per-project pinning. Drop a
.ruby-versionin the project root:echo "3.3.6" > /var/www/app/.ruby-version cd /var/www/app && ruby -v -
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-failureIf you must use shims (for
rbenv localto win at runtime), setEnvironment=PATH=/home/deploy/.rbenv/shims:/usr/local/bin:/usr/bin:/binand callruby/bundleplainly.
Operational notes
rbenv installbuilds against the system OpenSSL, libreadline, and libyaml at the time of install. Years later, asudo apt upgradethat bumps OpenSSL major can break the Ruby — rebuild after distribution upgrades that touch the linked libs.gem installwithoutbundlewrites to~/.rbenv/versions/X.Y.Z/lib/ruby/gems/...and is per-Ruby. Use Bundler with aGemfile.lockin production — global gems are a foot-gun.- For multi-app boxes, prefer
rbenv local 3.3.6per project rather thanrbenv global— the global Ruby is what scripts and cron jobs without a.ruby-versionwill use, which is often confusing. RBENV_GEMSETSand 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, runrbenv rehashif newly-installed gems with executables (likerake) do not appear inPATH. 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.