« RailsConf 2011: Deploying with Bundler | Main | LivingSocial's Aaron Batalion says "Be Nervous" »

May 18, 2011

Rails 3 Gemfile: Installing PostgreSQL on OS X

After two months of building my Rails app working on remote servers, I'm sick of always having to SSH in and use a server-side text editor to work. Not to mention that any time I move the laptop I've got to break and then re-establish all the terminal windows I had open.

So I embarked on setting things up to run my Rails app right on my MacBook Pro. Yea, the thing that 99% of the rest of the Rails community has been doing for years.

I figured everything except PostgreSQL is either already on the laptop or is nicely grouped together in the my Rails application in git. So I installed Postgres from their OS X Postgres install page.

Checked my git repo out on OS X, ran the "bundle install" and got:
Installing pg (0.11.0) with native extensions /Library/Ruby/Site/1.8/rubygems/installer.rb:533:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /Library/PostgreSQL/8.3/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***

Obviously a failure. The Gemfile includes "gem 'pg'", which works fine up on the Linux servers but apparently doesn't do so well on OS X.

A lot of digging and reading, followed by a lot more digging and reading.

The first solution...when compiling pg, the gem needs to have an environment variable set:
env ARCHFLAGS="-arch i386" bundle install --path /Users/mike/inventory/vendor

No problem running the bundler, but when you actually try to use the database:
bash$ rake
rake aborted!
dlopen(/Users/mike/inventory/vendor/ruby/1.8/gems/pg-0.11.0/lib/pg_ext.bundle, 9): no suitable image found. Did find:
/Users/mike/inventory/vendor/ruby/1.8/gems/pg-0.11.0/lib/pg_ext.bundle: mach-o, but wrong architecture - /Users/mike/inventory/vendor/ruby/1.8/gems/pg-0.11.0/lib/pg_ext.bundle

Tons more reading and digging, trying lots of suggestions.

Finally I stumbled into something that made a lot of sense. A post on the rails forum suggests that Ruby and PostgreSQL are compiled for different architectures and the gem pg connector isn't going to work unless Ruby and Postgres are on the same page.

I got an updated Ruby using MacPorts, same error. I got an updated PostgreSQL from MacPorts and attempted to run "bundle install" as standard.


Then I went back to including the architecture-specific command:
env ARCHFLAGS="-arch x86_64" bundle install --path /Users/mike/inventory/vendor

Works, and I'm able to run "rake db" commands.

Everything else looks like it's in place, am running the Rails app in my browser at localhost:3000 and have TextMate loaded up with my project files.

Much, much, much better.

Posted by mike at May 18, 2011 3:45 PM