Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
776 views
in Technique[技术] by (71.8m points)

ruby on rails - Installing pg gem; ERROR: Failed to build gem native extension

After updating to OS X 10.9 Mavericks I tried to start a Rails 3 app, but the connection to the PG database was not working. Checking on PGAdmin III, the database is still there and it works fine. So I tried to reinstall the pg gem:

gem uninstall pg
gem install pg

But the last command doesn't succeed, and gives the following error:

Building native extensions. This could take a while... ERROR: Error installing pg:

ERROR: Failed to build gem native extension.

    /Users/XXX/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb checking for pg_config... yes Using config values from

/usr/local/bin/pg_config * 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.

/Users/XXX/.rvm/rubies/ruby-1.9.3-p194/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/XXX/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/mkmf.rb:461:in try_link0' from /Users/XXX/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/mkmf.rb:476:in try_link' from extconf.rb:39:in `'

I guess the problem is related to the Xcode developer tools. I updated Xcode to the latest version, but that didn't solve the problem. Do you know how to fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're right that the problem is related to the Xcode developer tools. It's not a bad idea to make sure you have all the developer tools installed (as opposed to solely installing gcc as mentioned in the previous answer):

  1. Open up Xcode
  2. In the application menu item "Xcode" select Open Developer Tool > More Developer Tools...
  3. This takes you to a site with a bunch of software. Go ahead and download and install "Command Line Tools (OS X Mavericks) for Xcode - Late October 2013".
  4. You will now be able to properly install the gem.

For anyone else coming to this issue off of a fresh install of the Postgres 9.3.0 app on Mac OS X Mavericks (i.e. you're not using homebrew for your Postgres installation) you may notice that even though you can build the pg gem you cannot run rake because of a dylib issue:

rake aborted!
dlopen(/Users/[USERNAME]/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/pg-0.15.1/lib/pg_ext.bundle, 9): Library not loaded: @loader_path/../lib/libpq.5.dylib
  Referenced from: /Users/[USERNAME]/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/pg-0.15.1/lib/pg_ext.bundle
  Reason: image not found - /Users/[USERNAME]/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/pg-0.15.1/lib/pg_ext.bundle

Unfortunately this is a problem with the current release version of 9.3.0 of Postgres. Winding back to a previous version will fix it for you:

  1. uninstall the pg gem: gem uninstall pg
  2. delete your 9.3.0 Postgres app by dragging it to the trash and emptying the trash
  3. install version 9.2.2.0 of the Postgres app here: http://postgres-app.s3.amazonaws.com/PostgresApp-9-2-2-0.zip
  4. reinstall the pg gem: gem install pg

*Thanks to the comment by jhiro009 on this thread for pointing me in the right direction on this last Postgres app part of the issue although the 9.2.4.3 version that he mentioned didn't work for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...