Introduction

Ruby has become a popular and powerful programming language over the years since its inception in 1995.
The Ruby programming language is the foundation for the popular web development framework Ruby on Rails.
Increased popularity implied increased user base and increased functionality. So, ruby packages or gems were introduced to add additional features to the installed ruby programming language. In this article, we will explain how to install and use RubyGems – a sophisticated package manager for Ruby.

 

Install RubyGems

Ruby does not come installed on Linux systems out of the box.
When you install RubyGems, it will install ruby as well as a required pre-requisite.

Install rubygems on ubuntu:
The ruby language packages, as well as the rubygems package, are available in the online repositories so you may type apt install rubgems to install them as shown below:

root@linuxnix:~# apt install rubygems
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'ruby' instead of 'rubygems'
The following additional packages will be installed:
libruby2.3 rake ruby2.3
Suggested packages:
ri ruby-dev
The following NEW packages will be installed:
libruby2.3 rake ruby ruby2.3
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,053 kB of archives.
After this operation, 14.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libruby2.3 amd64 2.3.1-2~16.04.2 [2,958 kB]
Get:2 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 ruby2.3 amd64 2.3.1-2~16.04.2 [41.0 kB]
Get:3 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 ruby all 1:2.3.0+1 [5,530 B]
Get:4 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 rake all 10.5.0-2 [48.2 kB]
Fetched 3,053 kB in 0s (49.9 MB/s)
Selecting previously unselected package libruby2.3:amd64.
(Reading database ... 108520 files and directories currently installed.)
Preparing to unpack .../libruby2.3_2.3.1-2~16.04.2_amd64.deb ...
Unpacking libruby2.3:amd64 (2.3.1-2~16.04.2) ...
Selecting previously unselected package ruby2.3.
Preparing to unpack .../ruby2.3_2.3.1-2~16.04.2_amd64.deb ...
Unpacking ruby2.3 (2.3.1-2~16.04.2) ...
Selecting previously unselected package ruby.
Preparing to unpack .../ruby_1%3a2.3.0+1_all.deb ...
Unpacking ruby (1:2.3.0+1) ...
Selecting previously unselected package rake.
Preparing to unpack .../archives/rake_10.5.0-2_all.deb ...
Unpacking rake (10.5.0-2) ...
Processing triggers for libc-bin (2.23-0ubuntu9) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libruby2.3:amd64 (2.3.1-2~16.04.2) ...
Setting up ruby2.3 (2.3.1-2~16.04.2) ...
Setting up ruby (1:2.3.0+1) ...
Setting up rake (10.5.0-2) ...
Processing triggers for libc-bin (2.23-0ubuntu9) ...
root@linuxnix:~#

 

Install rubygems on Centos:
Ruby is not installed on Centos by default but is available in the base repository.
To install ruby and rubygems in Centos, type yum install rubygems as shown below:

