Q: How we can maintain the multiple versions of Ruby on the same system?

The best solution is RVM.

First, you need to install it on the operating system.

I am using CentOS version 7.

website: https://www.rvm.io 

1. gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
2. \curl -sSL https://get.rvm.io | bash -s stable

you would require to install curl before using the second command.

yum install curl

List of command after installing curl:

1. check the rvm version:

rvm -v

2. list of different version of Ruby installed 

rvm list

3.  Install a version of Ruby on your system:

For ruby version 2.2.1 use:

rvm install 2.2.1

For ruby version 1.9.3

rvm install 1.9.3

Now, to install a particular ruby version along with its patch:

suppose you want to install the ruby 1.9.7 with patch 327 command will be:

rvm install 1.9.3-p327

For ruby 2.2.1 patch xyz command is as:

rvm install 2.2.1 -pxyz

4. Change the version of Ruby you want to use.

rvm use 2.2.1

rvm use 1.9.3

5. Gemset:

you can maintain different versions of Rails for each ruby by using Gemset.

(i) create a new gemset:

rvm create gemset rails2

(ii) then use that gemset:

rvm use rails2

(iii) Install rails in it

rvm install rails -v 5.0.1


Q1 : How to list all gemset’s created for Ruby -v 2.2.1

(i) rvm use 2.2.1

(ii) rvm gemset list

Q2 : How to change the ruby and rails version in one command?

suppose, you to installed Ruby -v 2.2.2 and Rails -v 5.0.1 in gemset rails5 for ruby 2.2.2.

(i) rvm use 2.2.2@rails5

note: rails5 should be the gemset created for ruby 2.2.2

Q3: How to change the version of ruby and rails when I enter the root directory of the project?

There are 2 methods:

(i) For this, you will need run a command from the root directory of the project:

rvm use –rails-version 2.2.2@rails5

This command will create 2 files in your project .ruby-version and .rails-version with ruby and rails version respectively.

(ii) create .rvmsh file in the root directory of the project.

cat > rvm use 2.2.2@rails5

Cntrl-c to exit.

Now, when every you cd into the project it will change the version of the Ruby & Rails.