Thought I’d share this because I had a lot of trouble figuring out how and where to run the “rake gems:install” when deploying with Capistrano. I wanted it to automatically deploy the gems when I call “cap deploy:migrations” – this is done by installing the gems right after update_code, like this:
# config/deploy.rb
after "deploy:update_code", "gems:install"
namespace :gems do
desc "Install gems"
task :install, :roles => :app do
run "cd #{current_release} && #{sudo} rake gems:install"
end
end
Some of the examples I’ve seen use “cd #{current_path}” but I don’t think this would work as the “current” symlink hasn’t been updated at the time the “update_code” task is run. That’s why I use “current_release”.
Also others use “rake RAILS_ENV=production gems:install” but I can’t see why you should have to use RAILS_ENV here. If I’m wrong, please correct me.
Some of the links I found when trying to get this to work:
- Capistrano recipes: deploy
- cap gems:install and a Gem dependency gotcha
- Install required gems remotely with capistrano
- lib/capistrano/recipes/deploy.rb
Andre indlæg:
Tags: Capistrano, Gems, Rake, Ruby on Rails

Hey mate, thanks for posting this – it was the only snippet I could find for installing gems that actually works!
Cheers
FYI – I just implemented this, and I inserted RAILS_ENV=production into mine, simply because I have gems that are used on my deployment machines that I do not want installed in my production machines (ie ruby-debug). I only have those gems required in my production environment.
@Justin: Thanks for telling – could be useful for others too.
Worked perfectly first try. Thank you so much!
@Tommy: Nice – good thing you could use it :-)
my favorite gemstone is none other than diamond. the most expensive and priceless gemstone ever.`.,