Ruby powers Ruby on Rails applications and various automation scripts. This guide covers installing Ruby via rbenv, letting you manage multiple Ruby versions per project cleanly.
Prerequisites
- Ubuntu 22.04/24.04 or Debian 11/12 VPS
- Root or sudo access
Step 1 — Install Build Dependencies
sudo apt update
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev \
autoconf bison build-essential libyaml-dev libncurses5-dev \
libffi-dev libgdbm-dev -y
Step 2 — Install rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
Step 3 — Install the ruby-build Plugin
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Step 4 — List Available Ruby Versions
rbenv install -l
Step 5 — Install a Ruby Version
rbenv install 3.3.5
This compiles Ruby from source and can take several minutes.
Step 6 — Set the Global Default Version
rbenv global 3.3.5
Step 7 — Verify Installation
ruby -v
Setting a Per-Project Version
cd myproject
rbenv local 3.2.4
This creates a .ruby-version file in the project directory, automatically switching versions whenever you're in that folder.
Step 8 — Install Bundler (Ruby's Dependency Manager)
gem install bundler
rbenv rehash
Installing Rails (If Building a Rails Application)
gem install rails
rbenv rehash
Installing Project Dependencies
cd myproject
bundle install
Common Errors
"rbenv: command not found" — the shell profile wasn't reloaded correctly; verify the export lines were added to ~/.bashrc and re-source it.
Compilation fails during rbenv install — almost always a missing build dependency; re-run Step 1 to confirm all required packages are installed.
"command not found" after installing a gem with an executable — run rbenv rehash to refresh rbenv's shim links after installing new gems.
Best Practices
- Use
rbenv localper project rather than relying solely on the global version - Commit
Gemfile.lockto version control for reproducible installs - Run
rbenv rehashafter installing any gem that provides a command-line executable
Related Articles
- How to Manage Services with systemd and systemctl
- How to Install Nginx on Ubuntu & Debian
- How to Install and Secure MySQL 8 on Ubuntu & Debian
