20

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?

BryanH
  • 5,826
  • 3
  • 34
  • 47
Abramodj
  • 5,709
  • 9
  • 49
  • 75

8 Answers8

22

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.

Community
  • 1
  • 1
schmielson
  • 330
  • 1
  • 7
  • thank you very much, i had another problem, all people said to me to solve it was ``xcode-select --install``, but it didn't work for me and now your solution More Developer Tools works perfectly :) – rafa Nov 07 '13 at 02:47
  • You don't have to use an earlier version of postgresql. You can still install postgresql 9.3.x successfully if you create a symlink to the missing dylib file. In my case I had to run: sudo ln -s /Library/PostgreSQL/9.3/lib/libpq.5.dylib /usr/local/lib/libpq.5.dylib – 2potatocakes Nov 19 '13 at 07:41
19

Using homebrew fixed this for me:

gem uninstall pg
brew install apple-gcc42
gem install pg

EDIT: I also manually installed "devtools"

xcode-select --install
codenamev
  • 2,203
  • 18
  • 24
  • 2
    Ugh. I'm (not the poster) hoping to avoid installing brew. – kwerle Oct 23 '13 at 15:52
  • 2
    brew is so easy to install, and will save you from future headache; but if you really don't want to, or can't, you can always try upgrading Apple's awful GCC: https://github.com/kennethreitz/osx-gcc-installer – codenamev Oct 23 '13 at 18:05
  • @kwerle I'd give homebrew a shot, it really makes a lot of stuff much easier, and save you in a lot of different situations. – Paul Pettengill Oct 24 '13 at 02:31
  • @kwerle putting off using homebrew only makes it harder for you. – Keith Smiley Oct 25 '13 at 21:49
  • If you get a "invalid multibyte escape: /^\037\235/" error when using homebrew, check out this [thread](http://stackoverflow.com/questions/19635804/updated-to-osx-10-9-now-getting-ruby-error-using-homebrew) – drynaski Oct 28 '13 at 17:29
  • There is Macports. Its like homebrew... but without the ruby & stuff. Its like Homebrew's geeky older brother who doesn't go to all the hipster parties ;-) (incase this seems fanboyish, I'm meerely suggesting an alternative, as long as you arent using Fink your doing the right thing lol) – Techdragon Nov 10 '13 at 04:10
  • 1
    `xcode-select --install` is showing a message that it can't install the software since it's not available... Go the manual way: 1) Open up Xcode. 2) In the application menu item "Xcode" select Open Developer Tool > More Developer Tools... 3) Download and install "Command Line Tools (OS X Mavericks) for Xcode - Late October 2013". – Someone Somewhere Nov 11 '13 at 17:58
  • 1
    I should also point out that you should install PSQL itself via brew as well to avoid stuff like this. Package managers job is to make your life easier via handling dependencies and generally _managing_ your packages. Don't be afraid to use it. – Tanel Suurhans Feb 16 '14 at 18:04
8

On OS X Mavericks

sudo ln -s /usr/bin/llvm-gcc /usr/bin/gcc-4.2
gem uninstall pg; gem install pg;

works with homebrew Postgresql (9.3.1) installation and Apple Command Line Tools installed (pg 0.17.0).

guapolo
  • 2,443
  • 2
  • 20
  • 19
3

None of the previous solutions worked for me (I just upgraded to Mavericks and updated XCode). Instead, I installed Postgress.app. and called

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
iceui2
  • 887
  • 8
  • 7
3

None of the solutions worked for me, and I didn't want to use MacPorts. Try and download the Postgres App and put it into the Application directory.

Then, specify the location of newly downloaded pg_config, which resides inside the app:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config

If you run in to missing headers problem, try specifying the include directory of the app:

gem install pg -- --with-pg-include='/Applications/Postgres.app/Contents/MacOS/include/'
Yuri
  • 17,422
  • 2
  • 23
  • 25
2

If you are looking for just a quick fix, add the following to your database.yml file:

host: localhost

I had the exact problem, added that line, and now all is well.

0

I had luck following this post from the guys at New-Bamboo:

3 Quick Tips for Coding with OS X 10.9 Mavericks

After installing the xcode dev tools, the third step sorted everything out:

brew tap homebrew/versions && brew install apple-gcc42
brew link --force apple-gcc42
ln -nsf $(which gcc-4.2) /usr/bin/gcc-4.2
purpletonic
  • 1,858
  • 2
  • 18
  • 29
-1

I had this problem the first time I tried to install pg. Through various trials and errors I found something that worked. Thankfully, my notes also worked when Mavericks broke everything.

Hope this helps:

Do not do any brewing.

download the enterpriseDB one-click installer, mount and run. If it doesn't work, run it through the command line (unattended mode) and it will change the memory settings. Restart, run again.

Makes a user called postgres with the password you supply. Also installs pgadmin III.

run this in app directory:

env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-include=/Library/PostgreSQL/9.2/include/ --with-pg-lib=/Library/PostgreSQL/9.2 sudo env ARCHFLAGS='-arch i386' gem install pg/lib/

Open pgadmin3 and create a user and password for rails, then add it to config.

psql [database_name] # to check on your db. Or use pgadmin III

Bernardo
  • 143
  • 8
  • Wanted to downvote this, then changed my mind but cannot seem to undo it.... Anyhow the point is that you SHOULD do brewing over all other hacks and tricks. Brew is a package _manager_, it solves dependencies for you, lets you keep your stuff up to date and also lets you to cleanly uninstall stuff you dont want. – Tanel Suurhans Feb 16 '14 at 18:02