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

23,710

Solution 1

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.

Solution 2

I encountered this error when I tried to install rails by gem on CentOS 6.3.

After googling a bit, I found a quick fix: installing the ruby-devel package.

sudo yum install ruby-devel

After that, everything worked fine.

Solution 3

This worked for me on OS X 10.6.6, with PostgreSQL 9.0.1 installed from the source code:

export PATH=/usr/local/psql/bin:$PATH
export ARCHFLAGS='-arch x86_64'
gem install pg

You'll likely need to edit that PATH to match your postgres install location.

Solution 4

I've got postgres 1.9.0 installed via macports. OS X 10.5 PPC

This worked for me:

gem install pg -- --with-pg-lib=/opt/local/lib/postgresql90 --with-pg-include=/opt/local/include/postgresql90

Good luck!

Solution 5

Generally the gem bundles for Postgres want to know where pg_config is hiding . . .

Right - this could be the trick. If you just installed PostgreSQL and added it to your path, and did "bundle install" in an old shell, it won't be able to find pg_config. If that's the case, just get a new shell and try again. Otherwise, follow the steps above to find it and get it seen by bundler.

The macports install for postgresql83 works fine and plays nicely with rails right out of the box - that's another way to do it.

Share:
23,710
npup
Author by

npup

Updated on May 04, 2020

Comments

  • npup
    npup about 4 years

    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?

  • npup
    npup over 13 years
    Thanks. I'm a bit wary about fiddling with postgres since this is my dev machine for work (we use postgres there, and it's well set up for its purposes). Maybe it doesn't matter? I don't know how homebrew works (yet).
  • npup
    npup over 13 years
    Thanks! I came a bit further (edited original post) and maybe there is now just some pg lib path to supply as you desribed. Maybe I'm getting close? :)
  • markquezada
    markquezada over 13 years
    I'd encourage you to check it out. Homebrew installs files under its own filepath (ie, /usr/local/Cellar/postgresql/) and then symlinks the binary to /usr/local/bin for easy access (and doesn't touch anything that's preinstalled). Even if you don't use homebrew though, you should still check out RVM. It'll probably save you a lot of trouble with managing gems.
  • npup
    npup over 13 years
    The above solution helped me on another computer in what first appeared to be in the same state, so it was a working solution, thanks. Still fighting with the main computer where something more is lacking..
  • PJP
    PJP over 13 years
    Re-edit your original question and add symptoms you're seeing. It's most likely something is missing still. I was just doing some development using my Postgres and Rails3 so I know it's possible to get it working.
  • Peter Degen-Portnoy
    Peter Degen-Portnoy about 13 years
    My issue is the same as @npup reported: running bundle install caused the problem. When I tried gem install pg it ran with no issues, but bundle install still failed. I am running OS/X 10.6.7, PostgreSQL 8.4.6, RVM 1.1.9, Ruby 1.9.2p136 and Rails 3.0.4. Running export ARCHFLAGS='-arch x86_64', then running bundle install, the bundle finally installed completely and correctly
  • Moshe
    Moshe almost 13 years
    I used the PostGRE installer from the postgre website.
  • cmhobbs
    cmhobbs almost 13 years
    This one worked for me. The fix selected for this question worked for installing pg via gem, but bundle still chocked. Setting ARCHFLAGS was the fix on 10.7 and PostgreSQL installed via Homebrew.
  • jjnevis
    jjnevis over 12 years
    Homebrew is brilliant - saved my sanity a couple of times now. RVM is a must for any ruby/rails developer.
  • Leopd
    Leopd about 12 years
    Also, make sure that you're running 64-bit ruby. file rbenv which ruby
  • thomax
    thomax about 12 years
    "or I did it when under the influence of too much work" :-) Didn't your momma tell you never to visit that bad neighborhood where Too Much Work used to hang out?
  • Trip
    Trip about 11 years
    My header files are all properly in the include/ directory; I used homebrew. I'm very speifically telling it to use my pg_config from Homebrew, and also very specifically telling it to use that include dir where I know the h files are located. Even so, it still can not find my Header files.. :(
  • humbug
    humbug over 8 years
    That did the trick for me too. But in Ubuntu 15.10 its sudo apt install ruby-dev