How to Install Ruby on CentOS 7

Updated 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.

This article shows several different ways to install Ruby on CentOS. Each has their own benefits, you can choose one of the installation method that works best for you.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges .

Install Ruby

The easiest way to install Ruby on your CentOS system is through the yum package manager. At the time of writing, the version in the CentOS repositories is 2.0.0 which is pretty outdated.

  1. Install the ruby package with the following command:

    sudo yum install ruby
  2. Once the installation is completed, you can verify that it was successful by printing the Ruby version:

    ruby --version

    The output will look something like this:

    ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

Install Ruby using Rbenv

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

We will also install the ruby-build plugin that extends the core functionality of Rbenv allowing us to easily install any Ruby version from source.

To install Ruby using Rbenv follow the steps below:

  1. First, install the dependencies required by the ruby-build tool:

    sudo yum install git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel
  2. Next, run the following curl command to install both rbenv and ruby-build:

    curl -sL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash -

    The script will clone both rbenv and ruby-build repositories from GitHub to ~/.rbenv directory. The installer script also calls another script which will try to verify the installation. The output of the script will look something like below:

    CentOS Install Ruby using Rbenv

    As you can see in the output above, before starting using rbenv we need to add $HOME/.rbenv/bin to our PATH .

    If you are using Bash, type:

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

    If you are using Zsh type:

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrcecho 'eval "$(rbenv init -)"' >> ~/.zshrcsource ~/.zshrc
  3. Now what we have rbenv installed on our system we can easily install the latest stable version of Ruby and set it as our default version with:

    rbenv install 2.5.1rbenv global 2.5.1

    To list all available Ruby versions you can use: rbenv install -l

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

    ruby -v
    ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

Install Ruby using RVM

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

To install Ruby using RVM follow the steps below:

  1. First we need to install the dependencies required for the RVM utility to be able to build Ruby from source:

    sudo yum install curl gpg gcc gcc-c++ make patch autoconf automake bison libffi-devel libtool patch readline-devel sqlite-devel zlib-devel openssl-devel
  2. Next, run the following commands to install RVM on your system:

    sudo gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDBcurl -sSL https://get.rvm.io | bash -s stable
    CentOS Install Ruby using RVM

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

    source ~/.rvm/scripts/rvm
  3. Install the latest stable version of Ruby with RVM and set it as the default version with:

    rvm install 2.5.1rvm use 2.5.1 --default

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

    ruby -v
    ruby 2.5.1p57 (2018-03-29 revision 63029) [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 7 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.