How to install the Postgres pg gem on OSX Mountain Lion 10.8.2 (Ruby)

I have Mountain Lion on my new machine. (What a horrible release.) I innocently decided to upgrade my Postgres installation to 9.2.2. I had a lot of trouble getting the Ruby pg gem to build. I found no single source that solved the problem, so I thought I’d post the solution.

The symptom is this spewage:

ERROR:  Error installing pg:
	ERROR: Failed to build gem native extension.

        /Users/marick/.rvm/rubies/ruby-1.9.2-p320/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... *** extconf.rb failed ***

Note: if the “checking for pg_config” line fails, you have a different problem. Either you need to set PGDATA correctly, or make sure that the `pg_config` command is in your $PATH. A number of pages out there on the web explain what’s going on.

  1. If you look at the `mkmf.log` file, you’ll see that (for some reason) the installer is trying to use `/usr/bin/gcc-4.2`. That doesn’t exist, so:

         sudo ln -s /Developer/usr/bin/gcc /usr/bin/gcc-4.2
    

    Thanks to Stackoverflow user dfrankow for that bit of the solution.

  2. That still fails because the program you’re compiling innocently tries to include `<stdio.h>` (etc.) Unlike Unix systems since the beginning of time, there is no `stdio.h` in `/usr/include`. To populate /usr/include you have to start Xcode (version 4.X), go to Preferences, pick the “Downloads” tab, and install the Command Line Tools.

    Thanks to Tim Burks on Twitter for this piece of the puzzle.

2 Responses to “How to install the Postgres pg gem on OSX Mountain Lion 10.8.2 (Ruby)”

  1. ged Says:

    Is your rvm Ruby one that was compiled on a different machine? One running OS 10.7, perhaps?

    The ‘extconf.rb’ for ‘pg’ uses ‘mkmf’, which in turn uses the ‘rbconfig’ library to determine which compiler/preprocessor/linker to use. If you try to compile an extension with a build environment different than the Ruby that it’s being installed for, you’ll get no end of problems like this.

    I’ve tried to make the ‘pg’ install experience an easy one, but it’s very difficult to anticipate every possible problem one might encounter, especially when you factor in rvm or rbenv. I’ve debated trying to distribute binary gems for the common platforms, but that I fear is just a different can of equally-complex worms. I’ve tried adding instructions and troubleshooting advice to the READMEs (the pg_config thing is in both the main one and the OSX one, for example), but I’ve never seen evidence that anyone reads them, so I’ve given up trying to keep them current.

    That said, I’m aware that it needs improvement. Any suggestions you have about how the installer could be more helpful (modulo the restricted opportunities for user interaction, output, etc. afforded by the gems installer), I’d be happy to hear them.

  2. Brian Marick Says:

    ged: I don’t think this is the pg_config problem, at least not as it’s described in the READMEs.

    I did not mean to be critical, just pragmatic. My own small experience has taught me of the horrors of multiple configurations. Something like the `pg` gem must be a nightmare to maintain. So: thank you.

    I was only hoping that someone doing the same google searches I did would stumble on this post and find a quick solution.

Leave a Reply

You must be logged in to post a comment.