Ruby gems

A ruby gem is a software package that contains a reusable ruby application or library.

Common use cases

# search gems
$ gem search rails

# install gems
$ gem install rails
$ gem install rails -v 5.2.3

# list installed gems
$ gem list
# list specific gems
$ gem list json
# list remote specific gems
$ gem list -r json

# find where a specific gem is installed
$ gem which json

# uninstall gems
# NOTE: one known bad point:
# it does not uninstall the dependent gems which were installed together:
$ gem uninstall rails
$ gem uninstall rails -v 5.2.3

# view docs of installed gems
$ gem serve

# download gems and unpack downloaded gems locally
# good for read the source code of interested gems
$ gem fetch json
$ gem unpack json-2.3.0.gem
# if gem is already installed, it unpacks to current directory
# if gem is not yet installed, it fetches it first and unpacks
$ gem unpack i18n
$ gem unpack i18n -v 1.6.0

How to call specific version of a gem which has multiple versions installed

# confirm there are multiple versions installed
$ gem list ^rails$

*** LOCAL GEMS ***

rails (, 5.2.3, 5.1.7)

# calling a gem cli will always invoke the latest version by default
$ rails -v

# use _version_ syntax to invoke an older version
$ rails _5.1.7_ new MyProject

How to quickly generate the skeleton for the development of a gem

  • The recommended way is to use bundler
    $ bundle gem NAME