HOWTO: Wrangle the pg (postgresql) gem with macports and rvm

Seeing this?

Installing pg (0.11.0) with native extensions /Users/mrm/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:533:in `rescue in block in build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

        /Users/mrm/.rvm/rubies/ruby-1.9.2-p180/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

Wondering how or where to get this mythical pg_config? It’s part of the PostgreSQL package. If you’re running MacPorts, it’s easy:

sudo port install postgresql90

Then, following the instructions on the RVM website, run

gem install pg -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config

Related posts:

  1. Quick MacPorts Installation of Ruby on Rails
    October 2010 update: I wrote this a while back, before I knew about RVM. The Ruby Version Manager has a bunch of features that makes it better than macports for......
  2. Apache2, PHP, and MySQL on Mac OS X using MacPorts
    1. Install MacPorts Follow the instructions here: http://www.macports.org/install.php. 2. Install apache2 sudo port install apache2 Note that the macports instructions suggest installing the launchctl script now, but we’ll do that......
  3. HOWTO: Fix Ruby on Rails 2.3.x textarea Value Truncation
    Turns out that Rails versions 2.3.6 through 2.3.8 have a pretty horrid parameter-parsing bug, and I’m surprised there hasn’t been more hoopla about it. If you have a textarea form......
  4. HOWTO: Make system-wide RVM installations work with cron, monit, delayed_job, and passenger
    System-wide RVM installations are recommended for production deployments, but because /usr/bin/ruby is no longer the “correct” ruby, you need to tell all your moving parts about RVM. Before installing ruby......

  • http://twitter.com/mike_gauthier Mike Gauthier

    I ran into something similar when installing the mysql gem with rvm.  Here is what worked for me…

    gem install mysql — –with-mysql-dir=/usr/local/mysql –with-mysql-lib=/usr/local/mysql/lib –with-mysql-include=/usr/local/mysql/include

    I’ve also seen this work for mysql2:
    gem install mysql2 — –with-mysql-config=/path/to/bin/mysql_config

  • http://pulse.yahoo.com/_6ZYQR6VU4EUJIVAVT3D4TMGR4E Jasdeep

    YOU ARE A LIFE SAVER!!!!!!!!! I’ve been running around like a clown for the last 3-4 hours to get pg gem installed on my Mac OS X but all attempts futile..! I feel like having a pint of beer with you at the moment and kissing you on the forehead!

    You the man!

  • Mathew

    Dude … I LOVE YOU LONG TIME!!! spent ages trying to sort this out .. and bang .. here’s the answer…

    Thanks a million!

  • lothar59

    i’m facing the same problem on Ubuntu, if anyone knows how to fix this…

  • lothar59

    ok just had to do sudo aptitude install libpq-dev then gem install pg