How to Freeze Gems with Rails >= 2.1

December 23, 2008

I spent a couple of minutes trying to figure out how to “freeze” gems with a recent Rails app (2.2.2) I was developing. A bunch of git repositories had this vendor/gems folder a bit like in Merb, so I was pretty sure something had changed in Rails itself.

Turns out it’s pretty well integrated. First edit your environment.rb to specify which gem you want to register as a dependency:

Rails::Initializer.run do |config|
  # Specify gems that this application depends on.
  # They can then be installed with
  # "rake gems:install" on new installations.
  config.gem "fastercsv", :version => '1.4.0'
end

If you want to freeze the registered gems automatically under vendor/gems, just do:

rake gems:unpack

There is also the possibility to install them on any machine using this:

rake gems:install

Generally, I prefer to keep things in my repository, as I don’t want my deployment to fail because of network or routing issues.