110

I'm trying to install the pg gem in order to work again with my rails projects. But I get this error:

Building native extensions. This could take a while... ERROR: Error installing pg: ERROR: Failed to build gem native extension.

/Users/jeanosorio/.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.

Provided configuration options: --with-opt-dir --without-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/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/bin/ruby --with-pg --without-pg --with-pg-config --without-pg-config --with-pg_config --without-pg_config --with-pg-dir --without-pg-dir --with-pg-include --without-pg-include=${pg-dir}/include --with-pg-lib --without-pg-lib=${pg-dir}/

Gem files will remain installed in /Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0 for inspection. Results logged to /Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0/ext/gem_make.out

I tried everything I found on stackoverflow but I still get this error.

If I try to install postgresql using brew I get the following waring:

Warning: postgresql-9.2.4 already installed, it's just not linked

If I try to link

brew link postgresql Linking /usr/local/Cellar/postgresql/9.2.4... Warning: Could not link postgresql. Unlinking...

Error: Could not symlink file: /usr/local/Cellar/postgresql/9.2.4/share/man/man7/WITH.7 /usr/local/share/man/man7 is not writable. You should change its permissions.

Help Please

NOTE: I already installed the command line tools for mavericks.


If I uninstall using homebrew and try to install again, I get this error:

==> Downloading http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.bz2

################################################################## 100.0%

==> Patching patching file src/pl/plpython/Makefile patching file contrib/uuid-ossp/uuid-ossp.c ==> ./configure --prefix=/usr/local/Cellar/postgresql/9.2.4 --datadir=/usr/local/Cellar/postgresql/9.2.4/share/postgresql --docdir=/usr/local/Cellar/p ==> make install-world ==> Caveats

Build Notes

If builds of PostgreSQL 9 are failing and you have version 8.x installed, you may need to remove the previous version first. See:
https://github.com/mxcl/homebrew/issues/issue/2510

Create/Upgrade a Database

If this is your first install, create a database with: initdb /usr/local/var/postgres -E utf8

To migrate existing data from a previous major version (pre-9.2) of PostgreSQL, see:
http://www.postgresql.org/docs/9.2/static/upgrading.html

Loading Extensions

By default, Homebrew builds all available Contrib extensions. To see a list of all available extensions, from the psql command line, run:
SELECT * FROM pg_available_extensions;

To load any of the extension names, navigate to the desired database and run: CREATE EXTENSION [extension name];

For instance, to load the tablefunc extension in the current database, run: CREATE EXTENSION tablefunc;

For more information on the CREATE EXTENSION command, see:
http://www.postgresql.org/docs/9.2/static/sql-createextension.html For more information on extensions, see:
http://www.postgresql.org/docs/9.2/static/contrib.html

Other

Some machines may require provisioning of shared memory:
http://www.postgresql.org/docs/9.2/static/kernel-resources.html#SYSVIPC When installing the postgres gem, including ARCHFLAGS is recommended: ARCHFLAGS="-arch x86_64" gem install pg

To install gems without sudo, see the Homebrew wiki.

To have launchd start postgresql at login: ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents Then to load postgresql now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Or, if you don't want/need launchctl, you can just run: pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start Warning: Could not link postgresql. Unlinking... Error: The brew link step did not complete successfully The formula built, but is not symlinked into /usr/local You can try again using `brew link postgresql' ==> Summary /usr/local/Cellar/postgresql/9.2.4: 2831 files, 38M, built in 4.9 minutes


SOLUTION:

I execute this command in order to change the permission of the folder:

sudo chown jeanosorio /usr/local/share/man/man7

Then

brew link postgresql Linking /usr/local/Cellar/postgresql/9.3.1... 421 symlinks created

And finally:

sudo ARCHFLAGS="-arch x86_64" gem install pg

Fetching: pg-0.17.0.gem (100%) Building native extensions. This could take a while... Successfully installed pg-0.17.0

Jean
  • 5,201
  • 11
  • 51
  • 87

27 Answers27

267

If you want to avoid using MacPorts, you can download the Postgres App and place it into the Application directory.

Then, specify the location of newly downloaded pg_config:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/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/Versions/latest/include/'
Spajus
  • 7,356
  • 2
  • 25
  • 26
