Ruby is one of the most popular languages today. It has elegant syntax and is the language behind Ruby on Rails.
In this article, we’ll go over the different ways to install Ruby on CentOS 8.
We will show you how to install Ruby from the default CentOS 8 repositories and use Rbenv and RVM scripts. Choose the installation method that best suits your installation and environment.
Installing Ruby from CentOS repositories
This is the easiest way to install Ruby on CentOS. At the time of writing, the version in the standard CentOS repositories is 2.5.5.
To install the package, run the following command as the root user or a user with sudo ruby rights:
sudo dnf install ruby
Once the installation is complete, you can verify that it was successful by printing the Ruby version:
The output will look something like this:
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]
Your Ruby version may differ from the one shown above.
All! 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 control utility that allows you to easily switch Ruby versions.
We will be using the ruby-build plugin, which extends the core functionality of Rbenv and allows you to install any version of Ruby from source.
Start by installing git and other dependencies required to build Ruby from source:
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:
wget -q https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer -O- | bash
The script will clone the rbenv and ruby-build repositories from GitHub into the ~ / .rbenv directory.
Before using rbenv, you need to add $ HOME / .rbenv / bin to your PATH.
If you are using Bash, enter:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(rbenv init -)"' >> ~/.bashrcsource ~/.bashrc
If you are using Zsh:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrcecho 'eval "$(rbenv init -)"' >> ~/.zshrcsource ~/.zshrc
Run the rbenv -v command to verify that the installation was successful:
For a list of all versions of Ruby that rbenv can install, type:
rbenv install -l
For example, if you want to install Ruby 2.7.0 and set it as the default version, enter:
rbenv install 2.7.0rbenv global 2.7.0
Make sure Ruby was installed correctly by typing the version number:
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 to build rvm Ruby from source:
sudo dnf install curl gcc bzip2 openssl-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel
Run the following commands to import GPG keys and install RVM:
gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDBcurl -sSL https://get.rvm.io | bash -s stable
To start using RVM, you need to run the following source command:
For a list of all known versions of Ruby, type:
rvm list known
For example, if you want to install Ruby 2.6 and set it as the default version, you must run the following commands:
rvm install 2.6rvm use 2.6 --default
Check if the installation is correct:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
For more information on how to manage Ruby installations with RVM, visit the RVM documentation page.
We’ve shown you three different ways to install Ruby on your CentOS 8 server. The method you choose depends on your requirements and preferences. While installing the packaged version from the CentOS repository is easier, the Rbenv and RVM methods give you more flexibility to add and remove different Ruby versions for each user.
If you have any questions or requests, feel free to comment below.