How to Install Ruby on CentOS 8

By 

Published on

4 min read

Install Ruby on CentOS

Ruby is one of the most popular languages today. It has an elegant syntax, and it is the language behind the Ruby on Rails framework.

In this article, we will explore different ways to install Ruby on CentOS 8.

We’ll show how to install Ruby from the default CentOS 8 repositories and using the Rbenv and RVM scripts. Choose the installation method that is most appropriate for your setup and environment.

Installing Ruby from the CentOS repositories

This is the easiest method for installing Ruby on CentOS. At the time of writing, the version in the standard CentOS repositories is 2.5.5.

Run the following command as root or user with sudo privileges to install the ruby package:

Terminal
sudo dnf install ruby

Once the installation is completed, you can verify that it was successful by printing the Ruby version:

Terminal
ruby --version

The output will look something like this:

output
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]

Your Ruby version may differ from the one shown above.

That’s it! You have successfully installed Ruby on your CentOS system, and you can start using it.

Install Ruby with Rbenv

Rbenv is a lightweight Ruby version management utility that allows you to easily switch Ruby versions.

We’ll use the ruby-build plugin that extends the core functionality of Rbenv and allow you to install any Ruby version from source.

Start by installing git and other dependencies required to build Ruby from the source:

Terminal
sudo dnf install git wget gcc bzip2 openssl-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel

Run the following command to install both rbenv and ruby-build:

Terminal
wget -q https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-installer -O- | bash

The script will clone both rbenv and ruby-build repositories from GitHub to ~/.rbenv directory.

Before starting using rbenv, you need to add $HOME/.rbenv/bin to your PATH .

If you are using Bash, type:

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

If you are using Zsh type:

Terminal
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
source ~/.zshrc

Run the rbenv -v command to ensure that installation was successful:

Terminal
rbenv -v
output
rbenv 1.1.2-17-g7795476

To get a list of all Ruby versions that can be installed with rbenv enter:

Terminal
rbenv install -l

For example, if you want to install the Ruby 2.7.0 and set it as default version you would type:

Terminal
rbenv install 2.7.0
rbenv global 2.7.0

Verify that Ruby was properly installed by printing the version number:

Terminal
ruby -v
output
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

Installing Ruby with RVM

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

First, install the dependencies required for rvm to build Ruby from source:

Terminal
sudo dnf install curl gcc bzip2 openssl-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel

Run the following commands to import the GPG keys and install RVM:

Terminal
gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable

To start using RVM you need to run the following source command:

Terminal
source ~/.rvm/scripts/rvm

To get a list of all known Ruby versions type:

Terminal
rvm list known

If for example, you want to install Ruby 2.6 and set it as the default version you would issue the following commands:

Terminal
rvm install 2.6
rvm use 2.6 --default

Verify the installation:

Terminal
ruby -v
output
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

For more information about how to manage your Ruby installations with RVM visit the RVM Documentation page .

Conclusion

We have shown you three different ways to install Ruby on your CentOS 8 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the CentOS repository is easier, the Rbenv and RVM methods give you more flexibility for adding and removing different Ruby versions on a per user basis.

If you have any questions or feedback, feel free to comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page