Yuri
  • 17,422
  • 2
  • 23
  • 25
  • 11
    Just a note that you can still use bundler by setting a config env variable: `export CONFIGURE_ARGS="with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config"` – film42 Nov 16 '13 at 17:47
  • 8
    If you are using Postgres 9.3 (latest as of today), you can do this: ```gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config``` – coderberry Dec 14 '13 at 14:23
  • Using Postgres93.app worked but had to add the bin path to PATH env variable to bundle install. – mkhatib Dec 20 '13 at 07:34
  • 6
    Had to get this working using `gem install pg -- --with-pg-config=/Applications//Postgres.app/Contents/Versions/9.3/bin/pg_config` – StevenNunez Feb 25 '14 at 00:37
  • 1
    Thanks! i was getting Warning: Could not link postgresql. Unlinking... Error: Could not symlink file:... is not writable. You should change its permissions. And everything is brew fault,after i brew install/link postgresql The installation goes to: Linking /usr/local/Cellar/postgresql/9.3.4... which is in my case a VERY CONFLICTIVE FOLDER, downloading pg and gem install using it solve the problem THANKS!!!!!! – d1jhoni1b Apr 15 '14 at 03:21
  • I had to do this: `sudo su` and `env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config` – frin Jul 16 '14 at 08:49
  • Just a quick note, as the `pg` versions increment, so will this command as it's looking within a specific folder in the app's contents. The default version of `pg` at the time of writing this is `9.4`, so beware. You'll see the same errors on `gem install` as you did before because you're not providing it with header files. – Mattygabe Oct 21 '15 at 15:15
  • I work with multiple ruby versions, so I found it inconvenient to always add the `--with-pg-config` option at the end (and I would like it to work with the `bundle` command). I found that adding a symlink works well. `sudo ln -s /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config /usr/local/bin` – parasquid Jun 29 '17 at 06:18
  • If you go down the Postgres App route, I'd also suggest to update your PATH to include all the pg commands such as dumpdb. To update your path: sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp – Ryan Sep 28 '17 at 20:53
  • There should be an easier way to do this after all these years – dsp_099 May 12 '18 at 18:51
  • 1
    ```export CONFIGURE_ARGS=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config``` – milushov Jun 03 '20 at 06:10
  • Awesome, this worked for me on OSX Catalina 10.15.6, gem pg:1.2.3 with PostgreSQL 12 installed. – rrrrrraul Aug 17 '20 at 03:13
78

Use brew to get postgresql

brew install postgresql

Check you have pg_config in the installed brew. I found mine in

/usr/local/Cellar/postgresql/9.3.3/bin/pg_config

Check via :

$ ls /usr/local/Cellar/postgresql/9.3.3/bin/pg_config
> /usr/local/Cellar/postgresql/9.3.3/bin/pg_config

Once done, install the pg gem with

env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.3/bin/pg_config
Ayush Goel
  • 3,134
  • 27
  • 36
  • 2
    worked for me after trying to many different things. i just had to make sure to use the correct version number (9.3.5_1 in my case)! – H K Oct 26 '14 at 17:33
  • 1
    Use `ls /usr/local/Cellar/postgresql/9.*/bin/pg_config` to find the real path and combined `env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=$(ls /usr/local/Cellar/postgresql/9.*/bin/pg_config)` – Jesper Grann Laursen Oct 16 '15 at 09:57
  • A bit more detailed description about what happens here https://deveiate.org/code/pg/README-OS_X_rdoc.html – Julia Usanova Apr 05 '18 at 11:54
37

I wasn't able to install postgres via MacPorts. Instead, I installed Postgress.app. and called

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

Note: in newer versions (in 9.3 at least) the path is actually: /Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

Kieran Senior
  • 17,960
  • 26
  • 94
  • 138
iceui2
  • 887
  • 8
  • 7
  • 2
    This works, but note that sometimes Postgres.app is installed as Postgres.app where NN is the version number. – nimrodm Dec 09 '13 at 19:47
12

If you have homebrew, just type:

$ brew install postgresql

user2373148
  • 463
  • 6
  • 9
  • 1
    This worked for me - I had PostgreSQL already installed, but I guess I had to install it again, to refresh the headers(?) Then I could do `gem install pg` – ckhatton Feb 19 '14 at 12:55
11

For me, on Yosemite, I didn't need to specify pg_config path. The following got it done.

brew install postgresql
sudo env ARCHFLAGS="-arch x86_64" gem install pg

Seems like architecture flag is the key.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
dinigo
  • 6,872
  • 4
  • 37
  • 48
