Qadry
unixcop Admin

Install Ruby on CentOS/RHEL 8 with 3 different methods

Introduction

Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

Ruby is seen as a flexible language, since it allows its users to freely alter its parts. Essential parts of Ruby can be removed or redefined, at will. Existing parts can added upon. Ruby tries not to restrict the coder.

Ruby can_be used in diverse applications such as data analysis and prototyping.

In this installation guide, you will learn how to install Ruby on CentOS 8 and RHEL 8 Linux.

Method [1]

Install Ruby on CentOS 8 from repository

We will install it via Appstream Repository

So just follow the steps:

  • Update the system’s packages and repositories with:
sudo dnf update -y
  • Ensure that the packages below has_been installed:
sudo dnf install curl tar gnupg2 -y
  • Install Ruby
sudo dnf install @ruby -y
  • Verify the version of Ruby installed
[root@unixcop ~]# ruby --version
ruby 2.5.9p229 (2021-04-05 revision 67939) [x86_64-linux]
[root@unixcop ~]#

A 2.5.9 version of ruby has been installed.

Method [2]

Install Ruby on CentOS 8 using the RVM Manager

So RVM (Ruby Version Manager) is a command-line tool that allows you to install and work with multiple Ruby environments.

So just follow the steps below:

  • To install rvm, you need to first download the RVM script. So, execute this command below with root user.
curl -sSL https://get.rvm.io | bash
  • When the installation is done, add the regular user to the rvm group which has been created while the installation.
usermod -aG rvm unixcop
  • Then update the environment variables as shown below:
source /etc/profile.d/rvm.sh
  • Then Reload RVM.
rvm reload
  • Install the requirements for CentOS for rvm.
rvm requirements
  • Then Check the versions of Ruby that are available for download with the command below:
rvm list known

Note: At this time, the latest version of Ruby is 3.0.2 as shown above in the screenshot.

  • Install Ruby using the RVM
rvm install -y ruby          #this will install the latest version in rvm list.
OR
rvm install -y ruby 3.0.2    #this will specify the version that you want to install.
  • Also Verify the version of Ruby.
[root@unixcop ~]# ruby --version
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
[root@unixcop ~]#

A 3.0.2 version of ruby has been installed.

  • To make the above version the default version for Ruby, Run:
[root@unixcop ~]# rvm use 3.0.2 --default
Using /usr/local/rvm/gems/ruby-3.0.2
[root@unixcop ~]#

Method [3]

Install Ruby with Rbenv

Rbenv is a lightweight Ruby version management utility which allows you to switch between Ruby versions.

Use the ruby-build plugin to extend the core functionality of Rbenv also to allow you to install any Ruby version.

So follow the steps below:

  • Installing git and other dependencies required.
sudo dnf install git wget gcc bzip2 openssl-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel -y
  • Install rbenv and ruby-build:
wget -q https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer -O- | bash

Notes:

1- The two repositories from GitHub will_be cloned to ~/.rbenv directory.

2- Also you have to add $HOME/.rbenv/bin to your PATH .

So If you are using Bash run:

#Installing and adding rbenv to your path
$ git clone git://github.com/sstephenson/rbenv.git .rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

#Installing and adding ruby-build to your path
$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

If you are using Zsh run:

#Installing and adding rbenv to your path
$ git clone git://github.com/sstephenson/rbenv.git .rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(rbenv init -)"' >> ~/.zshrc

#Installing and adding ruby-build to your path
$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.zshrc
$ source ~/.zshrc
  • Ensure that installation was successful with command:
[root@unixcop ~]# rbenv -v
rbenv 1.1.2-61-g585ed84
[root@unixcop ~]#
  • List all Ruby versions you can install with rbenv :
rbenv install -l

As shown above in the screenshot you may notice that the latest version of ruby at this time is 3.0.2.

  • Install the Ruby 3.0.2 and set it as default version as shown below:
rbenv install 3.0.2
rbenv global 3.0.2
  • Verify that Ruby installed successfully:
[root@unixcop ~]# ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
[root@unixcop ~]#

Conclusion

In this installation guide, we shew you three different methods to install Ruby on your CentOS 8/RHEL8 Linux. The method you choose depends on your requirements and preferences.

That’s all !! Thank you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

x