[root@linuxnix~]# yum install rubygems
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 7.0 kB 00:00:00
extras | 3.4 kB 00:00:00
puppetlabs-pc1 | 2.5 kB 00:00:00
updates | 3.4 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: centos.mirror.net.in
* epel: epel.mirror.angkasa.id
* extras: mirror.digistar.vn
* updates: mirrors.123host.vn
Resolving Dependencies
--> Running transaction check
---> Package rubygems.noarch 0:2.0.14.1-30.el7 will be installed
--> Processing Dependency: rubygem(rdoc) >= 4.0.0 for package: rubygems-2.0.14.1-30.el7.noarch
--> Processing Dependency: rubygem(psych) >= 2.0.0 for package: rubygems-2.0.14.1-30.el7.noarch
--> Processing Dependency: rubygem(io-console) >= 0.4.2 for package: rubygems-2.0.14.1-30.el7.noarch
--> Processing Dependency: ruby(release) for package: rubygems-2.0.14.1-30.el7.noarch
--> Processing Dependency: /usr/bin/ruby for package: rubygems-2.0.14.1-30.el7.noarch
--> Running transaction check
---> Package ruby.x86_64 0:2.0.0.648-30.el7 will be installed
--> Processing Dependency: rubygem(bigdecimal) >= 1.2.0 for package: ruby-2.0.0.648-30.el7.x86_64
---> Package ruby-libs.x86_64 0:2.0.0.648-30.el7 will be installed
---> Package rubygem-io-console.x86_64 0:0.4.2-30.el7 will be installed
---> Package rubygem-psych.x86_64 0:2.0.0-30.el7 will be installed
---> Package rubygem-rdoc.noarch 0:4.0.0-30.el7 will be installed
--> Processing Dependency: ruby(irb) = 2.0.0.648 for package: rubygem-rdoc-4.0.0-30.el7.noarch
--> Processing Dependency: rubygem(json) >= 1.7.7 for package: rubygem-rdoc-4.0.0-30.el7.noarch
--> Running transaction check
---> Package ruby-irb.noarch 0:2.0.0.648-30.el7 will be installed
---> Package rubygem-bigdecimal.x86_64 0:1.2.0-30.el7 will be installed
---> Package rubygem-json.x86_64 0:1.7.7-30.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
rubygems noarch 2.0.14.1-30.el7 base 216 k
Installing for dependencies:
ruby x86_64 2.0.0.648-30.el7 base 69 k
ruby-irb noarch 2.0.0.648-30.el7 base 90 k
ruby-libs x86_64 2.0.0.648-30.el7 base 2.8 M
rubygem-bigdecimal x86_64 1.2.0-30.el7 base 81 k
rubygem-io-console x86_64 0.4.2-30.el7 base 52 k
rubygem-json x86_64 1.7.7-30.el7 base 77 k
rubygem-psych x86_64 2.0.0-30.el7 base 79 k
rubygem-rdoc noarch 4.0.0-30.el7 base 319 k
Transaction Summary
================================================================================
Install 1 Package (+8 Dependent packages)
--------------------------------------------------------output truncated for brevity
Installed:
rubygems.noarch 0:2.0.14.1-30.el7
Dependency Installed:
ruby.x86_64 0:2.0.0.648-30.el7 ruby-irb.noarch 0:2.0.0.648-30.el7 ruby-libs.x86_64 0:2.0.0.648-30.el7
rubygem-bigdecimal.x86_64 0:1.2.0-30.el7 rubygem-io-console.x86_64 0:0.4.2-30.el7 rubygem-json.x86_64 0:1.7.7-30.el7
rubygem-psych.x86_64 0:2.0.0-30.el7 rubygem-rdoc.noarch 0:4.0.0-30.el7
Complete!
[root@linuxnix~]#

Now that we have seen how to install rubygems, we’ll go through some usage examples.
I’ll be using an Ubuntu 16.04 system for the demonstrations ahead.

 

Example 1: Verify that ruby and rubygems have been installed

To check the version of ruby and rubygems on the system, we use the ruby –version and gem –version commands respectively.

root@linuxnix:~# ruby --version
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
root@linuxnix:~# gem --version
2.5.1

 

Example 2: Check that gem environment is set up
To verify that the environment for rubygems is set up correctly, we use the gem env command as shown below.

root@linuxnix:~# gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.5.1
- RUBY VERSION: 2.3.1 (2016-04-26 patchlevel 112) [x86_64-linux-gnu]
- INSTALLATION DIRECTORY: /var/lib/gems/2.3.0
- USER INSTALLATION DIRECTORY: /root/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /usr/bin/ruby2.3
- EXECUTABLE DIRECTORY: /usr/local/bin
- SPEC CACHE DIRECTORY: /root/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /var/lib/gems/2.3.0
- /root/.gem/ruby/2.3.0
- /usr/lib/x86_64-linux-gnu/rubygems-integration/2.3.0
- /usr/share/rubygems-integration/2.3.0
- /usr/share/rubygems-integration/all
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
- /sbin
- /bin
- /snap/bin

Example 3: List installed gems
To list currently installed ruby gems or packages we use the following command

root@linuxnix:~# gem list --local

*** LOCAL GEMS ***

