Linux

How to Install Ruby on Ubuntu 20.04

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:

  • From the standard Ubuntu repository. This is the easiest way to install Ruby on Ubuntu and should be sufficient for most use cases. The version included in the Ubuntu repository is 2.7.0.
  • Using Rbenv. Script that allows you to install multiple versions of Ruby on the same machine.
  • Using RVM (ruby environment manager). Heavier scripts and more features that allow you to install, manage, and work with several ruby ​​versions.

Choose the installation method that is suitable for your environment. If you are developing a Ruby application and working in several Ruby environments, the preferred way is to install Ruby using Rbenv or RVM.

If you are not sure which version of Ruby will be installed, see the application documentation that will be used.

Install Ruby from the Ubuntu Repository

An easy way to install Ruby on Ubuntu is to use the apt package manager. At the time of writing, the version in the Ubuntu repository is 2.7.0, which may not always be the latest stable release.

Installation is quite easy. Run the following command as root or a user with sudo rights to update the package index and install Ruby:
sudo apt update
sudo apt install ruby-full

When finished, verify the installation by printing a Ruby version:

ruby --version

The output will look like this:

Output :

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

Your version of Ruby may differ from the one shown above.

There she is! You have successfully installed Ruby on your Ubuntu machine, and you can start using it.

Install Ruby using Rbenv

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

By default, rbenv does not handle Ruby installation. We will 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 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 an installation shell script. Run the following curls or to download and run the script:

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

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

The installer script also invokes other scripts that verify installation. The output from the script will look like below:

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

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

  • If you use 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

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

rbenv -v

Output :

rbenv 1.1.2-30-gc879cb0

To get a list of all versions of Ruby 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 must type:
rbenv install 2.7.1
rbenv global 2.7.1

Rbenv works by entering a directory called shims on the front of your PATH. This directory contains scripts (shims). His job is to intercept Ruby commands and run the appropriate binaries.

Rbenv allows you to set the Ruby, local and global versions of the shell:

  • The shell version is used in the current shell, and has the highest priority. That can be defined by setting the RBENV_VERSION environment variable using the rbenv shell <ruby-version> command.
  • Local version is set on a per directory basis. This version is written in a .ruby-version file. When you run a Ruby script, look for the file in the current directory and all parent. This uses the version of Ruby that is saved in the file that was first discovered. To set the local version, navigate to the directory and run the local <ruby-version> command.
  • The global version is used in all shells when no shell or local version is set. Use global rbenv <ruby-version> to set the global version.

Don’t use sudo to install gems when Ruby is managed with rbenv. Each version of Ruby is installed in the ~ / .rbenv / version directory and is writable by the user.

Install Ruby using RVM

RVM is a command line tool that you can use to install, manage, and work with several 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 command to add the GPG key and install RVM:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable

To start using RVM, load the script’s environment variable using the source command:

source ~/.rvm/scripts/rvm

To get a list of all versions of Ruby 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 ruby
rvm --default use ruby

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

ruby -v

Output :

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

If you want to install a certain version of Ruby, enter the command below. Replace x.x.x with the version of Ruby that you want to install:
rvm install ruby-x.x.x
rvm --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 on managing your Ruby installation with RVM, check out 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 needs and preferences. Although installing package versions of the Ubuntu repository is easier, Rbenv and RVM scripts give you more flexibility to use different Ruby versions on a per user basis.

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

Related posts

How to Set Up an FTP Server on Ubuntu

Linux

How to Add Elements to a List in Python (add, add and enter)

Linux

How to Install Elasticsearch, Logstash, and Kibana (ELK Stack) on Ubuntu 20.04

Linux

How to Install Tomcat 9 on Ubuntu 20.04

Linux

How to Use a Loop in a Playbook Possible

Linux

How to Install Python on a Mac

Apple

How to use the chmod command on Linux

Howto

How to Use Sudo Commands on Linux

Linux

How to delete a Terminal on Ubuntu and other Linux distributions

Linux