201

It seems many others have had problems installing the pg gem. None of the solutions posed for others have worked for me.

I have tried to install the pg gem and postgres.app. The pg gem won't install. The first error I get is:

An error occurred while installing pg (0.17.0), and Bundler cannot continue. Make sure that gem install pg -v '0.17.0' succeeds before bundling.

The installation advice about pointing my gem install to the config for pg fails with the following error message (which many others on this forum have encountered):

Failed to build native extensions... Results logged to /Users/melanie/.rvm/gems/ruby-1.9.3-p448/gems/pg-0.17.0/ext/gem_make.out

I don't know how to find or access this log file to search for further clues.

I also get an error message (command not found) when I try using the sudo apt-get install command. I've scoured this forum for the last 6 hours, trying each piece of advice to get pg working with my rails project.

I can't find advice about how to change a path, or specifically, what change is required. My which pg_config returns a file source. I've used that with a command to install pg using that config. It fails.

There are so many people that have had trouble with this. Many answers suggest homebrew. I've had to remove that because it threw up other issues.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Mel
  • 2,481
  • 26
  • 113
  • 273
  • 1
    Hey, a couple questions that will help down the road: - You're on Mac, right? Right went wrong with homebrew? (ps, as you noted, apt-get is just for Ubuntu (and its Debian-based cousins)) - what postgres version do you have? try running `psql -v` in your shell. - what ruby version are you running? It looks like 1.9.3, but what does `ruby -v` give you? – rmosolgo Oct 09 '13 at 03:40
  • I should add, that my psql is added in: /Applications/Postgres-1.app/Contents/MacOS/bin/psql ; exit; psql (9.3.0) but which psql returns: usr/bin/psql – Mel Oct 09 '13 at 03:47
  • Hi, thanks very much for looking at this. My psql is v 9.3.0 and ruby is v 1.9.3 – Mel Oct 09 '13 at 03:48
  • 1
    See @paninapress's answer for Postgres.app. It worked great for me and doesn't require installing another instance of Postgres. – Jamon Holmgren Sep 30 '14 at 05:49

15 Answers15

480

Same error for me and I didn't experience it until I downloaded OS X 10.9 (Mavericks). Sigh, another OS upgrade headache.

Here's how I fixed it (with homebrew):

  • Install another build of Xcode Tools (typing brew update in the terminal will prompt you to update the Xcode build tools)
  • brew update
  • brew install postgresql

After that gem install pg worked for me.

wpp
  • 7,093
  • 4
  • 33
  • 65
shicholas
  • 6,123
  • 3
  • 27
  • 38
234

If you are using Ubuntu try to install following lib file

sudo apt-get install libpq-dev

and then

gem install pg

worked for me.

Cfreak
  • 19,191
  • 6
  • 49
  • 60
sandesh kota
  • 2,491
  • 1
  • 11
  • 11
50

I am using OS X Mavericks (version 10.9)

and when I run the above I got the following message: If builds of PostgreSQL 9 are failing and you have version 8.x installed.

So I run the following command:

ARCHFLAGS="-arch x86_64" gem install pg

and this worked for me, I hope this helps someone :)

Jon
  • 1,328
  • 1
  • 11
  • 11
  • 2
    +1, I tried others, but finally I got pg installed by gem with this solution. – BMW Jul 06 '14 at 11:11
  • 1
    +1, likewise, this was the solution for me as well, where my postgresql was already installed via brew. Thanks Jon for the post. – Jason Hoekstra Aug 07 '14 at 01:01
  • 5
    n.b. If installing as root (for whatever reason), sudo goes *before* the variable: `sudo ARCHFLAGS="-arch x86_64" gem install pg` – Noach Magedman Aug 12 '14 at 21:40
  • Actually, I need this every time I run `bundle install` so I've put it inside `~/.bash_profile` as an export :) – Ja͢ck Aug 25 '15 at 13:14
47

Ok I also had this problem (psql is v 9.3.0 and ruby is v 2.1.2) and the solution that worked for me was setting the bundle config settings first:

bundle config build.pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

This answer finally helped me figure it out: https://stackoverflow.com/a/9235107/3546680

Community
  • 1
  • 1
