Monday July 11th, 2016


Install Ruby on CentOS 7

In this tutorial, we will be installing rbenv and ruby-build on CentOS7. But before we diving let's explain what are rbenv and ruby-build.

rbenv

rbenv stands for ruby environment and it is a ruby version manager, basically it is a tool for managing multiple ruby versions installed on the same machine. For example you can have a given ruby version which is global and set another given version for a specific directory. 

ruby-build

ruby-build is basically a plugin which is exposed as rbenv install, when running rbenv install ruby_version, the ruby-build is actually the one running behind the scene. So ruby-build downloads, compiles and installs a given ruby version via rbenv install.


Installation

First, install the following RPM packages dependencies.

$ sudo yum install -y git-core zlib zlib-devel gcc-c++ 
$ sudo yum install -y patch readline readline-devel 
$ sudo yum install -y libyaml-devel libffi-devel curl
$ sudo yum install -y openssl-devel make bzip2 bison
$ sudo yum install -y autoconf automake libtool sqlite-devel

Now, let's install rbenv for a given user from the source code. In my case, I will use "kalilou" as my user. cd /home/kalilou 

Clone the source code

$ git clone git://github.com/sstephenson/rbenv.git .rbenv

Now we will export the PATH in the ~/.bashprofile file in your home directory. As note, for the Zsh terminal use the ~/.zshrc file instead of the ~/.bashprofile file

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

Let's reload the current terminal so the changes will take effect

$ exec $SHELL

 or same shell command below also does the same

$ source ~/.bash_profile

 Now let's install** ruby-build** from the source code 

$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

Now we will export the PATH in the ~/.bashprofile file in your home directory. As note, for the Zsh terminal use the ~/.zshrc file instead of the ~/.bashprofile file

$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile

And reload the current terminal

$ source ~/.bash_profile

Now let's install ruby

Let's install a given ruby version, to see all available ruby versions, run the following command 

$ rbenv install --list

Choose a version from the list, in my case I will be installing 2.2.3 

$ rbenv install -v 2.2.3
$ rbenv rehash  # run this after installing executables

After you've installed the given version, you can decide to make global which means that version will be the default version of Ruby all your shells will be using. Let's make it global.

$ rbenv global 2.2.3

You may in some cases use a given version for the given directory. Run the following shell command in that directory.

rbenv local 2.2.3  # You can also unset it from that directory (rbenv local --unset 2.2.3)

Some useful shell commands

# Set the RBENV_VERSION environment variable in your shell, this will 
# override the global version 
$ rbenv shell 2.2.3 

# same as above 
$ export RBENV_VERSION=2.2.3

# To unset that from the shell 
$ rbenv shell --unset

# List ruby versions installed 
$ rbenv versions 

# See all ruby available version 
$ rbenv install --list 

# The current ruby version 
$ rbenv version 

# The ruby executable path 
$ rbenv which irb 

Hope this was helpful

© 2020 Revolight AB