How to Install Ruby on CentOS 7

How to Install Ruby on CentOS 7
How to Install Ruby on CentOS 7

Install Ruby on CentOS

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 also 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 CentOS 7.

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

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

Prerequisites

Before you start to install Redis on CentOS 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 versions on the same system if multiple versions are installed. To actually Install Ruby we are using here Rbenv plugin ruby-build which is used to install any version easily. Follow the instruction below to install Ruby using Rbenv:

Install required dependencies for installing rbenv and ruby-build:

sudo yum 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 typing following command in the terminal:

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

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

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 following command using terminal:

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 following:

ruby 2.6.1 [x86_64-linux-gnu]

Method 2: Install Ruby from CentOS Repository

Install the Ruby typing following command in terminal:

sudo ruby install ruby

Confirm the installation and check the version using below command:

ruby -v

The output should be following:

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 the following lines inside demo.rb file:

puts "Welcome to Ruby"

Save the file and exit the editor using Ctrl+x.

Next run following command and run demo.rb file:

ruby demo.rb

The output should be following:

Welcome to Ruby

Conclusion

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here