Installing Rails Gems (Not Updated)

Introducing Gems in Rails Projects

For most gems, you just want to add a line to your config/environment.rb if you want it to be available regardless of the environment the application is run or config/environments/{development,production,test,cucumber}.rb if you want it to work only in a specific environment.

## config/environment.rb do |config|
  config.gem 'haml',  :version => '3.0.0', :source => ''
  config.gem 'paperclip'

## config/environments/cucumber.rb
config.gem 'cucumber-rails',   :lib => false
config.gem 'database_cleaner', :lib => false
config.gem 'capybara',         :lib => false

If the gems you want to add aren't installed on your system, rails will look for them under vendor/gems. All rails need is to be able to locate the gems it needs either in your system or vendored in the project's vendor folder.

In order to install the gems listed in config/environment.rb you just need to run:

sudo rake gems:install RAILS_ENV=development

You may omit specifying the environment or change it to the environment you want the raketask to run on.

The command above will try to install the gems in your system. For most gems, there isn't any problem to have multiple versions installed you just need to make sure rails knows where to look for them.

Vendored Gems

You can vendor gems listed in your config/environment.rb by running the following command:

rake gems:unpack GEM=haml

It will store a copy of the gem's source code under vendor/gems. This is useful if you want to create a self-contained application. But always keep in mind that some gems depend on other gems. If you vendor a gem, but not a dependency, it will probably fail to start and you'll still be required to install the dependencies on the target machine.

So know your gems, investigate carefully before vendoring gems, and when including new ones in a project. No one likes to start working on a new rails application which doesn't have it's dependencies undocummented.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License