Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't install pg gem on Mountain Lion

I have been having a lot of troubles with Rails after upgrading to Mountain Lion.

Trying to trouble shoot these and searching for hours on the internet so far I have done the following to ensure my system has all the dependencies to run rails after the upgrade:

  1. I updated my Homebrew installation
  2. I updated my RVM installation
  3. I installed the XCode command line tools
  4. I installed GCC as mentioned
  5. I upgraded my version of Ruby to

    ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]

  6. My Rails version is verified as Rails 3.2.6
  7. I also found out that Mountain Lion has some issues with Postgres so I saw this post and found out that I need to add this line to my Bash profile:

    export PATH="/usr/local/bin:/usr/bin:$PATH"
    

Now I go to my Rails app that used to run fine on Snow Leopard before I upgraded, and when I run rails s at the command line I get the following error:

AM@~/Documents/RailsWS/app0815 >gem install pg
 Building native extensions.  This could take a while...
 ERROR:  Error installing pg:
 ERROR: Failed to build gem native extension.
 /Users/AM/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
 checking for pg_config... yes
 Using config values from /usr/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/AM/.rvm/rubies/ruby-1.9.2-p290/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
--without-pg-config
--with-pg_config
--without-pg_config
 /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:368:in `try_do': 
 The complier failed to generate an executable file. (RuntimeError)
 You have to install development tools first.
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:452:in `try_cpp'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:853:in `block in   find_header'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:693:in `block in checking_for'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:280:in `block (2  levels) in postpone'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:254:in `open'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:280:in `block in postpone'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:254:in `open'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:276:in `postpone'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:692:in `checking_for'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:852:in `find_header'
from extconf.rb:41:in `<main>'


 Gem files will remain installed in /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.14.1 for inspection.
 Results logged to /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.14.1/ext/gem_make.out

When I go to the Rails app and try bundle install it fails at pg installation.

like image 693
banditKing Avatar asked Sep 05 '12 02:09

banditKing


2 Answers

Download the postgresSQL installer from http://www.enterprisedb.com/products-services-training/pgdownload#osx

and then make your path look something like this:

PATH=$PATH:/Library/PostgreSQL/9.0/bin/ gem install pg

This would do!

Note: Please specify your postgreSQL installed version correctly in the path.

UPDATE(10/11/2015) (Taken from Heroku)

On OS X with Homebrew:

  1. Install postgresql using:

    brew install postgresql

  2. Then install the gem using:

    gem install pg -- --with-pg-config=/usr/local/bin/pg_config

On OS X with MacPorts:

gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config

Hope this helps!

like image 176
uday Avatar answered Nov 02 '22 16:11

uday


As banditKing says in his answer, need to tell the gem where to find the postgres binaries. You can use the binaries included with the excellent and easy postgres.app from Heroku ( http://postgresapp.com ) to install this gem instead of the EnterpriseDb version. The EnterpriseDB version is great, but an involved install for a dev machine.

Download Postgres.app on your system and add the internal bin directory to your path. I put it in /Applications/Postgres.app, so I used

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

worked for me on 10.8.2, Ruby 1.9.3, pg-0.14.1, and the XCode CLI tools

like image 32
billspat Avatar answered Nov 02 '22 16:11

billspat