Ruby Installation - Linux
The steps to install Ruby on a Linux machine are listed below.
**Note:** Before installation, make sure you have root privileges.
Installing from source
- Download the latest Ruby archive file.Please click here to download。
- After downloading Ruby, extract it into 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 properly by entering the following command at the command line:
$ruby -v
ruby 2.2.3……
- If everything works properly, the version of the installed Ruby interpreter will be output as shown above. If you installed a different version, a different version will be displayed.
Automatically Installing Ruby
If your computer is connected to the Internet, the easiest way to install Ruby is to use yum or apt-get. Enter the following command at the command prompt to install Ruby on your computer.
$ sudo yum install ruby # CentOS, Fedora, 或 RHEL 系统
或
sudo apt-get install ruby-full # Debian 或 Ubuntu 系统
If you are on an Apple system, you can use brew command to install:
$ brew install ruby
Installing Ruby Using RVM
RVM can install and manage multiple Ruby versions on a system. It can also manage different gem sets. 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, some installation information will be listed, and one line requires attention:
...
To start using RVM you need to run `source /etc/profile.d/rvm.sh`
....
This means that to start using it you need to execute a source command to re-run the just-modified initialization file. Following the installation prompt, execute the following command to load the RVM environment (no need to do this in a newly opened Terminal, as it will reload automatically)
source /etc/profile.d/rvm.sh
Check that it is installed correctly
$ rvm -v
rvm 1.22.17 (stable) by Wayne E. Seguin , Michal Papis [https://rvm.io/]
Install a Ruby environment with RVM
List known ruby versions:
$ rvm list known
You can choose an existing rvm version to install (the following takes installing rvm 2.4.2 as an example)
$ rvm install 2.4.2
Also continue to wait for the long download and compilation process; once finished, Ruby and Ruby Gems are installed.
Common rvm commands
Query the installed ruby
$ rvm list
Uninstall an installed version
$ rvm remove 1.9.2
Set the Ruby version
After RVM is installed, you need to execute the following command to set the specified Ruby version as the system default
$ rvm 2.0.0 --default
Likewise, you can use other version numbers, provided you have installed that version with rvm install
At this point you can test whether it is correct
$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]
$ gem -v
2.1.6
This may be because Ruby's default source uses cocoapods.org, and accessing this site from within the country can sometimes be problematic. One online solution is to replace the source with ruby-china's, as follows:
$ gem source -r https://rubygems.org/
$ gem source -a https://gems.ruby-china.com/
To verify that the replacement was successful, you can execute:
$ gem sources -l
Normal output:
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
Please make sure only gems.ruby-china.com
$ gem install rails
If you use Gemfile and Bundle (for example: a Rails project)
You can use Bundler's Gem source mirroring 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'
...