255

I tried using gem install pg but it doesn't seem to work.

gem install pg gives this error

Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

C:/Ruby/bin/ruby.exe 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=C:/Ruby/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


Gem files will remain installed in C:/Ruby/lib/ruby/gems/1.8/gems/pg-0.10.1 for
inspection.
Results logged to C:/Ruby/lib/ruby/gems/1.8/gems/pg-0.10.1/ext/gem_make.out
Matt
  • 14,906
  • 27
  • 99
  • 149
Rohit
  • 5,631
  • 4
  • 31
  • 59
  • 2
    @NatchiQ broken link? – kingsfoil May 13 '17 at 04:59
  • 1
    in my case the error log said `libpq` was not found, so i installed `sudo apt install postgresql postgresql-contrib libpq-dev pgadmin3 -y` – Raj Nov 27 '19 at 09:39
  • 1
    Try opening a new terminal tab and trying again. That worked for me. I had just installed psql, and the window I was trying to install the gem in didn't have psql in its path. – Jared Menard Nov 10 '21 at 20:54

25 Answers25

436

I had this problem, this worked for me:

Install the postgresql-devel package, this will solve the issue of pg_config missing.

sudo apt-get install libpq-dev
Devaroop
  • 7,900
  • 2
  • 37
  • 34
110

Issue is gem dependency so before installing pg make sure you have installed "libpq-dev"

Ubuntu systems:

sudo apt-get install libpq-dev

RHEL systems:

yum install postgresql-devel

Mac:

brew install postgresql

umishra
  • 390
  • 2
  • 14
Mahattam
  • 5,405
  • 4
  • 23
  • 33
  • 9
    The answer for Mac is true. It's just because we haven't installed PostgreSQL on the machine. – Hoang Le Jun 09 '15 at 16:52
  • On Centos 7 `yum install postgresql-devel` solved my error related to pg_config for installing the 'pg' .gem. By the way I opted to use the just released PostgreSQL 10 – Arthur Nov 05 '17 at 02:42
69

gem install pg -- --with-pg-config=/usr/pgsql-9.1/bin/pg_config