10

Here's another alternative in case you want to use the postgresql installer directly. There's a couple of hoops you gotta jump through to begin with after updating to mavericks. Here's what I did:

Install the xcode command line tools first:

xcode-select --install

Download and install the latest version of PostgreSQL (9.3.1), in my case I just used the graphical installer. Here's the link to the downloads page:

http://www.enterprisedb.com/products-services-training/pgdownload#osx

Just choose all of the defaults it gives you. It my case it installed postgres to the following directory, if you installed it to a different directory, just remember the path you chose, because you'll need it shortly.

/Library/PostgreSQL/9.3

If you now try and install the latest pg gem (0.17.0) you'll need to pass a couple of options on the command line. This is what I used:

ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config

If you installed postgres to a different location then you'll need to fix up the path to the pg_config file.

If it complains of a missing 'libpq.5.dylib' file while trying install the pg gem you'll need to create a symlink pointing to the actual location of this file. Postgres is looking for it in your /usr/local/lib/ directory so create a symlink in there and redirect it to its actual location. In my case I had to run:

sudo ln -s /Library/PostgreSQL/9.3/lib/libpq.5.dylib /usr/local/lib/libpq.5.dylib

Once you've done that, retry installing the gem again and hopefully it will work for you the same way it did for me.

2potatocakes
  • 2,240
  • 15
  • 20
5

In my case (i needed PG 0.16.0 on Mavericks), i installed postgresql via MacPorts

sudo port install postgresql90

and then

gem install pg -v '0.16.0' -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config

For the latest version you need to deduct -v '0.16.0'

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

If you have Homebrew installed do not install MacPorts before you read about their coexistense

Community
  • 1
  • 1
Gi0
  • 182
  • 2
  • 8
5

Place the missing --with-pg-config in Bundler's config file: /Users/<your_user>/.bundle/config to make your life easier.

---
BUNDLE_BUILD__PG: --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
BUNDLE_BUILD__EXAMPLE_GEM: --other-configs
BUNDLE_BUILD__OTHER_EXAMPLE_GEM: --other-configs

Then run bundle install again.

Bruno Buccolo
  • 1,111
  • 9
  • 20
5

That works for me! (Postgres 93, mac ox 10.9.1)1. download @ http://postgresapp.com/ and than

gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
meck373
  • 1,114
  • 1
  • 18
  • 31
4

I was stuck on my bundle install for 3 days. Tried Everything like adding env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.5_1/bin/pg_config

I was able to see pg gem getting installed after this command but still it was not installing from bundle install, which was a pain because I dint know what to write in Gemfile except gem 'pg'

The thing which finally worked for me was to find that my pg_config was in /Library/PostgreSQL/9.3/bin/pg_config and by default the Gemfile bundle install looks in /usr/local/bin/pg_config

I just ran the following command and magic happened. bundle config build.pg --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config

Neeraj Gupta
  • 341
  • 2
  • 9
4

In my case it didn't work if you don't specify the arch flags, OS X 10.10.3 and postersApp 9.4

sudo env ARCHFLAGS="-arch x86_64" gem install pg -- \--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
Rachidi Mohamed
  • 166
  • 1
  • 7
3

The easiest way for me to resolve this was to use Postgres.app and set up my env vars so gem install pg just works (instead of requiring the --with-pg-config flag).

Using Homebrew Cask,

brew cask install postgres
echo 'export PG_HOME=/Applications/Postgres.app/Contents/Versions/latest' >> ~/.bash_profile
echo 'export PATH="$PATH:$PG_HOME/bin"' >> ~/.bash_profile
source ~/.bash_profile

# now it just works
gem install pg
smoll
  • 121
  • 1
  • 6
2

This is what it worked for me:

gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
tokhi
  • 21,044
  • 23
  • 95
  • 105
2

In my case, I uninstalled the version installed via homebrew and switched to Postgres.app (v9.3.4.2 at the time of writing).

It only seemed to work when prepending the environment architecture flags and specifying the path to pg_config. Your milage might vary, so this answer could help with variations on pg_config's location.

Here is the final, complete command that worked for me:

env ARCHFLAGS="-arch x86_64" gem install pg -- \
--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config 
Community
  • 1
  • 1
Marius Butuc
  • 17,781
  • 22
  • 77
  • 111
2

sudo ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config

