What is a Gemfile?

A Gemfile is a file that is created to describe the gem dependencies required to run a Ruby program. A Gemfile should always be placed in the root of the project directory.

Setup

Global sources

At the top of your Gemfile, include a line for the RubyGems source that contains all the gems listed in the Gemfile by using the source method. In the vast majority of cases, the gem source will be, https://rubygems.org.

source 'https://rubygems.org'

# Gems go here 

Credentials

In some instances, a gem source will require a username and password. In these cases, use bundle config(1) to set the credentials.

$ bundle config https://gems.example.com/ user:password

Ruby information

If your program requires a specific Ruby version or engine, use the ruby method to specify your requirements. This method requires a version argument. Other optional arguments include:

  • :patchlevel – indicates Ruby’s patch level

  • :engine – indicates the Ruby engine

  • :engine_version – indicates the version of the engine being used. This argument cannot be used without the :engine argument.

ruby "1.9.3", :engine => 'jruby', :engine_version => "1.6.7", :patchlevel => 247

Gems

The most basic syntax for setting up a gem is,

gem “my_gem”

where my_gem is the name of the gem. The gem version is also commonly set. The seven operators you can use to set your gem’s version are:

  • = Equal To
  • != Not Equal To
  • >Greater Than
  • < Less Than
  • >= Greater Than Or Equal To
  • <= Less Than Or Equal To
  • ~> Pessimistically Greater Than or Equal To