How to install Ruby on Ubuntu 20.04 and setup programming environment?

Overview

Ruby is a popular programming language used for web development, data analysis, and many other applications. It serves as the basis for Ruby on Rails infrastructure. If you're using Ubuntu, you can install Ruby using the RVM (Ruby Version Manager). This tutorial will guide you throughout the installation process and will create a sample program to check its proper functioning. Here's how to do it:

Prerequisites

There are certain prerequisites that need to be met before you begin:

1. Server running Ubuntu 20.04

2. Access to SSH-connected text editor

3. User account with root or sudo access

4. Internet connection

Key

· Red box- input.

· Green box- Output.

Get Started

Step 1: Enter as Root User

sudo -i

Step 2: Update the system

Begin by updating the system repositories using the following command:

sudo apt-get update

Step 3: Install the Dependencies

Before installing RVM, you need to install the dependencies it requires. Open a terminal window and enter the 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

You can confirm the installation by typing "Y" and pressing the Enter key when asked.

Step 4: Installing RVM

Use the GPG command to access the public key server which will verify the RVM installation releases. This command also provides legitimate sources for downloading the latest RVM releases. To install GPG, use the following command:

gpg --keyserver hkp://pgp.mit.edu --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Once you have the dependencies installed, you can install RVM. Enter the following command in your terminal:

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

This command will download and install RVM on your system.

Step 5: Loading RVM

Next, you need to load RVM. Enter the following command in your terminal:

source ~/.rvm/scripts/rvm

This command will load RVM into your shell session.

Note: If you get the following error:

source ~/.rvm/scripts/rvm
-bash: /root/.rvm/scripts/rvm: No such file or directory

You may try using the following command to load rvm files:

source /etc/profile.d/rvm.sh

Step 6: Installing Ruby

Now that RVM is installed and loaded, you can install Ruby by entering the following command in your terminal:

rvm install ruby

This command will download and install Ruby's latest version. By stating the version number, you can also specify a specific Ruby version. For example, to install Ruby 3.0.2, enter the following command:

rvm install 3.0.2

Step 7: Setting the default Ruby version

You can set the default version of Ruby, if you've installed multiple versions, using the following command:

rvm --default use ruby-[3.0.0]

In case, you have downloaded the default ruby version, use the following command to set the default:

rvm --default use ruby

Step 8: Verifying the installation

To verify that Ruby is installed and working correctly, enter the following command in your terminal:

ruby -–version

Step 9: Testing the Ruby Environment

We can start by creating a simple program, which will verify that your environment is properly configured and help you become familiar with creating and executing a Ruby program. Create a new file using your preferred text editor, with the command below:

nano program.rb

Type the following lines in the recently created file:

puts "My First Program"

Save and exit by pressing Ctrl + O and then Ctrl + X. Now, let's run the program, using the following command:

ruby program.rb

Conclusion

You now have Ruby installed on your Ubuntu system using RVM.

Last updated