Taysky
  • 4,331
  • 2
  • 20
  • 28
  • this also worked for me with bundle install like so: `ARCHFLAGS="-arch x86_64" bundle install` – d4n3 Jan 13 '15 at 16:24
2

I had installed postgres using homebrew on mac. Used below command to find the location

which postgres
/usr/local/opt/postgresql@12/bin/postgres

Installed gem using below command. In the above path just replace the postgres with pg_config since command requires path for pg_config.

gem install pg -- --with-pg-config=/usr/local/opt/postgresql@12/bin/pg_config
Dave
  • 4,376
  • 3
  • 24
  • 37
1

I was getting the same error when running bundle install regarding the pg gem (using Mac OS X Mavericks). After hours of researching, I found the solution. I installed postgres using homebrew

brew install postgres

After installing that, I created a new rails app using postgres. I ran

bundle install

to no avail (got same error as the question). I used

which psql

to figure out where my postgres was installed, it returned

/usr/local/bin/psql

before running bundle install again, I had to change the global path to build.pg by running the following

bundle config build.pg --with-pg-config=/usr/local/bin/pg_config

then I ran bundle install again and voila! I used the postgres where brew installed it.

Rav Johal
  • 374
  • 2
  • 14
1

What worked for me on 10.9.3 with Xcode 5.1.1 was the following:

brew update
brew install postgresql 
gem install pg -v '0.17.1'

I needed pg 0.17.1

bogen
  • 9,954
  • 9
  • 50
  • 89
Tom Andersen
  • 7,132
  • 3
  • 38
  • 55
1

On OS X Mavericks with latest Postgres App do the following with sudo privileges

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

For me, the error message looked like this:

Installing pg 0.18.4 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

...

checking for pg_config... no

No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config

As the error message shows, it's trying to execute the command/script "pg_config". So just add it to the path from wherever your Postgres installation is. In my case, I did:

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

then

bundle

That's it.

paneer_tikka
  • 6,232
  • 1
  • 20
  • 17
1

For any lost soul outthere still having this issue with no resolution and have a brew installed ruby and postgres. Here's the following steps to make sure of:

  • Make sure your version of ruby wasn't installed with the --universal flag.
    • This was the hardest thing to figure out, I had --universal which was the final root of the problem.
  • Make sure you have initialized postgresql properly with initdb and createdb commands (look elsewhere for steps to do this).
  • Make sure Xcode (Optional) and command line tools is installed; on the terminal:
    • xcode-select --install
    • sudo xcodebuild -license (if Xcode installed) or open the application and accept the license agreement.
    • Reboot Computer
  • which ruby, which gem and which postgres should show /usr/local/bin/ruby, /usr/local/bin/gem and /usr/local/bin/postgres respectfully.
  • On the terminal run:

    • gem install pg -- --with-pg-config=/usr/local/bin/pg_config
    • or
    • env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config
  • Making sure all these steps are satisfied I was able to solve the following errors:

    • ruby.h header error
    • developer tools installed first error
link_boy
  • 1,025
  • 4
  • 12
  • 23
1

First of all find pg_config location

sudo find / -name "pg_config" -print

The answer is /Library/PostgreSQL/9.1/bin/pg_config in my configuration (MAC Maverick)

Then try installing something like below

gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config

if this does not work then try to check in what way you installed postgresql

brew/mac port/setup

then you have to try relevant option to the same.

Thank you.

Ram

Rameshwar Vyevhare
  • 2,699
  • 2
  • 28
  • 34
0

I was with the same problem, this was the solution I've found.

If you run brew doctor, it will tell you that probably you have installed something without brew that have changed the permissions on some folders, and so, you need to change permissions back to you on that folder.

Inside /usr/local/share/man, what you can do is the following:

sudo chown [yourusername] man7

And then:

brew link postgres

Hope it helps!

Gustavo Semião-Lobo
  • 2,468
  • 3
  • 18
  • 26
0

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'

Shannon Perkins
  • 353
  • 4
  • 12
0

Postgres 9.4:

gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/Versions/9.4/bin/pg_config
MDD
  • 144
  • 8
0
sudo su 

then

ARCHFLAGS="-arch x86_64" gem install pg -v '0.18'

worked on 10.10.2

Keaton Burleson
  • 498
  • 3
  • 16
0

If after trying everything here it still won't install, try opening Xcode and accepting the license agreement.

hattenn
  • 4,371
  • 9
  • 41
  • 80