Ruby installation

Create directory for ruby and rubygems sources under your home /home/username/env. Download ruby there from official site. Extract it in the same folder and move into it:

tar -xzvf ruby-2.2.2.tar.gz         
cd ./ruby-2.2.2/

Now run configure script with option which tells where to install it (to avoid permissions issues): -prefix=$Home/env then make and install:

./configure -prefix=$Home/env
make
makeinstall # or sudo make install for v.2.4.2

Almost done, the only thing left is to add /bin folder to the PATH. Do it in .profile or in .bash_profile or some other startup script:

export PATH=$HOME/env/bin/:$PATH

Please note that the bin folder should be before original PATH variable, just in case you have set another version of ruby in it.

To check the installation run ruby -v.

Rubygems installation

Download rubygems into the env folder from here and extract it.

tar -xzvf rubygems-2.4.8.tgz

Create gems folder under env (mkdir gems) and set GEM_HOME variable to point it in this folder:

export GEM_HOME=/home/username/env/gems/

Now go to extracted ruby folder and run setup script:

cd ./rubygems-2.4.8/
ruby setup.rb

To check the installation run gem env.