gem install pg doesn't work on OSX Lion
Asked Answered
P

13

31

There are variations of this question bouncing around SO, but none of them seem to have an answer that solves my problem.

I am running OSX Lion (10.7.3). The latest XCode is installed.

I've installed Postgres using the Postgres.app package from postgresapp.com.

But when I try to install the pg gem, I get a failure:

$ gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

        /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/bin/ruby extconf.rb --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Using config values from /Applications/Postgres.app/Contents/MacOS/bin/pg_config
checking for libpq-fe.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/bin/ruby
    --with-pg
    --without-pg
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
    --with-pg-config
/Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:506:in `try_cpp'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:970:in `block in find_header'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:790:in `block in checking_for'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:284:in `block (2 levels) in postpone'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:284:in `block in postpone'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:280:in `postpone'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:789:in `checking_for'
    from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:969:in `find_header'
    from extconf.rb:40:in `<main>'


Gem files will remain installed in /Users/disaacs/.rvm/gems/ruby-1.9.3-p125/gems/pg-0.13.2 for inspection.
Results logged to /Users/disaacs/.rvm/gems/ruby-1.9.3-p125/gems/pg-0.13.2/ext/gem_make.out

The output of the /Applications/Postgres.app/Contents/MacOS/bin/pg_config command is:

$ /Applications/Postgres.app/Contents/MacOS/bin/pg_config
BINDIR = /Applications/Postgres.app/Contents/MacOS/bin
DOCDIR = /Applications/Postgres.app/Contents/MacOS/share/doc
HTMLDIR = /Applications/Postgres.app/Contents/MacOS/share/doc
INCLUDEDIR = /Applications/Postgres.app/Contents/MacOS/include
PKGINCLUDEDIR = /Applications/Postgres.app/Contents/MacOS/include
INCLUDEDIR-SERVER = /Applications/Postgres.app/Contents/MacOS/include/server
LIBDIR = /Applications/Postgres.app/Contents/MacOS/lib
PKGLIBDIR = /Applications/Postgres.app/Contents/MacOS/lib
LOCALEDIR = /Applications/Postgres.app/Contents/MacOS/share/locale
MANDIR = /Applications/Postgres.app/Contents/MacOS/share/man
SHAREDIR = /Applications/Postgres.app/Contents/MacOS/share
SYSCONFDIR = /Applications/Postgres.app/Contents/MacOS/etc
PGXS = /Applications/Postgres.app/Contents/MacOS/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/Users/mattt/Code/heroku/Postgres/Postgres/Vendor/postgres' '--enable-thread-safety' '--with-openssl' '--with-gssapi' '--with-bonjour' '--with-krb5' '--with-libxml' '--with-libxslt' '--with-ossp-uuid' '--with-perl' '--with-python'
CC = gcc
CPPFLAGS = -I/usr/include/libxml2
CFLAGS = -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wformat-security -fno-strict-aliasing -fwrapv
CFLAGS_SL = 
LDFLAGS = -Wl,-dead_strip_dylibs
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgport -lxslt -lxml2 -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lm 
VERSION = PostgreSQL 9.1.3

All those setting appear to be correct, except for the CONFIGURE variable (which seems to be referring to some other computer --I have no user named mattt).

The contents of the mkmf.log file are

"/usr/bin/gcc-4.2 -o conftest -I/Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/x86_64-darwin10.8.0 -I/Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/ruby/backward -I/Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1 -I. -I/Users/disaacs/.rvm/usr/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/Applications/Postgres.app/Contents/MacOS/include  -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration  -fno-common -pipe conftest.c  -L. -L/Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib -L/Users/disaacs/.rvm/usr/lib -L. -L/usr/local/lib -L/Applications/Postgres.app/Contents/MacOS/lib     -lruby.1.9.1  -lpthread -ldl -lobjc "
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main() {return 0;}
/* end */

Any suggestions would be appreciated.

I've looked at the extconf.rb file, and after some debugging I've found that it is a call to

find_header ('libpq-fe.h')

that is failing. Not because libpq-fe.h is not there, but because the find_header method itself is failing. I tried running find_header using irb, and got the same error:

$ irb
1.9.3-p125 :001 > require 'mkmf'
 => true 
1.9.3-p125 :002 > find_header 'libpq-fe.h'
checking for libpq-fe.h... RuntimeError: The compiler failed to generate an executable file.
You have to install development tools first.

EDIT:

Found out my computer must be even more borked than I thought. I was doing bundle install for another project, and I got a very similar failure installing the sqlite3 gem.

checking for sqlite3.h... *** extconf.rb failed ***

I've never had problems with the sqlite3 gem in the past.

Powered answered 27/4, 2012 at 4:42 Comment(11)
#8549309Florrieflorry
The solution to the issue in that link is to install the latest XCode. I already have the latest XCode installed.Powered
Have you tried installing pq using homebrew? Also worth trying is seeing if the libpq or libpq-dev packages are available.Degrading
I tried installing postgres using Homebrew. I get the same error when installing the gem "checking for libpq-fe.h... *** extconf.rb failed ***" --What is the libpq-fe.h file anyway? I looked in the include files that were installed with postgres, and there is no libpq-fe.h file. There is a libpq-fs.h file.Powered
I found libpq-fe.h in /usr/local/include, but even when I add this directory using --with-pg-include, it does not make any difference.Powered
[deleted] Oops, thought this was a different question. You were getting the same error.Seepage
If you manually create the program dumped in the mkmf.log, and try to compile it using the line right above that (/usr/bin/gcc-4.2 [...]), what happens?Seepage
I've just added some additional information to the bottom of my original question. It seems that it is a call to find_header, from the mkmf library, that is causing the problem.Powered
Have you tried #4393689Hemostat
Where is the mkmf.log file?Pal
@Jason, the mkmf.log file can be found under the gem's installation directory. E.g., if using RVM, then look for a directory like ~/.rvm/gems/ruby-1.9.3-p125/gems/pg-0.13.2/ext/mkmf.log. Or try running the command `find . | grep mkmf.log' from the .rvm directory. That will find them all.Powered
P
62

