How to Install Ruby on Ubuntu 20.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install Ruby on Ubuntu 20.04, let’s briefly understand – What is Ruby?

Ruby is a powerful and dynamic object-oriented programming language. Created by Yukihiro Matsumoto, it combines simplicity and flexibility, making it highly favored for web development. Ruby's elegant syntax and vast library options enable developers to write clean and efficient code.

Its focus on readability and productivity attracts a growing community of programmers worldwide. Its versatility extends beyond web development, as it can be used for automation, data analysis, and many other applications. Discover the endless possibilities with Ruby!

In this tutorial, you will install Ruby in an independent environment on Ubuntu 20.04. We will also address a few FAQs on how to install Ruby on Ubuntu 20.04.

Advantages of Ruby

  1. Simplicity: Ruby's elegant syntax and straightforward code make it easy to read and write.
  2. Flexibility: Its dynamic nature allows developers to modify and update code on the fly.
  3. Vast library options: Access to numerous libraries and gems empowers developers to find solutions quickly and efficiently.
  4. Productivity: Ruby's focus on developer happiness ensures efficient coding and boosts productivity.
  5. Active community: The supportive and growing community of Ruby programmers provides valuable resources and support for developers.

Installing Ruby from Ubuntu Repositories

Using the aptpackage manager to install Ruby on Ubuntu is the easiest way. The version in the Ubuntu repositories at the time of writing is 2.7.0, albeit this may not always be the latest stable release.

The installation process is rather simple. To update the package index and install Ruby, issue the following commands as root or as a user with sudo privileges:

sudo apt update
sudo apt install ruby-full

Upon completion, print the Ruby version to confirm the installation:

ruby --version

The output should resemble the following:

Output

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

Your Ruby version might not be the same as the one displayed above.

All done! You have now successfully installed Ruby on your Ubuntu system and are ready to use it.

Installing Ruby using Rbenv

With the help of the simple command-line utility Rbenv, you may easily change Ruby versions.

By default, rbenv does not manage Ruby installation. Ruby will be installed using ruby-build. It is available as a standalone program as well as a plugin for rbenv.

Ruby is installed from the source by the ruby-build script. Install the essential libraries and compilers to be able to build Ruby:

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

Using the installation shell script is the easiest way to install the rbenv tool. For the script to download and execute, run the following curlcommand:

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash

Both the rbenv and ruby-build repositories from GitHub are cloned to the ~/.rbenv directory by the script.

Another script is called by the installer script to verify the installation. The script's output will appear somewhat like this:

Output

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/config.fish'.

You must add $HOME/.rbenv/bin to your PATH in order to use rbenv.

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

To check whether the installation was successful, run the rbenv -v command:

rbenv -v
Output

rbenv 1.1.2-30-gc879cb0

Enter the following to get a list of all Ruby versions that rbenv can install:

rbenv install -l

For example, to install Ruby version 2.7.1 and set it as a global version, type the following:

rbenv install 2.7.1
rbenv global 2.7.1

Rbenv operates by adding the directory shims to the beginning of your PATH. This directory includes scripts (shims) that are responsible for intercepting Ruby commands and executing the corresponding binaries.

Setting the shell, local, and global Ruby versions is possible with Rbenv.

  • The shell version has the highest priority and is used in the current shell. The rbenv shell <ruby-version> command can be used to specify it by setting the RBENV_VERSION environment variable.
  • The local version is set on a per-directory pretext. This version is written in the .ruby-version file. rbenv looks for the file in the current and all parent directories when you run a Ruby script. It employs the Ruby version stored in the first discovered file. Navigate to the directory and use therbenv local <ruby-version> command to set a local version.
  • When no shell or local version is set, the global version is applied across all shells. To set a global version, use rbenv global <ruby-version>.

When Ruby is managed by rbenv, do not install gems using sudo. Each Ruby version is installed in the ~/.rbenv/versions directory and is writable by the user.

Installing Ruby using RVM

You may install, manage, and work with many Ruby environments using the command-line utility RVM.

Install the prerequisites needed to compile 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

Use the commands below to add the GPG keys and install RVM:

curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg --import -
curl -sSL https://get.rvm.io | bash -s stable

Using the source command, load the script environment variables to begin using RVM:

source ~/.rvm/scripts/rvm

To get a list of every version of Ruby that can be installed using this utility, enter:

rvm list known

With RVM, install the latest stable version of Ruby and make it the default version:

rvm install ruby
rvm --default use ruby

Print the version number to confirm that Ruby was properly installed:

ruby -v
Output

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

Enter the commands listed below to install a specific version of Ruby. The Ruby version you want to install should be substituted for x.x.x:

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

Enter the following to switch to a different version without setting it as your default Ruby:

rvm use ruby-x.x.x

Visit their Documentation page for more details on managing your Ruby installations using RVM.

FAQs to Install Ruby on Ubuntu 20.04

Can I use a specific version of Ruby?

Yes, you can install a specific version by using a version manager like RVM or rbenv. Search online for instructions on how to install and use them.

How do I uninstall Ruby from my Ubuntu system?

Run sudo apt remove ruby-full to remove the Ruby package from your system.

Is there any other package required to develop Ruby applications?

You may need development tools like build-essential to compile and install Ruby gems. Run sudo apt install build-essential to install them.

What if I encounter permission errors while installing gems?

Prefix the installation command with sudo to install gems with administrative privileges. However, it is recommended to use a gemset instead for better project isolation.

How to update Ruby and gems to the latest versions?

Run sudo apt update && sudo apt upgrade ruby-full to update Ruby, and use gem update to upgrade installed gems.

Can I use a Ruby version manager for better control of Ruby installations?

Yes, tools like RVM or rbenv allow you to manage multiple Ruby versions and switch between them effortlessly. Refer to their documentation for installation instructions.

How do I uninstall Ruby from my Ubuntu system?

Run sudo apt remove ruby-full to remove the Ruby package from your system.

Conclusion

We demonstrated three separate methods for installing Ruby on Ubuntu 20.04. The way you select is determined by your preferences and needs. Although it is simpler to install the packaged version from the Ubuntu repository, the Rbenv and RVM scripts provide you more freedom to use various Ruby versions on a per-user basis.

If you have any queries, feel free to post a comment below, and we'll be happy to answer them.