Capistrano with Rails 3 and bundler

Today I tried to deploy a new Rails 3 project using beloved Capistrano.

When it came to installing the gems after the deployment process however, although I found an almost ready to use task for Capistrano, I stumbled upon a severe issue:
Whenever bundler tried to install gems, I was automatically asked for my root password.

Given that interactive prompting is nasty with Capistrano, and I don’t want a single application’s gems installed system-wide anyway, I did a little research and found a hardly documented way to install the gems inside the Rails application directory (what used to be vendor/gems).

Apparently you don’t use vendor/gems anymore, but vendor/bundle. The idea is almost the same though.
Here is the task you need, in order to make bundler unpack the gems there:

namespace :bundler do
  set :bundle_dir, File.join(release_path, 'vendor/bundle')
 
  task :create_symlink, :roles => :app do
    shared_dir = File.join(shared_path, 'bundle')
    run("mkdir -p #{shared_dir} && ln -s #{shared_dir} #{bundle_dir}")
  end
 
  task :bundle_new_release, :roles => :app do
    bundler.create_symlink
    run "cd #{release_path} && bundle install #{bundle_dir} --without test"
  end
end
 
after 'deploy:update_code', 'bundler:bundle_new_release'

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.