I finally found the cause of the problem! Though I am still uncertain how this problem came about.

Look at the contents of the mkmf.log file that is created when my install command fails (see my original post). It logs an attempt to run the following command

/usr/bin/gcc-4.2 ...and so on...

There is no /usr/bin/gcc-4.2 on my system. There is a gcc, which is symbolically linked to llvm-gcc-4.2. My solution was to create another symbolic link

sudo ln -s llvm-gcc-4.2 gcc-4.2 

After making this link, my gem install pg command worked without any problems.

I found the solution to this problem on the Frozen Canuck blog in the post Error Installing Ruby Gem with C Extension. He speculates that the problem can be caused by conflicting versions of XCode. whatever the cause, my eternal thanks!

Powered answered 5/5, 2012 at 3:34 Comment(2)
Fixed with "sudo ln -s /usr/bin/{llvm-,}gcc-4.2". Thanks!Coactive
I had no llvm-gcc-4.2 Run "brew install apple-gcc42" to get /usr/bin/gcc-4.2 (10.9.1)Kemppe
D
19

On OS X Mavericks

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

works.

Dibri answered 24/10, 2013 at 5:0 Comment(0)
W
15

I had a similar issue. Following an answer by bobfet1 for a similar issue I was able to get the gem installed by:

sudo env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Warp answered 3/5, 2012 at 11:18 Comment(3)
I tried this. Made no difference. I just found out some new things and updated my original post.Powered
What is the purpose of the lone -- after pg? It doesn't seem to work without them, but I don't know what they are for.Ablebodied
@Ablebodied From gem help install: "You may need to specify the path to the library's headers and libraries to continue [to build an extension]. You can do this by adding a -- between RubyGems' options and the extension's build options". In general, -- means "following arguments are not options." E.g. rm -- -r removes a file named -r.Pomiferous
N
13

