Ruby Gems

Installing a Specific Version

$ gem install rails -v

Updating RubyGems Itself

$ gem update --system

Version Constraints

  • >= - optimistic - assume all future versions work
  • ~> - pessimistic - allow only the last digit specified to change. '~> 2.2' is the same as ['>= 2.2.0', '< 3.0']
  • != - exclude a version


Creating a gem:

$ bundle gem my_gem_name

Referencing an unpublished gem locally

gem 'gemname', path: '/path/to/gem'

Installing a gem from git

gem 'rack', git: ''
gem 'rack', github: 'rack/rack'
gem 'rack', github: 'rack'

Referencing a git commit, tag, or branch of a gem

gem 'nokogiri', git: '', ref: '0bd839d'
gem 'nokogiri', git: '', tag: '2.0.1'
gem 'nokogiri', git: '', branch: 'rack-1.5'

Publishing a gem

$ gem build
$ gem push mygem-0.1.0.gem

Overriding a published gem with a local copy

all apps on system

$ bundle config local.mygem /path/to/mygem

one project

$ bundle config --local local.mygem /path/to/mygem
gem "tacokit", github: "rossta/tacokit", branch: "master"
$ bundle config --delete local.YOUR_GEM_NAME

Opening a gem’s source to view it

$ gem open -e vi [gemname]
# gem pristine [gemname]