⚠️ Archived Post
This is an archived post from my previous blog (2007-2014). It may contain outdated information, broken links, or deprecated technical content. For current writing, please see the main Writing section.

Using Rails gems:build Rake Task with Capistrano

Originally published on June 19, 2008

I use acts_as_ferret in my Rails app which depends on the ferret gem. With Rails 2.1 you can define gem dependencies right in your environment.rb file. That’s pretty neat. What if your gem needs native extensions to be built? Also easy, just run

rake gems:build

Of course you’d also like these extensions to be built on your production machine when you deploy with Capistrano. I’m sure there are more elegant ways to do this, but this works for me. Just add this to your deploy.rb file:

task :after_update_code, :roles => :app do
  if ENV['build_gems'] and ENV['build_gems'] == '1'
    run "rake -f #{release_path}/Rakefile gems:build"
  end
end

This way you can pass an environment variable to tell Capistrano if you want native gem extensions to be built or not. If you do, call this:

build_gems=1 cap deploy

…and if you don’t just call

cap deploy
.

I always love to learn about more elegant solutions, so please comment if you know one.

If this was useful for you, please take a minute and recommend me:
Recommend Me
Thank you!