Libraries

As most programming languages, Ruby leverages a wide set of third-party libraries.

Most of them are released in the form of a gem. RubyGems is a Ruby packaging system designed to facilitate the creation, sharing and installation of libraries (in some ways, it is a distribution packaging system similar to, say, apt-get, but targeted at Ruby software). Since version 1.9 Ruby comes with RubyGems by default, while previous Ruby versions require to install it by hand.

Some other libraries are released as archived (.zip or .tar.gz) directories of source code. Installation processes may vary, typically a README or INSTALL file is available with instructions.

Let’s take a look at finding libraries and installing them for your own use.

Finding libraries

The main place where libraries are hosted is RubyGems.org, providing Ruby libs as gems. You may browse the website directly, or use the gem command.

Using gem search -r, you can inspect RubyGems’ repository. For instance, gem search -r rails will return a list of Rails-related gems. With the --local (-l) option, you would perform a local search through your installed gems. To install a gem, use gem install [gem]. Browsing installed gems is done with gem list. For more information about the gem command, see below or head to RubyGems’ docs.

There are other sources of libraries though. RubyForge used to be a popular home for Ruby libraries, but last years saw the rise of GitHub as one of the main ruby-related content repository. Most often a gem source code will be hosted on GitHub while being published as a fully-fledged gem to RubyGems.org.

The Ruby Toolbox is a project that makes it easy to explore open source Ruby projects. It has categories for various common development tasks, collects a lot of information about the projects like release and commit activity or dependencies, and rates projects based on their popularity on RubyGems.org and GitHub. The search makes it easy to find what you are looking for.

A few more words about RubyGems

Here is a quick review of the gem command for your daily use. More detailed documentation is available, covering all aspects of this packaging system.

Searching among available gems

The search command can be used to look for gems, based on a string. Gems which names start with the specified string will be listed in return. For instance, to search for the “html”-related gems:

$ gem search -r html

*** REMOTE GEMS ***

html-sample (1.0, 1.1)

The --remote / -r flag indicates that we want to inspect the official RubyGems.org repository (default behaviour). With the --local / -l flag you would perform a local search among your installed gems.

Installing a gem

Once you know which gem you would like to install, for instance the popular Rails:

$ gem install rails

You can even install just a certain version of the library, using the --version / -v flag:

$ gem install rails --version 3.0

Listing all gems

For a list of all locally installed gems:

$ gem list

To obtain a (very long) list of all gems available on RubyGems.org:

$ gem list -r

Help!

Documentation is available inside your terminal:

$ gem help

For instance, gem help commands is very useful as it outputs a list of all gem’s commands.

Crafting your own gems

RubyGems.org has several guides about this topic. You may also want to investigate on Bundler, a generic tool which helps you manage an application’s dependencies and may be used along RubyGems.