bigdecimal (1.2.8)
did_you_mean (1.0.0)
io-console (0.4.5)
json (1.8.3)
minitest (5.8.4)
net-telnet (0.1.1)
power_assert (0.2.7)
psych (2.0.17)
rake (10.5.0)
rdoc (4.2.1)
test-unit (3.1.7)
root@linuxnix:~#

Example 4: Search for gems
To look for ruby gems we use the gem search command followed by the named of the package we wish to search for.
Let’s demonstrate by searching for Huginn, a system for building agents that perform automated tasks for you online

root@linuxnix:~# gem search huginn

*** REMOTE GEMS ***

huginn_agent (0.6.1)
huginn_github_notifications_agent (0.0.1)
huginn_naive_bayes_agent (0.1.4)
huginn_s3_file_agent (0.1.1)
huginn_slackbot_agent (0.1)
huginn_todoist_agent (0.5.0)
huginn_transmission_agent (0.1)
huginn_xero_agent (0.1.1)

Example 5: Install a ruby gem
We use the gem install command to install a ruby gem.
Let’s install the huginn gem we searched for earlier.

root@linuxnix:~# gem install huginn_agent
Fetching: thor-0.20.0.gem (100%)
Successfully installed thor-0.20.0
Fetching: huginn_agent-0.6.1.gem (100%)
Successfully installed huginn_agent-0.6.1
Parsing documentation for thor-0.20.0
Installing ri documentation for thor-0.20.0
Parsing documentation for huginn_agent-0.6.1
Installing ri documentation for huginn_agent-0.6.1
Done installing documentation for thor, huginn_agent after 1 seconds
2 gems installed

Example 6: Remove a gem
We use the gem uninstall command to remove a ruby gem.
Let’s uninstall the huginn gem we searched for earlier.

root@linuxnix:~# gem uninstall huginn_agent
Remove executables:
huginn_agent
in addition to the gem? [Yn] y
Removing huginn_agent
Successfully uninstalled huginn_agent-0.6.1
root@linuxnix:~#

Example 7: Update ruby gems
To update all currently installed ruby gems on the system, use the gem updated command as shown below

root@linuxnix:~# gem update
Updating installed gems
---------output truncated for brevity
Done installing documentation for test-unit after 1 seconds
Gems updated: bigdecimal did_you_mean io-console json minitest power_assert psych rake rdoc test-unit
root@linuxnix:~#

 

Example 8: List files that make up a ruby gem
To view files installed when we install a ruby gem, use the gem contents command.
Here is an example:

root@linuxnix:~# gem contents json
/usr/lib/ruby/2.3.0/json.rb
/usr/lib/ruby/2.3.0/json/add/bigdecimal.rb
/usr/lib/ruby/2.3.0/json/add/complex.rb
/usr/lib/ruby/2.3.0/json/add/core.rb
/usr/lib/ruby/2.3.0/json/add/date.rb
/usr/lib/ruby/2.3.0/json/add/date_time.rb
/usr/lib/ruby/2.3.0/json/add/exception.rb
/usr/lib/ruby/2.3.0/json/add/ostruct.rb
/usr/lib/ruby/2.3.0/json/add/range.rb
/usr/lib/ruby/2.3.0/json/add/rational.rb
/usr/lib/ruby/2.3.0/json/add/regexp.rb
/usr/lib/ruby/2.3.0/json/add/struct.rb
/usr/lib/ruby/2.3.0/json/add/symbol.rb
/usr/lib/ruby/2.3.0/json/add/time.rb
/usr/lib/ruby/2.3.0/json/common.rb
/usr/lib/ruby/2.3.0/json/ext.rb
/usr/lib/ruby/2.3.0/json/generic_object.rb
/usr/lib/ruby/2.3.0/json/version.rb
/usr/lib/x86_64-linux-gnu/ruby/2.3.0/json/ext/generator.so
/usr/lib/x86_64-linux-gnu/ruby/2.3.0/json/ext/parser.so

Conclusion

This concludes our demonstration of using the rubygems tool for interacting with Ruby packages in Linux. We will try to post more articles on ruby in the future.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.