How to install Ruby on Ubuntu 20.04

s2 0

Ruby is one of the most popular programming languages ​​today. It has elegant syntax and focuses on simplicity and productivity. Ruby is the language behind the powerful Ruby on Rails framework.

In this tutorial, we’re going to show you three different ways to install Ruby on Ubuntu 20.04:

  • From the standard Ubuntu repositories. This is the easiest way to install Ruby on Ubuntu and should be fine for most use cases. The version included in the Ubuntu repositories is 2.7.0.
  • With Rbenv. A script that allows you to install multiple versions of Ruby on the same computer.
  • Using RVM (Ruby Environment Manager). A heavier, more feature-rich script that allows you to install, manage, and work with multiple versions of Ruby.

Choose the installation method that is appropriate for your environment. If you develop Ruby applications and work in multiple Ruby environments, the best way to install Ruby is with Rbenv or RVM.

If you are not sure which version of Ruby to install, see the documentation for the application you plan to deploy.

Install Ruby from Ubuntu repositories

The easiest way to install Ruby on Ubuntu is to use the apt
Package manager. At the time of writing, the version is in the Ubuntu repositories 2.7.0which may not always be the latest stable release.

Installation is pretty straightforward. Run the following commands as root or user with sudo privileges to update the package index and install Ruby:

sudo apt updatesudo apt install ruby-full

When done, verify the installation by printing out the Ruby version:

ruby --version

The output looks something like this:

ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]

Your Ruby version may differ from the one shown above.

That’s it! You have successfully installed Ruby on your Ubuntu computer and are ready to use it.

Ruby with Rbenv. to install

Rbenv is a lightweight command line tool that allows you to easily switch Ruby versions.

By default, rbenv does not install Ruby. We use ruby-build Install Ruby. It is available as a stand-alone program and as a plug-in for rbenv.

the ruby-build Script installs Ruby from source. To build Ruby, install the required libraries and compilers:

sudo apt update
sudo apt install git curl autoconf bison build-essential     libssl-dev libyaml-dev libreadline6-dev zlib1g-dev     libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev

The easiest way to install the rbenv tool is to use the installation shell script. Do the following curl
or to download and run the script:

curl -fsSL | bash

The script clones both of them rbenv
and Ruby construction
Repositories from GitHub for ~/.rbenv Directory.

The installation script also calls another script that verifies the installation. The output of the script looks something like this:

Running doctor script to verify installation...
Checking for `rbenv' in PATH: not found
  You seem to have rbenv installed in `/home/vagrant/.rbenv/bin', but that
  directory is not present in PATH. Please add it to PATH by configuring
  your `~/.bashrc', `~/.zshrc', or `~/.config/fish/'.

To use rbenv you need to add $HOME/.rbenv/bin to your PATH

  • If you are using bash:

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(rbenv init -)"' >> ~/.bashrcsource ~/.bashrc
  • If you are using Zsh:

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrcecho 'eval "$(rbenv init -)"' >> ~/.zshrcsource ~/.zshrc

Run the rbenv -v Command to make sure the installation was successful:

rbenv -v
rbenv 1.1.2-30-gc879cb0

To get a list of all Ruby versions that can be installed with rbenv Enter:

rbenv install -l

For example, to install Ruby version 2.7.1 and set it as the global version, enter:

rbenv install 2.7.1rbenv global 2.7.1

Rbenv works by putting a directory called shims at the front of your PATH. This directory contains scripts (shims) whose job it is to intercept Ruby commands and execute the corresponding binary files.

With Rbenv you can set the shell, local and global Ruby version:

  • The shell version is used in the current shell and has the highest priority. It can be defined by the RBENV_VERSION Environment variable with the rbenv shell <ruby-version> Command.
  • The local version is determined for each directory. This version is in the. written .ruby-version File. When you run a Ruby script, rbenv looks for the file in the current and all parent directories. It uses the version of Ruby that is stored in the first file found. To set a local version, navigate to the directory and run rbenv local <ruby-version> Command.
  • The global version is used in all shells if no shell or local version is set. Use rbenv global <ruby-version> to set a global version.

Do not use sudo to install gems when managing Ruby with rbenv. Every Ruby version is installed in the ~/.rbenv/versions Directory and writable by the user.

Install Ruby with RVM

RVM is a command line tool that you can use to install, manage, and work with multiple Ruby environments.

Install the dependencies needed to build Ruby from source:

sudo apt update
sudo apt install curl g++ gcc autoconf automake bison libc6-dev         libffi-dev libgdbm-dev libncurses5-dev libsqlite3-dev libtool         libyaml-dev make pkg-config sqlite3 zlib1g-dev libgmp-dev         libreadline-dev libssl-dev

Run the following commands to add the GPG key and install RVM:

gpg --keyserver hkp:// --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDBcurl -sSL | bash -s stable

To start using RVM, load the scripting environment variables with the source

source ~/.rvm/scripts/rvm

For a list of all the Ruby versions that can be installed using this tool, type:

rvm list known

Install the latest stable version of Ruby using RVM and set it as the default:

rvm install rubyrvm --default use ruby

Verify that Ruby installed correctly by printing out the version number:

ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

If you want to install a specific version of Ruby, enter the following commands. Substitute x.x.x with the version of Ruby you want to install:

rvm install ruby-x.x.xrvm --default use ruby-x.x.x

To switch to a different version without setting it as the default Ruby, enter:

rvm use ruby-x.x.x

For more information on managing your Ruby installations with RVM, see their Documentation page


We showed you three different ways to install Ruby on Ubuntu 20.04. Which method you choose depends on your needs and preferences. While installing the packaged version from the Ubuntu repository is easier, the Rbenv and RVM scripts give you more flexibility to use different Ruby versions per user.

If you have any questions or feedback, feel free to comment below.