89

I just installed Rails 4.0.2 and when creating a new app, in the bundle stage I get:

Installing pg (0.17.1) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/Dee/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb 
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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.

How do I fix this?

Deekor
  • 9,144
  • 16
  • 69
  • 121
  • 1
    You may want to add an osx tag. The solution appears that it may be related specifically to your OS. – Vince Dec 24 '13 at 02:58
  • You might want to visit . http://stackoverflow.com/questions/3116015/how-to-install-gem-pg-on-ubuntu . Hope one of those solutions help – Yana Agun Siswanto Dec 24 '13 at 03:37

17 Answers17

188

Some kind of error resported here Installing PG gem on OS X - failure to build native extension

To install dependencies on Ubuntu try this:

sudo apt-get install libpq-dev

and this

gem install pg
Community
  • 1
  • 1
Talgat Medetbekov
  • 3,741
  • 1
  • 19
  • 26
146

I'm on a Mac running Mavericks. My solution was to install Postgres.

And then in terminal install using homebrew with the configuration:

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

Note: This answer has been edited to use the latest symlink that is currently included in shipping versions of the Postgres app.

Previous versions suggested:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
LunaCodeGirl
  • 5,432
  • 6
  • 30
  • 36
Bob O'Brien
  • 1,583
  • 1
  • 11
  • 6
  • Yup mine was in a different location as well. Thanks for the answer. – Deekor Apr 11 '14 at 00:07
  • Correct Bob, the version of postgresapp in which this worked for me, was Version 9.2.2.0, so they must have changed this location on the 9.3 release. – Robertibiris Apr 22 '14 at 13:20
  • 1
    And now, as of 1/22/15 we're looking at the 9.4 release, it's good to see they're keeping it updated -- "gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config" – corprew Jan 22 '15 at 22:58
  • 3
    I was in a rush and missed the part of this answer that says "install using home-brew". This answer worked for me after I did `brew update; brew install postgres` – Dennis Feb 26 '15 at 16:28
  • 6
    In your solution, please add the version, other the latest PG gem will install: `gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config` – user1322092 Mar 07 '15 at 15:59
  • In my case (Linux Mint 17.1), install libpq-dev via apt-get made it. – saiyancoder Mar 11 '15 at 17:18
  • This worked for me while trying to `bundle install` gems included in the Ruby Getting Started Heroku walkthrough: https://devcenter.heroku.com/articles/getting-started-with-ruby#run-the-app-locally – ctlockey Apr 10 '18 at 17:57
23

app root:

  1. brew update
  2. brew install postgres
  3. gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.4/bin/pg_config
  4. bundle install
  5. ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
  6. launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  7. createuser -s -r postgres
  8. rake db:create:all
  9. rake db:migrate
  10. rails s

NOTE: replace the version number in step 3 if needed.

knice
  • 391
  • 2
  • 8
11

Previously working answer with older version

I installed under mac OSX Mavericks, having the postgres app (Version 9.2.2.0 ) from www.postgresapp.com installed. The underlying problem was simpy that the since postgres was installed via the app, the configuration file resides on a location which is not the default one when installing it without postgressapp. so we need to tell gem where to find this file by:

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

Hope it helps

Community
  • 1
  • 1
Robertibiris
  • 844
  • 8
  • 15
5

If gem install pg fails, try the following command:

env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config

... from the PostgreSQL.app Documentation

Vince
  • 3,962
  • 3
  • 33
  • 58
  • 2
    Since the official app recently changed names to "Postgres93.app", the command should be: `env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config` – Blackcoat Jan 19 '14 at 20:49
  • 1
    for me it worked with: `env ARCHFLAGS="-arch x86_64" gem install pg` – dnlcrl May 13 '14 at 23:02
4

Looks like you do not have PostgreSQL installed. The pg gem requires some headers from PostgreSQL to compile native extension.

tungd
  • 14,467
  • 5
  • 41
  • 45
  • How about your Ruby/Bundler setup? You're using Mac OS X or Linux? and if you're using Mac OS X, you installed PostgreSQL via `homebrew` or PostgreSQL.app? – tungd Dec 24 '13 at 02:44
  • Im on a mac. I installed via the app – Deekor Dec 24 '13 at 02:46
  • I have 0.17.0 installed. Any harm in using that with rails 4? – Deekor Dec 24 '13 at 02:46
  • Then try running `bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config` and run `bundle` again. Adjust the config path if you install the app to `~/Application` rather than `/Application` – tungd Dec 24 '13 at 02:47
3

I had to combine everything and use

sudo env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
Mark Wang
  • 342
  • 3
  • 11
3

If you are using something other than Postgres in development and Postgres in production only, you can add the pg gem to your gemfile like so...

group :production do
  gem 'pg',             '0.17.1'
end

Then use bundle install --without production

j8d
  • 446
  • 7
  • 23
3

For CentOS users:

sudo yum install postgresql-devel

and

gem install pg
webster
  • 3,902
  • 6
  • 37
  • 59
1

I have just set up a new Macbook Pro which was prebuilt with Catalina.

What worked for me:

  1. Install the Postgres from here: https://postgresapp.com/
  2. Add export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"to your .zshenv, since Z shell is now the default terminal.
Henrik Holm
  • 617
  • 2
  • 6
  • 13
1

On mac this command worked for me.

gem install pg -v '0.18.4' -- --with-cflags="-Wno-error=implicit-function-declaration"

vikas95prasad
  • 1,234
  • 1
  • 12
  • 37
0

After installing Postgres I had to run the following command

env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

After this bundle install works great!

Hope it helps

pochocosta
  • 13
  • 3
0

The way I managed to get past that error was:

  • cd to app folder and then set the ruby version locally. I'm using ruby 2.1.2.

rbenv local 2.1.2

  • instead of just running bundle install, install the gems in vendor/bundle

bundle install --path vendor/bundle

This did it for me.

0

I needed to use sudo

sudo gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config
gypsyDev
  • 1,232
  • 1
  • 14
  • 22
0

I was having a problem with Amazon and couldn't use apt-get. For my worked:

    sudo yum install postgresql-devel

then:

    bundle install

and try again:

    rails serve
Maurici Abad
  • 1,012
  • 9
  • 20
0

If you installed through Homebrew; gem install pg -- --with-pg-config=/usr/local/bin/pg_config. Works with Ruby 2.4.6 and pg 0.20.0.

zykadelic
  • 1,083
  • 11
  • 19
-2

It works for me

rvm list gemsets
rvm use ruby-2.4.1
bundle

error[![resolved with rvm use ruby]2