Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. Yukihiro “Matz” Matsumoto created it in the mid-1990s, using his influence from other prpgramming languages i.e. Perl, Ada, Lips, Eiffel, and Smalltalk. Ruby was released in 1995. Like Python (released few years earlier), ruby also has dynamic typing and implicit memory management
Install Ruby using yum
yum install ruby
Issue: The installed version may not be the latest version, as yum will install the latest version of Ruby available at the release time of the specific distribution version.
output:
[nahmed@localhost ~]$ ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
Installing Latest Ruby version
The common way to install ruby version of choice (latest) is using the rbenv, the ruby management utility.
Step 1: OS level dependencies for Ruby and rbenv
sudo yum install -y git-core ruby-devel zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make
Step 2: Install rbenv
rbenv is a pyenv equivalent for Ruby – for creating virtual environments, installing and managing multiple Ruby versions.
If you’re on macOS, we recommend installing rbenv with Homebrew.
$ brew install rbenv
Note: that this also installs ruby-build, so you’ll be ready to install other Ruby versions out of the box.
Direct Git Checkout
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Add binary to PATH
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ exec $SHELL
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ exec $SHELL
Output:
[nahmed@localhost ~]$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Cloning into '/home/nahmed/.rbenv'...
remote: Counting objects: 2625, done.
remote: Total 2625 (delta 0), reused 1 (delta 0), pack-reused 2624
Receiving objects: 100% (2625/2625), 485.78 KiB | 87.00 KiB/s, done.
Resolving deltas: 100% (1644/1644), done.
[nahmed@localhost .rbenv]$ cd ~/.rbenv && src/configure && make -C src
make: Entering directory `/home/nahmed/.rbenv/src'
gcc -fPIC -c -o realpath.o realpath.c
gcc -shared -Wl,-soname,../libexec/rbenv-realpath.dylib -o ../libexec/rbenv-realpath.dylib realpath.o
make: Leaving directory `/home/nahmed/.rbenv/src'
[nahmed@localhost .rbenv]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
[nahmed@localhost .rbenv]$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
[nahmed@localhost .rbenv]$ exec $SHELL
Cloning into '/home/nahmed/.rbenv'...
remote: Counting objects: 2625, done.
remote: Total 2625 (delta 0), reused 1 (delta 0), pack-reused 2624
Receiving objects: 100% (2625/2625), 485.78 KiB | 87.00 KiB/s, done.
Resolving deltas: 100% (1644/1644), done.
[nahmed@localhost .rbenv]$ cd ~/.rbenv && src/configure && make -C src
make: Entering directory `/home/nahmed/.rbenv/src'
gcc -fPIC -c -o realpath.o realpath.c
gcc -shared -Wl,-soname,../libexec/rbenv-realpath.dylib -o ../libexec/rbenv-realpath.dylib realpath.o
make: Leaving directory `/home/nahmed/.rbenv/src'
[nahmed@localhost .rbenv]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
[nahmed@localhost .rbenv]$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
[nahmed@localhost .rbenv]$ exec $SHELL
Step 3: Install ruby-build
It is a requirement for using the rbenv subcommand ‘install’ – for installing multiple Ruby versions of choice.
Git checkout
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Adding to PATH
$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
$ exec $SHELL
$ source ~/.bash_profile
$ exec $SHELL
Verification
rbenv install --list
Lists all the available and locally installed Ruby versions.
Output:
[nahmed@localhost .rbenv]$ rbenv install --list
Available versions:
1.8.5-p52
1.8.6
..
1.9.0-5
...
2.0.0-dev
.....
2.1.10
2.2.0-dev
2.2.0
2.2.1
......
2.2.6
2.3.0
2.3.1
2.3.2
2.4.0-dev
.....
2.4.0-rc1
2.4.0
2.4.1 (current stable release - https://www.ruby-lang.org/en/downloads/)
2.5.0-dev
jruby-1.5.6
Available versions:
1.8.5-p52
1.8.6
..
1.9.0-5
...
2.0.0-dev
.....
2.1.10
2.2.0-dev
2.2.0
2.2.1
......
2.2.6
2.3.0
2.3.1
2.3.2
2.4.0-dev
.....
2.4.0-rc1
2.4.0
2.4.1 (current stable release - https://www.ruby-lang.org/en/downloads/)
2.5.0-dev
jruby-1.5.6
Step 4: Install Ruby 2.4.1
$ rbenv install 2.4.1
Output
[nahmed@localhost .rbenv]$ rbenv install 2.4.1
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
Installing ruby-2.4.1...
Installed ruby-2.4.1 to /home/nahmed/.rbenv/versions/2.4.1[nahmed@localhost .rbenv]$ rbenv versions
* system (set by /home/nahmed/.rbenv/version)
2.4.1
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
Installing ruby-2.4.1...
Installed ruby-2.4.1 to /home/nahmed/.rbenv/versions/2.4.1[nahmed@localhost .rbenv]$ rbenv versions
* system (set by /home/nahmed/.rbenv/version)
2.4.1
Setting Ruby 2.4.1 as global
The global sub-command sets the default version of Ruby that all of your shells will use. If you want to install and use a different version, simply run the rbenv commands with a different version number.
$ rbenv global 2.4.1
Output
[nahmed@localhost .rbenv]$ rbenv global 2.4.1
[nahmed@localhost .rbenv]$ rbenv versions
system
* 2.4.1 (set by /home/nahmed/.rbenv/version)
[nahmed@localhost .rbenv]$ rbenv versions
system
* 2.4.1 (set by /home/nahmed/.rbenv/version)
NOTE: check if all the path are set, restart the terminal, or reload the bash_profile / bashrc .
[nahmed@localhost ~]$ source .bash_profile
[nahmed@localhost ~]$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[nahmed@localhost ~]$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
gem the package manager
For installing any Ruby package it provides a manager utility gem (Python pip equivalent).
$ gem install kickscraper
irb – interactive Ruby shell