Ruby Language Gem Usage Using a Gemfile and Bundler


A Gemfile is the standard way to organize dependencies in your application. A basic Gemfile will look like this:

source ''

gem 'rack'
gem 'sinatra'
gem 'uglifier'

You can specify the versions of the gem you want as follows:

# Match except on point release. Use only 1.5.X
gem 'rack', '~>1.5.2'
# Use a specific version.
gem 'sinatra', '1.4.7'
# Use at least a version or anything greater.
gem 'uglifier', '>= 1.3.0'

You can also pull gems straight from a git repo:

# pull a gem from github
gem 'sinatra', git: ''
# you can specify a sha
gem 'sinatra', git: '', sha: '30d4fb468fd1d6373f82127d845b153f17b54c51'
# you can also specify a branch, though this is often unsafe
gem 'sinatra', git: '', branch: 'master'

You can also group gems depending on what they are used for. For example:

group :development, :test do
    # This gem is only available in dev and test, not production.
    gem 'byebug'

You can specify which platform certain gems should run on if you application needs to be able to run on multiple platforms. For example:

platform :jruby do
  gem 'activerecord-jdbc-adapter'
  gem 'jdbc-postgres'

platform :ruby do
  gem 'pg'

To install all the gems from a Gemfile do:

gem install bundler
bundle install