Ruby Installation - Linux
Below are the steps to install Ruby on a Linux machine.
Note: Ensure you have root permissions before installation.
Source Code Installation
Download the latest version of the Ruby compressed file. Click here to download.
After downloading Ruby, extract it to a newly created directory:
$ tar -xvzf ruby-2.2.3.tgz $ cd ruby-2.2.3
Now, configure and compile the source code as follows:
$ ./configure $ make $ sudo make install
After installation, ensure everything is working correctly by entering the following command in the command line:
$ ruby -v ruby 2.2.3……
If everything is working correctly, it will output the version of the Ruby interpreter installed, as shown above. If you have installed a different version, it will display that version instead.
Automatic Installation of Ruby
If your computer is connected to the Internet, the easiest way to install Ruby is using yum or apt-get. Enter the following commands at the command prompt to install Ruby on your computer:
$ sudo yum install ruby # CentOS, Fedora, or RHEL systems
or
$ sudo apt-get install ruby-full # Debian or Ubuntu systems
If you are using macOS, you can install Ruby using the brew command:
$ brew install ruby
Installing Ruby with RVM
RVM allows you to install and manage multiple Ruby versions on your system. It also manages different gemsets. It supports OS X, Linux, and other UNIX-like operating systems.
Installing RVM
$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ curl -sSL https://get.rvm.io | bash -s stable
After installation, you will see some installation information, including a line to note:
...
To start using RVM you need to run `source /etc/profile.d/rvm.sh`
....
This means you need to execute a source command to reload the initialization file. Follow the installation prompt and run the following command to load the RVM environment (this is not necessary for new terminals, as they will automatically reload):
$ source /etc/profile.d/rvm.sh
Check if RVM is installed correctly:
$ rvm -v
rvm 1.22.17 (stable) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
Installing Ruby Environment with RVM
List known Ruby versions:
$ rvm list known
You can choose an existing RVM version to install (example using RVM 2.4.2):
$ rvm install 2.4.2
Wait for the download and compilation process to complete. Afterward, Ruby and Ruby Gems will be installed.
Common RVM Commands
Query installed Ruby versions:
$ rvm list
Uninstall an installed version:
$ rvm remove 1.9.2
Setting the Ruby Version
After installing RVM, set the specified Ruby version as the system default:
$ rvm 2.0.0 --default
You can also use other version numbers, provided you have installed them with rvm install
.
Test if the setup is correct:
$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]
$ gem -v
2.1.6
This might be due to Ruby's default source using cocoapods.org, which can sometimes have issues in China. A common solution is to replace the source with ruby-china's. Replace it as follows:
$ gem source -r https://rubygems.org/
$ gem source -a https://gems.ruby-china.com/
To verify if the replacement was successful, execute:
$ gem sources -l
The expected output:
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
Ensure only gems.ruby-china.com is listed.
$ gem install rails
If you use Gemfile and Bundler (e.g., Rails projects)
You can use Bundler's gem source mirror command:
$ bundle config mirror.https://rubygems.org https://gems.ruby-china.com
This way, you don't need to change your Gemfile's source:
source 'https://rubygems.org/'
gem 'rails', '4.1.0'
...