21

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
npup
  • 2,531
  • 1
  • 17
  • 22
  • 1
    This kind of error usually means you haven't installed the development packages for the relevant software, in this case PG. – Keith Gaddis Dec 08 '10 at 22:59
  • The XCode that came with Snow Leopard on the DVD was broken, which is why we need to update it. – the Tin Man Dec 09 '10 at 05:20
  • Snow Leopard is 64-bit so you have to use `x86_64`. If you've installed anything and forced `i386` you'll need to recompile it as `x86_64` otherwise you will see the message you got. That includes any gems which were compiled/installed prior to installing Snow Leopard if you were running Leopard on that machine previously. – the Tin Man Dec 09 '10 at 19:15
  • You'll need to do a `locate libpq` and find where it is. It is probably in the `lib` directory of your Postgres install. Then scan through the README and/or INSTALL doc for the gem and see if there's a flag needed, similar to the one for "include" and "lib". Search the googles for "ruby mac os libpq" and a lot of threads will show up with info; Others have encountered this, it's one of the more complicated parts of integrating Ruby with a service, but well worth it once it's done. I love working with Postgres and prefer it over the alternatives. – the Tin Man Dec 09 '10 at 19:20
  • FWIW, I'm using homebrew and rbenv, and saw this when trying to ```gem install pg```: ```Can't find the PostgreSQL client library (libpq)```. An old PostgreSQL was the issue. I had to ```rm -rf /Library/Postgre*``` before the homebrew-installed ```pg_config``` was seen. – mrm Mar 12 '12 at 00:36

10 Answers10

32

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.

phant0m
  • 16,595
  • 5
  • 50
  • 82
Yuta
  • 531
  • 1
  • 6
  • 6
11

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 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? :) – npup Dec 09 '10 at 10:01
  • 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.. – npup Jan 03 '11 at 13:35
  • 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. – the Tin Man Jan 03 '11 at 13:40
  • I used the PostGRE installer from the postgre website. – Moshe Jul 06 '11 at 22:42
  • 1
    "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? – thomax May 14 '12 at 18:25
  • 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.. :( – Trip Apr 02 '13 at 13:46
10

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.

Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48
  • 1
    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 – Peter Degen-Portnoy Apr 27 '11 at 14:08
  • 1
    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. – cmhobbs Jul 25 '11 at 15:48
  • Also, make sure that you're running 64-bit ruby. file `rbenv which ruby` – Leopd Apr 26 '12 at 15:35
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!

Josh
  • 8,329
  • 4
  • 36
  • 33
3

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.

Steve
  • 836
  • 11
  • 14
3

I just spent a good deal of time getting this to work tonight. I saw a similar error to this:

Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***

I tried different gem install variations:

gem install pg -- --with-pg-config=/Library/PostgreSQL/9.1/bin/ --with-pg-lib=/Library/PostgreSQL/9.1/lib/ --with-pg-include=/Library/PostgreSQL/9.1/include/

But finally what worked for me was:

gem install pg -- --with-pg-dir=/Library/PostgreSQL/9.1/

Also, before that I updated rvm (rvm get head) and reinstalled ruby (rvm --force install 1.9.2). Not sure if this helped or not, but it might be worth trying if you still hit issues

2

You might want to try going with homebrew for installing postgres (brew install postgres) and Ruby Version Manager for installing and maintaining ruby and ruby gems.

It'll leave your default versions (installed with OSX) untouched and give you more flexibility. For example, you could have different rails apps using rails 2 or 3 with ruby 1.8.7 and 1.9.2 all installed on the same system without problems.

markquezada
  • 8,444
  • 6
  • 45
  • 52
  • 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 Dec 09 '10 at 09:59
  • 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. – markquezada Dec 09 '10 at 20:06
  • Homebrew is brilliant - saved my sanity a couple of times now. RVM is a must for any ruby/rails developer. – jjnevis Sep 23 '11 at 10:25
1

This worked for me:

sudo env ARCHFLAGS="-arch x86_64" gem install pg
ethiyo
  • 11
  • 1
  • 2
0

All the suggestions here and around the Web were incomplete for me, until I run the following on CentOS 6.6:

wget -c ftp://mirror.switch.ch/pool/4/mirror/scientificlinux/6.3/x86_64/updates/security/kernel-devel-2.6.32-504.1.3.el6.x86_64.rpm && sudo yum install kernel-devel-2.6.32-504.1.3.el6.x86_64.rpm
wget -c ftp://mirror.switch.ch/pool/4/mirror/scientificlinux/6.6/x86_64/updates/security/kernel-headers-2.6.32-504.30.3.el6.x86_64.rpm && sudo yum install kernel-headers-2.6.32-504.30.3.el6.x86_64.rpm
sudo yum groupinstall "Development Tools" "Development Libraries" 
sudo yum -y install gcc gcc-c++ git ruby ruby-devel rubygems libvirt-devel mysql-devel postgresql-devel openssl-devel libxml2-devel sqlite-devel libxslt-devel zlib-devel readline-devel tar make automake autoconf curl-devel openssl-devel zlib-devel httpd-devel apr-devel apr-util-devel sqlite-devel ruby193-ruby-doc ruby193-ruby-devel ruby193-build
curl -L get.rvm.io | bash -s stable && source $HOME/.rvm/scripts/rvm && rvm requirements
iammyr
  • 271
  • 5
  • 13
0

Install Postgres.app:

http://postgresapp.com/

Verify what version was installed:

$ ls /Library/PostgreSQL/
# 9.4

Export the SQL_PATH:

export SQL_PATH=/Library/PostgreSQL/9.4

Install pg:

gem install pg -- --with-pg-config=$SQL_PATH/bin/pg_config
monteirobrena
  • 2,562
  • 1
  • 33
  • 45