sodcalculator

10 Tech How-To

Install Ruby on Ubuntu: Step-by-Step Guide

Guide 10-59938 · Updated · Step by step

To install Ruby on Ubuntu, you can use the default APT package manager, a version manager like rbenv or RVM, or compile from source. The fastest method is sudo apt install ruby-full, which gives you a stable version, but for development work, a version manager is recommended to easily switch between Ruby versions.

Background: What Ruby Is and Why Install It

Ruby is a dynamic, open-source programming language focused on simplicity and productivity. It was created in the mid-1990s by Yukihiro Matsumoto and is known for its elegant syntax that reads almost like English. Ruby gained widespread popularity with the Ruby on Rails web framework, which powers many well-known websites. Installing Ruby on Ubuntu is a common first step for web development, scripting, and automation tasks.

Ubuntu, a Debian-based Linux distribution, includes Ruby in its official repositories. This means you can install a working Ruby environment without adding third-party sources. However, the version in the repositories may be older than the latest release, which is why many developers opt for version managers.

How to Install Ruby Using APT

The simplest way to install Ruby on Ubuntu is through the Advanced Package Tool (APT). This method installs a version that is compatible with your Ubuntu release and receives security updates from the Ubuntu team.

  1. Open a terminal window (Ctrl+Alt+T).
  2. Update your package list: sudo apt update
  3. Install Ruby and its development libraries: sudo apt install ruby-full
  4. Verify the installation: ruby --version

This installs Ruby, the gem package manager, and common development headers. The ruby-full package is a metapackage that pulls in the core Ruby libraries and tools. If you only need a minimal installation, you can use sudo apt install ruby instead.

How to Install Ruby with a Version Manager

Version managers like rbenv and RVM let you install multiple Ruby versions and switch between them per project. This is essential if you work on different applications that require specific Ruby versions. Here’s how to set up rbenv, a popular lightweight option.

  1. Install 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
  2. Clone rbenv: git clone https://github.com/rbenv/rbenv.git ~/.rbenv
  3. Add rbenv to your shell configuration: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
  4. Add rbenv init to your shell: echo 'eval "$(rbenv init -)"' >> ~/.bashrc
  5. Restart your shell: exec $SHELL
  6. Install ruby-build plugin: git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
  7. Install a specific Ruby version: rbenv install 3.2.2
  8. Set it globally: rbenv global 3.2.2

RVM is another option that also manages gemsets (isolated gem collections). The installation is a single command: curl -sSL https://get.rvm.io | bash -s stable. After reloading your shell, you can run rvm install ruby to get the latest version.

How to Choose the Right Installation Method

Your choice depends on your use case:

  • System-wide default (APT): Best for quick setups, system scripts, or when you just need a Ruby interpreter and don’t care about version specificity.
  • rbenv: Ideal for developers who work on multiple projects with different Ruby versions. It’s lightweight and doesn’t interfere with system Ruby.
  • RVM: Useful if you also want to manage gemsets (separate gem collections per project) or need a more feature-rich manager.
  • Source compilation: Only recommended if you need a custom build with specific compile-time options. It’s time-consuming and requires manual maintenance.

For most users, APT is sufficient. For developers, rbenv is often preferred due to its simplicity and non-invasive nature.

Common Problems and How to Fix Them

Even with straightforward installation, you might encounter issues:

  • Ruby command not found after installation: Ensure your shell’s PATH includes the directory where Ruby was installed. For rbenv, verify that ~/.rbenv/bin is in your PATH and that rbenv is initialized.
  • Permission errors when installing gems: Avoid using sudo gem install. Instead, configure gem to install into your user directory by adding gem: --user-install to your ~/.gemrc file.
  • Missing development headers: When installing gems that need native extensions (like nokogiri), you need the build tools and libraries. Install them with sudo apt install build-essential libssl-dev libxml2-dev libxslt1-dev.
  • Version mismatch after rbenv install: Run rbenv rehash after installing a new Ruby or gems to refresh shims.

Comparison: APT vs. rbenv vs. RVM

Here’s a quick comparison to help you decide:

  • APT: Simple, system-wide, but limited to one version (the one in Ubuntu’s repos). Updates come through the system package manager.
  • rbenv: Lightweight, per-project versioning, no system pollution. Requires a bit of setup and knowledge of shell configuration.
  • RVM: More features (gemsets, per-project gems), but heavier and can override system commands if not configured carefully.

In practice, many developers start with APT and later switch to rbenv when they need more flexibility. The choice is about control vs. convenience.

Safety and Maintenance Tips

After installation, keep your Ruby environment healthy:

  • Regularly update Ruby if you installed via APT: sudo apt update && sudo apt upgrade
  • If using rbenv, update the ruby-build plugin to get new Ruby versions: git -C ~/.rbenv/plugins/ruby-build pull
  • Never run sudo gem update --system on a system Ruby unless you know what you’re doing; it can break system dependencies.
  • Use a Gemfile in your projects to lock gem versions, and run bundle install to manage dependencies.

Frequently Asked Questions

Do I need to install Ruby separately for Rails?

No, Ruby on Rails is a gem (a library) that runs on top of Ruby. Once Ruby is installed, you can install Rails with gem install rails. However, Rails requires a recent Ruby version, so if you’re using APT and the version is old, consider using rbenv.

How do I uninstall Ruby installed via APT?

You can remove it with sudo apt remove ruby-full. This won’t remove gems you’ve installed manually; you’d need to clean those up separately. For rbenv, just delete the ~/.rbenv directory and remove the lines from your shell config.

What is the difference between ruby and ruby-full?

The ruby package is a minimal installation, while ruby-full includes additional libraries, documentation, and development tools. For most users, ruby-full is recommended because it includes what you need to build native extensions.

Can I install Ruby without sudo?

Yes, using a version manager like rbenv or RVM installs Ruby in your home directory, so you don’t need root privileges. This is also safer because you won’t alter system files.

Why does my Ruby version show 3.0.0 when the latest is 3.2.2?

Ubuntu’s repositories often lag behind the latest Ruby release. To get a newer version, use rbenv or RVM. Alternatively, you can add the brightbox/ruby-ng PPA, but it’s generally safer to use a version manager.

How do I check if Ruby is installed correctly?

Run ruby -v to see the version, and gem -v to check the gem manager. You can also run a simple script: ruby -e "puts 'Hello from Ruby'" which should print the message.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →