Installing Ruby on CentOS 8

Posted by Strikebf on Sat, 19 Feb 2022 14:47:56 +0100

Introduction: Ruby is one of the most popular languages today. It has a concise syntax and is the language behind the Ruby on Rails framework. In this article, we will show you three ways to install Ruby on CentOS 8.


For image download, domain name resolution and time synchronization, please click Alibaba open source mirror station

Ruby is one of the most popular languages today. It has a concise syntax and is the language behind the Ruby on Rails framework.
In this article, we will explore different ways to install Ruby on CentOS 8.
We will show how to install Ruby through CentOS 8 source repository, using Rbenv, and using RVM script. Choose the installation method that best suits your environment.

1, Install Ruby from CentOS source repository

This is the simplest installation method on CentOS. At the time of writing, the Ruby version on the standard CentOS source repository was 2.5.5.
Run the following command as root or other user with sudo permission to install ruby package:

sudo dnf install ruby

Once the installation is complete, you can verify the successful installation of Ruby by printing the Ruby version number.

ruby --version

The output should look like this:

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

Your Ruby version number may not be the same as shown above.
That's all. You have successfully installed Ruby on CentOS and can start using it.

2, Installing Ruby using Rbenv

Rbenv is a lightweight Ruby version management tool that allows you to easily switch Ruby versions.
We will use the ruby build plug-in to extend the core functions of Rbenv and allow you to install any Ruby Version from the source code.
Install git and other dependent software necessary to compile 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 rbenv # and Ruby build:

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

This script will clone the rbenv and Ruby build source code from Github to ~ / Rbenv directory.
Before you start using rbenv, you need to add $home / Rbenv / bin is added to your PATH.
If you are using Bash, enter:

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

If you are using Zsh, enter:

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

Run the rbenv -v command to ensure successful installation:

rbenv -v

The output is as follows:

rbenv 1.1.2-17-g7795476

To get all installable Ruby versions installed through rbenv, enter:

rbenv install -l

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

rbenv install 2.7.0
rbenv global 2.7.0

Print the Ruby version number to verify that Ruby is installed correctly:

ruby -v

Output:

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

3, Installing Ruby using RVM

RVM (Ruby Version Manager) is a command line tool that allows you to install, manage and use multiple Ruby environments.
First, install the dependent software necessary for rvm to build Ruby from the source code:

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

Run the following command to import GPG public key and install RVM:

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:

source ~/.rvm/scripts/rvm

To get all known Ruby versions, enter:

rvm list known

For this example, if you want to install Ruby 2.6 and set it as the default version, you can trigger the following command:

rvm install 2.6
rvm use 2.6 --default

Verify installation:

ruby -v

The output is as follows:

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

For more information on how to manage Ruby installations using RVM, please visit RVM document page.

4, Summary

We have shown you three different ways to install Ruby on your CentOS 8 server. You choose one of them according to your requirements and preferences. Even though it is easy to install Ruby through CentOS source repository, Rbenv and RVM give you more choices. You can add or remove different Ruby versions for each user.

This article is transferred from: Install Ruby Alibaba cloud developer community on CentOS 8

Topics: Ruby CentOS