How to Install Ruby on Ubuntu
If you’re setting up a new project or just want to play around with Ruby, getting it installed on Ubuntu is a quick win. This guide will walk you through the most common paths—using the official packages, rbenv, and RVM—and explain why each choice matters.
1. Quick‑start with the default Ubuntu package
sudo apt update sudo apt install ruby-full
ruby-full pulls a stable Ruby version (usually 2.6 or 2.7 on older releases) and its standard library, so you’re ready to run irb or any script right away. It’s the simplest route if you don’t need a newer release.
2. Installing a newer Ruby with rbenv
rbenv gives you fine‑grained control over which Ruby version is active without polluting the system packages.
1. Prerequisites
sudo apt install git build-essential libssl-dev zlib1g-dev \
libreadline-dev libyaml-dev libsqlite3-dev sqlite3
Those libraries let the Ruby source compile cleanly and add essential features like SSL and SQLite support.
2. Clone rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv cd ~/.rbenv && src/configure && make -C src
Builds the runtime that manages Ruby binaries.
3. Add to your shell (for bash)
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc source ~/.bashrc
Makes the rbenv command available immediately.
4. Install ruby-build plugin
git clone https://github.com/rbenv/ruby-build.git \
"$(rbenv root)/plugins/ruby-build"
Turns rbenv install into a one‑liner that fetches, compiles, and installs any Ruby version.
5. Pick a version
rbenv install 3.2.2 rbenv global 3.2.2
Sets 3.2.2 as the default for every shell session, ensuring your scripts run under that Ruby.
6. Verify
ruby -v
You should see ruby 3.2.2p0.
3. Using RVM for flexibility (but with some baggage)
RVM installs Ruby into your home directory and manages gemsets. It’s handy if you frequently switch between projects that need different Ruby environments.
1. Install dependencies
sudo apt install gnupg2 curl
2. Import GPG keys and install RVM
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys \
409B6B1796C275462A1703113804BB82D39DC0E3 \
7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
The key import prevents tampering; the script installs RVM itself.
3. Install Ruby
rvm install 2.7.6 rvm use 2.7.6 --default
4. Verify
ruby -v
4. Common pitfalls to avoid
- Mixing system Ruby and rbenv: If you install gems globally with sudo gem install, they’ll go under /usr/lib/ruby/gems/... and won’t be seen by the Ruby managed by rbenv or RVM. Stick to gem install --user-install unless you’re deliberately editing the system Ruby.
- Missing build tools: Forgetting build-essential or any of the SSL/SQLite libraries will leave you with a broken binary that refuses to run scripts. The error logs usually say “missing libssl” or “undefined symbol”.
- Old Ubuntu releases: On Ubuntu 18.04, ruby-full is 2.3, which is dead and insecure. If you’re on that distro, lean toward rbenv or RVM to get a newer version.