Ruby, different ways to install it on Ubuntu 20.04

ruby

In the next article we are going to take a look at different ways we can install Ruby on Ubuntu 20.04. Ruby is an open source, object-oriented, general-purpose programming language.

Today we can find several administrators available to install Ruby. These allow you to use multiple versions, and help to switch between versions of Ruby. The most used Ruby managers are rbenv and rvm. Although Ruby is also available in the Ubuntu repository. In the following lines we are going to see how to install this language in Ubuntu 20.04, using these three installation options.

Install Ruby on Ubuntu 20.04

From Ubuntu repositories

The simplest and easiest way to install this language is by using Ubuntu's built-in apt package manager. The version of Ruby that has been installed for me from the apt package manager today is 2.7. Before starting the installation, we are going to update the list of available software by typing in the terminal (Ctrl + Alt + T):

sudo apt update

Next we will use the following command to proceed to installation:

install ruby-full

sudo apt install ruby-full

After completing the installation, run this other command to check if the installation was successful and what version was installed:

version installed with apt

ruby --version

Using RVM

Another tool for install and manage Ruby 3 on Ubuntu and other Gnu / Linux systems es RMV.

To install RVM on Ubuntu 20.04, we will start by updating the available software index by typing in the terminal (Ctrl + Alt + T):

sudo apt update

Now we can start with install RVM dependencies, we will execute the same terminal following command:

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

After the installation of the dependencies, we will only have to execute these other commands to install RVM:

install rvm

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

curl -sSL https://get.rvm.io | bash -s stable

Now we are going to run this other command to activate RVM:

source ~/.rvm/scripts/rvm

At this point we can proceed to the installation of this language typing in the same terminal:

install ruby ​​with rvm

rvm install 3.0.0

To use installed Ruby as default, run the command:

rvm use 3.0.0 --default

To check installation and version, run this other command:

installation with rvm

ruby -v

Using Rbenv

Rbenv is a tool that can be used to switch between different versions of Ruby. To install this language, we will need another ruby-build tool.

Before starting we will use the following command to update the list of available packages:

sudo apt update

Now we will execute the following command to install the necessary dependencies:

sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev

After installing the dependencies, we are going to launch these commands to clone the Rbenv and Ruby-build repositories.

cloning ruby ​​github repository

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

The next step will be to execute the following commands to set PATH in .bashrc:

configure path

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

exec $SHELL

At this point we can install any available version using Rbenv. For check available versions, in the terminal (Ctrl + Alt + T) we just need to execute:

rbenv available versions

rbenv install -l

We can install the version we want by executing a command like the following. For this article, we are going to select version 3.0.0 typing:

install with rbenv

rbenv install 3.0.0

To set global variable, we will have to use the following command:

rbenv global 3.0.0

Replace the version number with the version supported by your environment. For check the installed version, run the command:

version installed with rbenv

ruby -v

Create a sample program

Whatever version you use, after installing Ruby you can create a simple example program. For this we are going to use any text editor to write the Ruby script. We will have to use a file with the extension .rb. For this example I am going to create a file called hi.rb. Knowing this, we execute the following command from the terminal (Ctrl + Alt + T):

vim hola.rb

Inside the file we will paste the following lines. In this script we will see the simple input and output operations. The command Gets It is used to receive information from the user. The command puts it is used in this language to print to the console. In Ruby, the operator + used to combine string values.

ruby example

puts "Escribe tu nombre :"
name = gets.chomp
puts "Hola "+ name +", gracias por probar este tutorial publicado en Ubunlog.com"

To launch this example, we just need to type the following command from the terminal. If the script is error free, it will first print the message 'Write your name'. There we will have to write something and press Enter. Next, it will print the message that we have saved in the variable "name"

ruby example

ruby hola.rb

In these lines we have seen different methods to install Ruby in Ubuntu 20.04. If someone wants to know more about this language, they can go to the project website y check its characteristics or documentation that can be found there.


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.