Flaviu
  • 6,240
  • 4
  • 35
  • 33
  • 3
    This helped me (on Centos 6.2) – TuK Sep 23 '12 at 16:47
  • 14
    Worked for me on OS X, but with path to /Applications/Postgres.app/Contents/MacOS/bin/pg_config (I have a standalone Postgres.app) – Matt Nov 15 '13 at 19:13
  • 10
    `gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config ` on yosemite with Postgres App – skilleo Feb 11 '15 at 20:26
  • Worked. But on Mac, preceding with `env ARCHFLAGS="-arch x86_64"` was a game changer for me. – Janusz Lenar Mar 29 '16 at 12:38
  • Worked for me on OS X 10.11 with psql app 9.6 and gem version 0.18.4 `gem install pg -v '0.18.4' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.6/bin/pg_config` – HarlemSquirrel Nov 15 '16 at 17:34
  • 3
    I used `gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config` for OSX 10.12 – Tim Krins Nov 08 '17 at 18:26
  • This help me on OS X with no brew support (using postgres installer) – racar Feb 06 '20 at 03:39
  • macOS Monterey M1. Added to .zshrc `export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/14/bin/` and `gem install pg` worked and could then `bundle install in Rails – Greg Jan 05 '22 at 01:45
46

@Winfield said it:

The pg gem requires the postgresql client libraries to bind against. This error usually means it can't find your Postgres libraries. Either you don't have them installed or you may need to pass the --with-pg-dir= to your gem install.

More than that, you only need --with-pg-config= to install it.

On a Mac

If, by any chance, you also installed postgres through the website bundle on mac, it will be on somewhere like /Applications/Postgres.app/Contents/Versions/9.3/bin.

So, either you pass it on the gem install:

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

Or you set the PATH properly. Since that might be too much, to temporarily set the PATH:

export PATH=%PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin/
Community
  • 1
  • 1
cregox
  • 17,674
  • 15
  • 85
  • 116
  • 3
    for specific version: `gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config` – Dan Sandland Mar 24 '15 at 19:06
  • There is a symlink folder called `latest` inside of the Postgres.app contents folder that is useful in case 9.3 is no longer shipped. ```gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config``` – Stephen Silber Jun 25 '18 at 16:27
29

This worked in my case:

sudo apt-get install libpq-dev

I used:

  • Ubuntu 14.04.2 LTS
  • Ruby 2.2.2
  • Rails 4.2.1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Zainal
  • 410
  • 5
  • 9
25

I hadn't postgresql installed, so I just installed it using

sudo apt-get install postgresql postgresql-server-dev-9.1

on Ubuntu 12.04.

This solved it.


Update:

Use the latest version:

sudo apt-get install postgresql-9.3 postgresql-server-dev-9.3
Lasse Skindstad Ebert
  • 3,370
  • 2
  • 29
  • 28
  • Fixed the problem for me but on a mac (with homebrew) I had to run this command: `brew install postgresql` – gMale Jul 19 '14 at 19:45
14

If you are using Postgres.app on Mac, you may resolve this issue once and for all like this:

First gem uninstall pg, then edit your ~/.bash_profile or ~/.zshrc file or equivalent and add:

# PostgreSQL bin path
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin

Then bundle install and gem install pg should both work as expected.

Darme
  • 6,984
  • 5
  • 37
  • 52
  • Thanks, it also works using brew installed postgreSQL # PostgreSQL bin path export PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/ – Benoit Jan 21 '16 at 07:56
  • Thanks! I switched from brew installed postgres to Postgress.app and had this problem – luigi7up Feb 13 '21 at 16:35
14

Answered here: Can't install pg gem on Windows

There is no Windows native version of latest release of pg (0.10.0) released yesterday, but if you install 0.9.0 it should install binaries without issues.

Community
  • 1
  • 1
Nikita Barsukov
  • 2,957
  • 3
  • 32
  • 39
11

On macOS (El Capitan). You can simply use: brew install postgresql

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Jones Agyemang
  • 1,230
  • 16
  • 15
10
$ PATH=$PATH:/Library/PostgreSQL/9.1/bin sudo gem install pg

replace the 9.1 for the version installed on your system.

jstnno
  • 1,035
  • 1
  • 11
  • 13
4
  • Ubuntu 20.10 (pop!_os)
  • Ruby 2.7.2
  • Rails 3.1.0
  • Postgresql 12

Uninstall and then reinstall postgresql-client libpq5 libpq-dev

sudo apt remove postgresql-client libpq5 libpq-dev
sudo apt install postgresql-client libpq5 libpq-dev

Then install the pg gem again pointing at /usr/lib to find the pg library:

gem install pg  --   --with-pg-lib=/usr/lib

Output (what you should see after the previous command):

Building native extensions with: '--with-pg-lib=/usr/lib'
This could take a while...
Successfully installed pg-1.2.3
Parsing documentation for pg-1.2.3
Installing ri documentation for pg-1.2.3
Done installing documentation for pg after 1 seconds
1 gem installed

Gem should install, then continue with normal bundle install or update:

bundle
bundle install
bundle update
Corey
  • 835
  • 1
  • 9
  • 32
3

For Mac Users

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

This should do the trick

Ronak Jain
  • 1,723
  • 2
  • 24
  • 35
3

Use with ARCH flag.

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

This resolved the same issue you are having.

yeyeyerman
  • 7,751
  • 7
  • 43
  • 52
Aman Chhabra
  • 607
  • 1
  • 8
  • 21
3

The pg gem requires the postgresql client libraries to bind against. This error usually means it can't find your Postgres libraries. Either you don't have them installed or you may need to pass the --with-pg-dir= to your gem install.

Winfield
  • 18,985
  • 3
  • 52
  • 65
2

I'd this issue on Linux Mint (Maya) 13, And I fixed it by Installing postgresql and postgresql-server :

apt-get install postgresql-9.1 

sudo apt-get install postgresql-server-dev-9.1
SAIDI Belkacem
  • 338
  • 4
  • 13
2

Regardless of what OS you are running, look at the logs file of the "Makefile" to see what is going on, instead of blindly installing stuff.

In my case, MAC OS, the log file is here:

/Users/za/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-1.0.0/mkmf.log

The logs indicated that the make file could not be created because of the following:

Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers

Inside the mkmf.log, you will see that it could not find required libraries, to finish the build.

checking for pg_config... no
Can't find the 'libpq-fe.h header
blah blah

After running "brew install postgresql", I can see all required libraries being there:

za:myapp za$ cat /Users/za/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-1.0.0/mkmf.log | grep yes
find_executable: checking for pg_config... -------------------- yes
find_header: checking for libpq-fe.h... -------------------- yes
find_header: checking for libpq/libpq-fs.h... -------------------- yes
find_header: checking for pg_config_manual.h... -------------------- yes
have_library: checking for PQconnectdb() in -lpq... -------------------- yes
have_func: checking for PQsetSingleRowMode()... -------------------- yes
have_func: checking for PQconninfo()... -------------------- yes
have_func: checking for PQsslAttribute()... -------------------- yes
have_func: checking for PQencryptPasswordConn()... -------------------- yes
have_const: checking for PG_DIAG_TABLE_NAME in libpq-fe.h... -------------------- yes
have_header: checking for unistd.h... -------------------- yes
have_header: checking for inttypes.h... -------------------- yes
checking for C99 variable length arrays... -------------------- yes
z atef
  • 7,138
  • 3
  • 55
  • 50
2

I've been experiencing this annoying problem with PG for years. I created this gist to help.

The following command always work for me.

# Substitute Postgres.app/Contents/Versions/9.5 with appropriate version number
sudo ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config

gist: https://gist.github.com/sharnie/5588340cf023fb177c8d

SharnieIvery
  • 141
  • 2
  • 6
1

I had to do this on CentOS 5.8. Running bundle install kept causing issues since I couldn't force it to use a particular PG version.

I can't yum erase postgresql postgresql-devel either, because of dependency issues (it would remove php, http etc)

The solution? Mess $PATH temporarily to give preference to the update pgsql instead of the default one:

export PATH=/usr/pgsql-9.2/bin:$PATH
bundle install

Basically, with the above commands, it will look at /usr/pgsql-9.2/bin/pg_config before the one in /usr/bin/pg_config

Christian
  • 27,509
  • 17
  • 111
  • 155
1

If you are using jruby instead of ruby you will have similar issues when installing the pg gem. Instead you need to install the adaptor:

gem 'activerecord-jdbcpostgresql-adapter'
Qiu
  • 5,651
  • 10
  • 49
  • 56
Nuri
  • 67
  • 1
  • 5
1

On Mac brew install postgres THEN bundle install

Yar HTUT
  • 46
  • 3
1

I'm on Linux (Pop_OS) 20.10 and using a version manager (asdf) for Ruby (and others) and after trying a million different ways to sort it including all of the above

TLDR

gem install pg -v '1.2.3' -- --with-pg-config='/home/username/.asdf/installs/postgres/12.6/bin/pg_config'

Find the PG install for the right version and point to the pg_config in the bin directory

saywhatnow
  • 1,026
  • 2
  • 15
  • 26
0

You just go to here to see if your pg version support Win32 platform, then use this command to install:

gem install pg -v 0.14.1 --platform=x86-mingw32

revskill
  • 31
  • 5
0

If you are using asdf to install postgres, just install pg gem like this:

asdf global postgres <version>
gem install pg
Siwei
  • 19,858
  • 7
  • 75
  • 95
0

Note that this library is called libpq-devel on some other Linux distrubitions, like Fedora, RHEL, etc. So it'd be sudo dnf install libpq-devel on Fedora.

https://unix.stackexchange.com/questions/594466/how-to-install-libpq-dev-on-fedora

Niko Dunk
  • 448
  • 6
  • 11
0

If you are using MacOS, check the mkmf.log to see whether it requires an xcode licence

Run:

sudo xcodebuild -license

and type agree in the prompt.

Plus if you have installed postgres.

where pg_config

Then

gem install pg:1.4.5 -- --with-pg-config='/usr/local/opt/libpq/bin/pg_config'

Hope it helps

Dat Le Tien
  • 1,880
  • 1
  • 9
  • 12