Failing installing pg gem, "mkmf.rb can't find header files for ruby" (Mac OSX 10.6.5)

postgresql, ruby, ruby-on-rails, rubygems

Solution

Generally the gem bundles for Postgres want to know where `pg_config` is hiding so they can ask about the Postgres installation.

Use `locate pg_config` to see if your Mac knows where it's hiding.

I installed a copy of Postgres using mappstack, so my Mac says there's a copy at:

/Applications/mappstack-1.2-3/postgresql/bin/pg_config

and another at:

/Library/PostgreSQL/9.0/bin/pg_config

I don't remember installing the one at `/Library/PostgreSQL/9.0`, so it might have been preinstalled by Snow Leopard, or I did it when under the influence of too much work, possibly using the Postgres installer from EnterpriseDB.

Once you've found the location of `pg_config` try adding that directory to the start of your PATH and then rerun the gem install. Or use:

export SQL_PATH=/Library/PostgreSQL/9.0
gem install pg -- --with-pg-config=$SQL_PATH/bin/pg_config

and try installing. If either of those work you're done. Otherwise...

The next thing the installers might want are access to the Postgres headers, so you look in the parent of the `bin` directories, and see if you can find an `include` directory.

After that, look in that directory for a `lib` directory. Once you know those locations you should have all you need to set your environment variables to let the installer complete. You'll need to read the `README` or `INSTALL` file of the installer and see what needs to be set up. You'll be configuring:

export include_dir=$SQL_PATH/include/
export     lib_dir=$SQL_PATH/lib/
gem install pg -- --with-pgsql-include-dir=$include_dir --with-pgsql-lib-dir=$lib_dir

Hopefully that'll all help. I have Rails 3 and my Postgres running fine, using the mappstack Postgres and the EnterpriseDB versions, so the above info should get you there.

Problem

I just upgraded to Rails 3 and had a bit of a mess with MacPorts, gems and databases to sort out. I threw out all the gems and installed them fresh. Everything seems to be OK except for the requirement of the pg gem. After creating a new Rails 3 project, prepared for PostgreSQL, the server would not start, complaining about the missing pg gem. Doing `bundle install`, it chugged along for a while and, of course, fails on the pg gem. ``` Installing pg (0.10.0) with native extensions /Library/Ruby/Site/1.8/rubygems/installer.rb:483: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 mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/pg-0.10.0 for inspection. Results logged to /Library/Ruby/Gems/1.8/gems/pg-0.10.0/ext/gem_make.out from /Library/Ruby/Site/1.8/rubygems/installer.rb:446:in `each' from /Library/Ruby/Site/1.8/rubygems/installer.rb:446:in `build_extensions' from /Library/Ruby/Site/1.8/rubygems/installer.rb:198:in `install' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/source.rb:95:in `install' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/installer.rb:55:in `run' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/spec_set.rb:12:in `each' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/spec_set.rb:12:in `each' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/installer.rb:44:in `run' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/installer.rb:8:in `install' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/cli.rb:225:in `install' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/task.rb:22:in `send' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/task.rb:22:in `run' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/invocation.rb:118:in `invoke_task' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor.rb:246:in `dispatch' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/base.rb:389:in `start' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/bin/bundle:13 from /usr/bin/bundle:19:in `load' from /usr/bin/bundle:19 ``` The most common suggestion I have found on forums and blogs is to (re)install the XCode developer tools. They were already there, but I reinstalled them anyway, with no better outcome than the above. `which ruby` says `/usr/bin/ruby`, and `ruby -v` says `ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]`, if there's any clue there. Pointers are welcome. Any missing information that would help figuring this out, I'll gladly and promptly provide! I downloaded XCode since the DVD version is said to be broken and things went better for a while. `bundle install` started talking about "archflags" instead, so I did: ``` sudo env ARCHFLAGS="-arch i386" bundle install ``` and things seemed to go fine, until starting Rails barfed, saying: ``` /Library/Ruby/Gems/1.8/gems/pg-0.10.0/lib/pg_ext.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/pg-0.10.0/lib/pg_ext.bundle, 9): no suitable image found. Did find: (LoadError) /Library/Ruby/Gems/1.8/gems/pg-0.10.0/lib/pg_ext.bundle: mach-o, but wrong architecture - /Library/Ruby/Gems/1.8/gems/pg-0.10.0/lib/pg_ext.bundle ``` Grappling for anything, I tried `bundle install` with archflags set to `-arch x86_64`, but it didn't go well: ``` *** Your PostgreSQL installation doesn't seem to have an architecture in common with the running ruby interpreter ([] vs. ["x86_64"]) I'll continue anyway, but if it fails, try setting ARCHFLAGS. [...] Can't find the PostgreSQL client library (libpq) *** extconf.rb failed *** ``` Is libpq something I now have to supply a path to via some flags? Or am I completely out to lunch?

Original source