Arun Agrawal’s Blog

Ruby on Rails Developer

Bundler Usage – Manage Gemfile

In the previous post we posted about basic Bundler usage. Here i am posting about some advance way of managing Gemfile. Bundler requires a Gemfile. If you are creating a Rails application which is above to version 3, the Gemfile automatic gets created. Gemfile contains all your dependent gems.
gem "rails", "~> 3.0.5"

gem "mysql2"
The above Gemfile explains that we need Rails 3.0.5 and Mysql2 gems for our application. Gemfile can be manage in different ways. We can group things together which is only used in specific environment.
group :test do 
  gem "faker
end
group :development, :test do
  gem 'ruby-debug'
end
We can specify the gem version as a second argument in gem. If there is no version then it will fetch the latest one. Some time gem name is different and the library needs to be loaded may be different. Then we need to specify specifically the lib name.
gem 'sqlite3-ruby', :require => 'sqlite3'
Loading gem from a git repository directly If our library or gem is hosted somewhere else then we can add more source at the top of Gemfile. But if we want to fetch Gem from a git repository then we need to something like below. But that library must contain a .gemspec file to consider as a Gem.
gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git'
We can also specify a branch/tag/ref of git repository.
gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git',
                                   :branch => 'stable-2'
gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git', 
                                   :tag => 'tag-2'
gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git', 
                                   :ref => '23456'
We can load gems from our local path also.
gem 'nokogiri', :path => '~/code/nokogiri'