Rails 3, bundler, and the pg gem 3

Posted by Tom Copeland Thu, 16 Sep 2010 21:54:00 GMT

I was moving a Rails 2 app up to Rails 3 and, because I have PostgreSQL installed in a non-standard location on my server, I ran into problems when bundler was trying to install the pg gem. After fiddling about for a bit I ended up with this in my config/deploy.rb:

require 'bundler/capistrano'

task :set_config_for_pg_gem, :roles => [:app, :db] do
  run "cd #{current_path} && bundle config build.pg --with-pg-config=/usr/local/pgsql/bin/pg_config --no-rdoc --no-ri"
end

before "bundle:install", :set_config_for_pg_gem

This sets up the appropriate command line flags for the pg gem so that they're in place when Capistrano runs the bundle:install task. The --no-rdoc --no-ri part isn't necessary, but I figured it'll save a second or two. Note that these flags end up in the deploy user's home directory on the server:

$ cat ~/.bundle/config 
--- 
BUNDLE_BUILD__PG: --with-pg-config=/usr/local/pgsql/bin/pg_config --no-rdoc --no-ri

Running this task every time you deploy is a little wasteful since it sets the configuration unnecessarily - really you just need it before the first time you deploy. So you could optimize things by touching a file in shared/system/ or some such and checking it as part of this task.

Comments

Leave a comment

  1. raggi 39 minutes later:

    One-time setup is one of the things I really liked about the vlad workflows. I’d also put a generator in to add the no-rdoc no-ri flags to ~/.gemrc

  2. Tom Copeland 42 minutes later:

    Ah right, .gemrc is probably a better place for that. Hm, I could also add .gemrc to my puppet configs makes note to self

  3. Matthias Viehweger 4 days later:

    I mostly have a little script for such one-time tasks. PostgreSQL seldomly comes to a project later in the process, so its part of the general setup.

    However your solution might come in handy for other gems…

Comments