paninapress
  • 491
  • 4
  • 6
  • 6
    This method is **preferred** if you use postgres.app. Using Homebrew may fix the native extension problem, but having two postgres applications will create conflicts (like port allocation) See also Sayanees' answer at http://stackoverflow.com/questions/19569031/error-failed-to-build-gem-native-extension-on-mavericks – Epigene Sep 05 '14 at 09:38
  • 4
    This is definitely the right answer for Postgres.app. – Jamon Holmgren Sep 30 '14 at 05:48
  • Works well on Mojave, tried successfully few minutes ago. – microspino May 08 '19 at 10:40
23

I tried everything for hours but the following finally fixed it (I'm on OS X 10.9.4):

  1. Install Xcode command line tools (Apple Developer site)
  2. brew uninstall postgresql
  3. brew install postgresql
  4. ARCHFLAGS="-arch x86_64" gem install pg
Ali
  • 7,297
  • 2
  • 19
  • 19
  • Thank You. I had use sudo for step 4, is that a bad thing? How can I avoid it? – Roy Sep 18 '15 at 19:12
  • @Roy - glad to help :) - should be fine, probably want to avoid using sudo as I believe it will install it for all users, but if you're the only user on the system than its probably not that big of a deal. – Ali Sep 20 '15 at 17:02
  • `brew uninstall postgresql` and `brew install postgresql` are not necessary steps on OS X 10.10 and 10.11. Just `gem uninstall pg` (and remove all versions), then make sure you `bundle` or `gem install pg` again, and native extensions will be rebuilt with the new command line tools for this OS X version. – Olivier Lacan Sep 25 '15 at 21:56
8

easy step

  1. brew install postgresql
  2. gem install pg -v 'your version'
James Dela Cruz
  • 106
  • 1
  • 2
6

Similarly, after installing Mavericks bundle update was throwing an error on the pg gem, which is only used on production and not locally.

I use Brew to manage my packages and postgresql was already installed, but still I was getting the 'no pg_config' error.

The fix was to just brew uninstall postgresql, then brew install postgresql. After which I was immediately able to successfully run bundle update.

Marius Butuc
  • 17,781
  • 22
  • 77
  • 111
Shannon Perkins
  • 353
  • 4
  • 12
6

I believe the “correct” answer would be to first configure PATH correctly for Postgres.app by adding the following to ~/.profile (.zshrc or ~/.zprofile if using ZSH):

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin

Then open a new tab or window in terminal and install the pg gem with:

ARCHFLAGS="-arch x86_64" gem install pg

Documented here:

Matias Korhonen
  • 886
  • 2
  • 8
  • 21
6

On OSX with Postgres installed in /Applications, I simply run the following command (change 0.20 & 9.4 according to your version)

gem install pg -v '0.20' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config

You should have :

Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config' This could take a while... Successfully installed pg-0.20.

jbheren
  • 516
  • 3
  • 11
4

running brew update and then brew install postgresql worked for me, I was able to run the pg gem file no problem after that.

3

I spent a day on this and here's how I got it fixed:

I found that global value of build.pg was set to: /opt/local/lib/postgresql91/bin/pg_config and that was not where postgres was installed.

I fixed it with replacing the value of build.pg to: bundle config build.pg --with-pg-config=/usr/local/Cellar/postgresql/9.4.4/bin/pg_config which is where my postgresql installation is.

HosseinToussi
  • 31
  • 1
  • 3
2

Try:

gem install pg -- --with-pg-config=`which pg_config`
Gerry
  • 10,584
  • 4
  • 41
  • 49
2

Solved! I found some lack of library for PostgreSQL on the system. Only two steps solved it:

brew install postgresql

Then run

gem install pg

Esmaeil MIRZAEE
  • 1,110
  • 11
  • 14
1

For those who are not interested to use brew.

  1. Download PostgreSQL application.
  2. Follow the macOS default instruction to install it.
  3. It is advisable to run PostgreSQl.
  4. Run gem install pg -- --with-pg-config=/path/to/postgress/in/your/applications/folder/`
    • For example, in my machine it is /Applications/Postgres.app/Contents/Versions/12/bin/pg_config
Esmaeil MIRZAEE
  • 1,110
  • 11
  • 14
0

i got same problem and i solved

gem update --system 3.0.6