How to Install Ruby on Ubuntu & Debian (with rbenv)

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 local per project rather than relying solely on the global version
  • Commit Gemfile.lock to version control for reproducible installs
  • Run rbenv rehash after 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
  • ruby, rbenv, install ruby, ruby on rails, ubuntu, debian
  • 0 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

How to Install PHP on Ubuntu & Debian (Complete Guide)

PHP powers a huge share of the web, including WordPress, Laravel, and Magento. This guide covers...

How to Install Node.js on Ubuntu & Debian (with NVM)

Node.js is a fast JavaScript runtime for building APIs, web applications, and backend services....

How to Install Python on Ubuntu & Debian

Python powers web frameworks like Django and Flask, automation scripts, and data applications....

How to Install Java (OpenJDK) on Ubuntu & Debian

Java powers enterprise applications, build tools like Maven and Gradle, and platforms like...

How to Install Composer for PHP Dependency Management

Composer is PHP's standard dependency manager, used by virtually every modern PHP framework...