breaking into the unknown…

gemfile in rails

Leave a comment

Gemfile contain all the gems needed for your projects. You can track the available gems and new upcoming gems in rails community at and rubytoolbox.

when, you create a new Rails application it automatically create a Gemfile for you. For other appliaction, like Sinatra you need to run below command on your project

$ bundle init # It will create Gemfile and Gemfile.lock for you

NOTE : before you install any other gem you should must install bundler gem first. If you are using RVM, it is already installed for you in the global gemset i,e you do not need to do anything

There is some syntax for specifying the gems in the gemfile. The complete detail is available here. Iam listing the important one. So my sample Gemfile look like this.

source “http//” # this is the top line. It specify to bundler that gem in this file is to be installed from which repository. You can add multiple repository also , provided it is a valid gem repository. Some of the other repository are  “http//”, “http//”, infact you can add your personal repository also

source  “http//”# one more source added. avoid adding multiple source unless you are sure that a particular gem is not hosted on “http//” but on that specific repository because if you use multiple source the bundle install command will take more time to execute as the bundler will try to fetch detail from all the mentioned repository before moving to next line. The good thing is that  “http//”contain most of the gem and is enough.
gem "nokogiri" # it is the simplest form of adding a gem to gem file. You can pass various options like version,
require, git, path and group

gem “oauth”, “>= 1.4.2” # it specify that version should be greater or equal to 1.4.2
gem “RedCloth”, “>= 4.1.0”, “< 4.2.0” # version should be between 4.1.0 and 4.2.0
gem "sqlite3-ruby", :require => "sqlite3" # specify that sqlite3 is the main file, which should be used when autorequiring (Bundler.require). the default is the gem name itself i,e if you do not have passed the required function the main file will be sqlite3-ruby

gem “cancan”, :require => false # it prevent bundler from requiring the gem, but still install it and maintain dependencies.

gem ‘jqgrid_for_rails’, :git => "" # it specify that the gem should be installed from its git source rather then repository. I find it useful as for jqgrid some new changes are available on git repository of the gem but has not updated on while using git , you can also pass
other options branch, tag,ref, and submodules You MUST only specify at most one of these options. The default is :branch => “master”. Specify :submodules => true to cause bundler to expand any submodules included in the git repository

gem "my-admin", :path => "vendor/my-admin" # it will install the gem from the specified path. It would will be helpful for your own gem or any gem you have customised for yourself. Unlike :git, bundler does not compile C extensions for gems specified as paths.
gem "weakling", :platforms => :jruby # It specify that this gem should be use only in jruby platforms. There is a no of other platform also available like mri, :mri_18, rbx, mswin, mingw etc gem "ruby-debug", :platforms => [:mri_18, :jruby]

gem “rspec”, :group => :test # will be loaded only in test environment
gem “wirble”, :groups => [:development, :test] # will be loaded in development and test environment


1->The :git, :path, :group, and :platforms options may be applied to a 
group of gems by using block form.

git "git://" do
  gem "activesupport"
  gem "actionpack"

platforms :ruby do
  gem "ruby-debug"
  gem "sqlite3-ruby"

group :development do
  gem "wirble"
  gem "faker"

group :development, :test do
  gem "rspec-rails", ">= 2.11.0"
  gem "factory_girl_rails", ">= 3.5.0"
  gem "debugger"

2->placing the gems in group do not prohibit there installation i,e irrespective 
of group in which they are placed all gem and its dependency get installed with bundle 
install command. If you want to skip gem in a particular group use bundler command 
line interface (CLI) --without option

bundle install --without test 
bundle install --without development test 

Similarly, bundler provides two run time methods : Bundler.setup and Bundler.require, 
to limit their impact to particular groups.

# setup adds gems to Ruby's load path
Bundler.setup                    # defaults to all groups
require "bundler/setup"          # same as Bundler.setup
Bundler.setup(:default)          # only set up the _default_ group
Bundler.setup(:test)             # only set up the _test_ group (but `not` _default_)
Bundler.setup(:default, :test)   # set up the _default_ and _test_ groups, but no others

#require requires all of the gems in the specified groups
Bundler.require                  # defaults to just the _default_ group
Bundler.require(:default)        # identical
Bundler.require(:default, :test) # requires the _default_ and _test_ groups
Bundler.require(:test)           # requires just the _test_group

3 -> while deploying in production, better run bundle install - -deployment instead of 
     bundle install. see the detail here

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s