Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Automating Bundler In Your Deploy

If you are using Bundler to lock and package your gem dependencies in your app (which you should), here’s some tips on making everything automatic in your Capistrano deploy.

Refer to the Bundler Documentation for instructions on how to use Bundler to properly package your gems and check everything in.

Once this is done, however, you still must ensure that two things are done on every machine to which you will deploy:

  1. Bundler is installed
  2. You run ‘bundle install’ on every deploy to install the packaged gems on the local machine (and compile any gems with native dependencies)

Here’s the Capistrano magic to accomplish these two tasks automatically on every deploy:

before "deploy:bundle_install", "deploy:install_bundler"
after "deploy:update_code", "deploy:bundle_install"

namespace :deploy do
  desc "installs Bundler if it is not already installed"
  task :install_bundler, :roles => :app do
    sudo "sh -c 'if [ -z `which bundle` ]; then echo Installing Bundler; sudo gem install bundler; fi'"

  desc "run 'bundle install' to install Bundler's packaged gems for the current deploy"
  task :bundle_install, :roles => :app do
    run "cd #{release_path} && bundle install"

Oh, and for you GemInstaller users out there – here’s an easy way to generate a Bundler Gemfile from your geminstaller.yml config:

geminstaller --bundler-export > Gemfile

You’ll probably still need some tweaks, but this will get you started. Just make sure you upgrade to GemInstaller 0.5.5 first (0.5.4 forgot to put the ‘source’ line in the Gemfile).

Happy Bundling!
— Chad

P.S. There is a similar article here, which includes tasks to symlink your .bundle dir into the Capistrano shared directory, but my deploy was pretty fast anyway, so I didn’t worry about it. YMMV.

  1. szimek says:

    It’s probably better to run “bundle install” after deploy:update_code using
    run “cd #{release_path} && bundle install”

    If you deploy using “deploy:migrations”, it runs “deploy:migrate” task before running “deploy:symlink”, so it will try to run “rake db:migrate” first and then “bundle install”. This can cause problems if you want to run migrations and deploy modified Gemfile at the same time.

  2. Chad Woolley says:


    Thanks for the tips. I’ve tested it and updated the article.

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *