Installing the pg gem on OS X with MacPorts

The pg gem, used to connect ruby programs to the PostgreSQL server, is notoriously difficult to install on OS X.

I’m using MacPorts and have PostgreSQL 9 installed. Here’s what I did:

jacques$ sudo bash
root$ export PATH=/opt/local/lib/postgresql90/bin:${PATH}
root$ env ARCHFLAGS="-arch x86_64" gem install pg
root$ env ARCHFLAGS="-arch x86_64" gem install rdbi-driver-postgresql

First, the code switches us to the root user to avoid various hair-pulling problems with using ‘sudo’ on commands starting with ‘env’.

Next we export the path where PostgreSQL lives in a MacPorts setup. You may need to adjust this path depending on the version you have installed.

Next, using ‘env’, we tell gem to install and compile a 64-bit version of the pg gem. Then we do the same again for the RDBI driver I’m using for a project.

And that’s it.

Leaving out any part, trust me, is a path to eternal madness.

This entry was posted in Technical Notes. Bookmark the permalink.