How to Install Ruby on Ubuntu 18.04

How to Install Ruby on Ubuntu 18.04
How to Install Ruby on Ubuntu 18.04

Install Ruby on Ubuntu

Ruby is one of the most popular dynamic and powerful programming languages. Ruby can be used for the development of web applications, games etc. It is mostly used for server-side scripting. It was found in 1993 in Japan. In this tutorial, you are going to learn How to install Ruby on Ubuntu 18.04.

There are two ways to install Ruby on Ubuntu and they are:

  1. Install Ruby using Rbenv (recommended)
  2. Install Ruby using Ubuntu Repository

Prerequisites

Before you start to install Redis on Debian 9. You must have a non-root user account on your server with sudo privileges.

Method 1: Install Ruby using Rbenv

Rbenv is nothing but Ruby version management tool. It is used for switching Ruby versions on the same system if multiple Ruby versions are installed. To actually Install Ruby we are using here Rbenv plugin ruby-build which is used to install any Ruby version easily. Follow the instruction below to install Ruby using Rbenv:

Now Update the apt package manager index by typing below command:

sudo apt update

Install required dependencies for installing rbenv and ruby-build:

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

Navigate to $HOME directory using below command:

cd

Next, clone rbenv package using below command:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
How to install Ruby - Install Rbenv
How to install Ruby – Install Rbenv

After completing above command, Set the path for rbenv and complete the installation by typing below command:

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

Here to install ruby-build plugin for Rbenv run below git command first:

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
How to install Ruby - install ruby-build
How to install Ruby – install ruby-build

After completing above command, Complete the installation and set the path for ruby-build by using below command:

echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

The current latest stable version for Ruby is 2.6.1 at the time of writing this tutorial. Run below command to install Ruby:

rbenv install 2.6.1
How to install Ruby - Installing ruby 2.6.1
How to install Ruby – Installing ruby 2.6.1

Now set the global version on your system running below command:

rbenv global 2.6.1

You should confirm the installation and check the version of Ruby using below command:

ruby -v

The output should be:

ruby 2.6.1 [x86_64-linux-gnu]

Method 2: Install Ruby from Ubuntu Repository

Update the package manager index typing below command:

sudo apt update

Install the Ruby typing:

sudo apt install ruby-full

Confirm the installation and check the version of Ruby using below command:

ruby -v

The output should be:

ruby 2.6.1 [x86_64-linux-gnu]

Running a Simple Ruby Program

Now create and open demo.rb example file using your favourite text editor:

sudo nano demo.rb

Copy and paste following lines inside demo.rb file:

puts "Welcome to Ruby"

Save file and exit the editor using Ctrl+x.

Next run below command and run demo.rb file:

ruby demo.rb

The output should be:

Welcome to Ruby

Conclusion

You have successfully learned how to install Ruby on Ubuntu 18.04. If you have any queries please don’t forget to comment out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here