Using RubyGems for managing ruby packages on Linux
Ruby programming language has gained a lot of popularity in the last two decades since it was created in 1995 by Yukihiro Matsumoto. Ruby is the foundation for the popular Ruby on Rails web development framework. The increase in ruby’s popularity and features led to it becoming more modular in nature with the availability of many ruby gems or packages to add more features to an already installed version of ruby. In this article, we will explain how to install and using RubyGems – a sophisticated package manager for Ruby. Managing ruby Packages
Install RubyGems – Managing ruby packages
Note: that the Ruby programming language is not installed on Linux distributions by default.
When you install RubyGems, it will install ruby as well as a required per-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 RubyGems to install them as shown below
root@sahil-ubuntu:~# apt install rubygems
Install RubyGems on Centos
The Ruby programming language 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@sahil-centos ~]# yum install rubygems 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 rubygem-io-console 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!
After installing RubyGems on our system, we will now cover some examples to understand it’s usage.
I’ll be using a Centos 7 system for the demonstrations ahead.
Example 1: Check ruby and Using rubygems versions
[root@sahil-centos ~]# ruby --version ruby 2.0.0p648 (2015-12-16) [x86_64-linux] [root@sahil-centos ~]# [root@sahil-centos ~]# gem --version 2.0.14.1
Example 2: Verify that gem environment is set up correctly
To verify that the environment for RubyGems is set up correctly, we use the gem env command as shown below.
[root@sahil-centos ~]# gem env RubyGems Environment: - RUBYGEMS VERSION: 2.0.14.1 - RUBY VERSION: 2.0.0 (2015-12-16 patchlevel 648) [x86_64-linux] - INSTALLATION DIRECTORY: /usr/local/share/gems - RUBY EXECUTABLE: /usr/bin/ruby - EXECUTABLE DIRECTORY: /usr/local/bin - RUBYGEMS PLATFORMS: - ruby - x86_64-linux - GEM PATHS: - /usr/local/share/gems - /root/.gem/ruby - /usr/share/gems - GEM CONFIGURATION: - :update_sources => true - :verbose => true - :backtrace => false - :bulk_threshold => 1000 - REMOTE SOURCES: - https://rubygems.org/
Example 3: List currently installed ruby packages
To list currently installed ruby gems or packages we use the following command
[root@sahil-centos ~]# gem list --local *** LOCAL GEMS *** bigdecimal (1.2.0) io-console (0.4.2) json (1.7.7) psych (2.0.0) rdoc (4.0.0)
Example 4: Search for ruby packages
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 fog, which is the Ruby cloud services library.
[root@sahil-centos ~]# gem search fog *** REMOTE GEMS *** chef-metal-fog (0.9) chef-provisioning-fog (0.26.0) crypt-fog (1.0.2) csv-to-fog (1.0.0) db2fog (0.9.0) deb-fog (0.1.0) defog (0.9.5) dragonfly-fog_data_store (0.0.4) errbit_fogbugz_plugin (0.1.0) ext_fog_aws (0.1.1) fog (1.42.0) fog-akamai (0.5.2) -------output truncated for brevity
Example 5: Install a gem
We use the gem install command to install a ruby gem.
Let’s install the git-fog gem which came up in the search results in the previous example.
[root@sahil-centos ~]# gem install git-fog Fetching: git-fog-2.0.0.gem (100%) Successfully installed git-fog-2.0.0 Parsing documentation for git-fog-2.0.0 Installing ri documentation for git-fog-2.0.0 1 gem installed
Example 6: Uninstall a ruby gem
To remove a ruby gem, we use the gem uninstall command followed by the name of the gem.
To uninstall the git-fog gem we installed earlier, use the following command:
[root@sahil-centos ~]# gem uninstall git-fog Successfully uninstalled git-fog-2.0.0
Example 7: Update installed ruby gems
We use the gem update command to update all installed ruby gems on the system.
Given below is an example:
[root@sahil-centos ~]# gem update Updating installed gems Updating bigdecimal Fetching: bigdecimal-1.3.2.gem (100%) Building native extensions. This could take a while... -----------output truncated for breivity Fetching: rdoc-6.0.0.gem (100%) ERROR: Error installing rdoc: rdoc requires Ruby version >= 2.2.2. Nothing to update
Most of the updates for the gems failed because my system does not have the latest version of ruby installed.
We need to first update it and then we may try again.
Example 8: View ruby gem contents
To view files installed when we install a ruby gem, use the gem contents command.
Here is an example:
[root@sahil-centos ~]# gem contents io-console /usr/share/gems/gems/io-console-0.4.2/lib/io/console/size.rb
Conclusion
This concludes our discussion on using RubyGems package manager to managing ruby packages. Please do share your suggestions if you would like to see more content in the Ruby scripting language.
Related Articles
Thanks for your wonderful Support and Encouragement