Site icon DesignLinux

How to Install Ruby on Ubuntu 20.04

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

In this tutorial we will show you three different ways to install Ruby on Ubuntu 20.04:

Choose the installation method that is appropriate for your environment. If you are developing Ruby applications and work multiple Ruby environments, then the preferred way is to install Ruby using Rbenv or RVM.

If you are not sure which Ruby version to install, consult the documentation of the application you’re going to deploy.

Installing Ruby from Ubuntu Repositories

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

The 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

Once completed, verify the installation by printing the Ruby version:

ruby --version

The output will look 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 machine, and you can start using it.

Installing Ruby using Rbenv

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

By default, rbenv doesn’t handle installing Ruby. We’ll use ruby-build to install Ruby. It is available as a standalone program and as a plugin for rbenv.

The ruby-build script installs Ruby from the source. To be able 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 simplest way to install the rbenv tool is to use the installation shell script. Run the following curl or to download and execute the script:

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

The script clones both rbenv and ruby-build repositories from GitHub to the ~/.rbenv directory.

The installer script also calls another script that verifies the installation. The output of the script will look something like below:

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'.

To start using rbenv, you need to add $HOME/.rbenv/bin to your PATH.

Run the rbenv -v command to ensure that 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 a global version, you would type:

rbenv install 2.7.1rbenv global 2.7.1

Rbenv works by inserting a directory named shims at the front of your PATH. This directory contains scripts (shims) whose job is to intercept Ruby commands and execute the corresponding binaries.

Rbenv allows you to set shell, local and global Ruby version:

Do not use sudo to install gems when Ruby is managed with rbenv. Each Ruby version is installed in the ~/.rbenv/versions directory and writeable by the user.

Installing Ruby using RVM

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

Install the dependencies required 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://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDBcurl -sSL https://get.rvm.io | bash -s stable

To start using RVM, load the script environment variables using the source command:

source ~/.rvm/scripts/rvm

To get a list of all Ruby versions that can be installed with this tool, type:

rvm list known

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

rvm install rubyrvm --default use ruby

Verify that Ruby was properly installed by printing 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 commands below. Replace x.x.x with the Ruby version you want to install:

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

To switch to another version without setting it as your default Ruby, enter:

rvm use ruby-x.x.x

For more information about how to manage your Ruby installations with RVM check their Documentation page.

Conclusion

We have shown you three different ways to install Ruby on Ubuntu 20.04. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu repository is easier, the Rbenv and RVM scripts give you more flexibility to use different Ruby versions on a per user basis.

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

ubuntu ruby
Exit mobile version