Thanks to this guy: http://www.computersnyou.com/2025/

On Mavericks using: xcode-select --install (assuming you have installed Xcode from the app store) works.

Niue answered 4/11, 2013 at 19:59 Comment(1)
I am not sure why this has not been marked as the answer but it absolutely hits the nail on the head considering the initial question. Thanks @NiueCopious
T
7

Installing xcode from the app store is not enough. You will also need xcode's command line tools. https://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_3.html

After installing xcode from the app store, open xcode. Goto Xcode->Open Developer Tool->More Developer Tools Download and install the latest command line tools.

Open a terminal window and run: sudo gem install pg

Tilburg answered 3/7, 2012 at 16:0 Comment(0)
M
6

Similar problem on Mavericks 10.9 -- but it was caused by not having agreed to the Xcode license agreement. Blech.

Run sudo xcodebuild and agree to the junk. The everything works as expected.

Meteoritics answered 3/11, 2013 at 19:19 Comment(0)
D
2

I also ran into this issue, for me the answer was updating RVM of all things.

rvm get head
rvm install 1.9.2
Denominate answered 3/5, 2012 at 11:48 Comment(1)
Wouldn't this downgrade my Ruby version to 1.9.2? I tried upgrade to the latest RVM, but it didn't make a difference.Powered
R
2

I too, am on Mountain Lion, besides the "sudo ln -s /usr/bin/{llvm-,}gcc-4.2" suggested by @grilix, I needed to do:

sudo ln -s /usr/bin/{llvm-,}g++-4.2
Rascality answered 5/10, 2012 at 15:10 Comment(0)
R
2

After a Mavericks upgrade...

Reinstall your ruby version.

rvm uninstall 1.9.2-p320

then

rvm install 1.9.2-p320

or whatever version of ruby you have.

Restrainer answered 12/6, 2014 at 0:15 Comment(0)
H
1

I am on Mountain Lion, and the accepted answer from Dave Isaacs worked for me. I also had to do

sudo ln -s llvm-g++-4.2 g++-4.2
Henri answered 27/7, 2012 at 16:58 Comment(1)
I didn't need that on OS X 10.8.2. sudo ln -s /usr/bin/{llvm-,}gcc-4.2 worked.Thibault
C
1

Rather than updating RVM itself (https://mcmap.net/q/458714/-gem-install-pg-doesn-39-t-work-on-osx-lion), as above, it was the reinstallation of Ruby 2.0.0-p247 that did the trick, after trying all the other steps above without success.

rvm get head
rvm install 2.0.0
Convergence answered 2/10, 2013 at 0:20 Comment(0)
L
1
$ gem install pg

If that failed:

$ sudo gem install pg

Then, if that failed:

$ sudo find / -name “pg_config”

After you find pg_config's location, run this to finally install it:

$ sudo gem install pg --with-pg-config=/usr/local/Cellar/postgresql/9.5.1/bin/pg_config

Of course, you'll want to change the path to wherever you found pg_config.

Then, check your installed gems with gem list, and it should show something like this:

<code>gem list</code> showing the right things

More information can be found in my guide.

Lixiviate answered 18/3, 2016 at 16:45 Comment(0)
S
0

If the output includes:

/Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': \
    The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.

then there really is something amiss with your compilation environment. Especially when the failure is the very first test that extconf.rb runs.

If you have XCode installed, then perhaps you don't have the command-line tools component of XCode installed? They were recently split out from XCode proper, so you need to install them separately.

Seepage answered 28/4, 2012 at 0:0 Comment(1)
I originally did not have the command-line tools installed. But installing them made no difference. The same error is still occurring.Powered

© 2022 - 2024 — McMap. All rights reserved.