I am using the Ruby on Rails 3.1 pre version. I like to use PostgreSQL, but the problem is installing the pg gem. It gives me the following error:

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

        /home/u/.rvm/rubies/ruby-1.9.2-p0/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=/home/u/.rvm/rubies/ruby-1.9.2-p0/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 /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
Results logged to /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out

How do I solve this problem?

37 Answers 37

up vote 1000 down vote accepted

It looks like in Ubuntu that header is part of the libpq-dev package (at least in the following Ubuntu versions: 11.04 (Natty Narwhal), 10.04 (Lucid Lynx), 11.10 (Oneiric Ocelot), 12.04 (Precise Pangolin), 14.04 (Trusty Tahr) and 18.04 (Bionic Beaver)):

...
/usr/include/postgresql/libpq-fe.h
...

So try installing libpq-dev or its equivalent for your OS:

  • For Ubuntu systems: sudo apt-get install libpq-dev
  • On Red Hat Linux (RHEL) systems: yum install postgresql-devel
  • For Mac Homebrew: brew install postgresql
  • For Mac MacPorts PostgreSQL: gem install pg -- --with-pg-config=/opt/local/lib/postgresql[version number]/bin/pg_config
  • For OpenSuse: zypper in postgresql-devel
  • For ArchLinux: pacman -S postgresql-libs

On macOS (previously Mac OS X and OS X), use Homebrew to install the proper headers:

brew install postgresql

and then running

gem install pg

should work.

I had also tried doing gem install libpq-dev, but I received this error:

Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

However I found that installing with sudo apt-get (which I try to avoid using with Ruby on Rails) worked, i.e.

sudo apt-get install libpq-dev
# or
apt-get install postgres-server-dev-{pg.version}
# for postgresql 9.4 on Ubuntu 14.04

then I was able to do

gem install pg

without issues.

I could solve this in another way. I didn't find the library on my system. Thus I installed it using an app from PostgreSQL main website. In my case (OS X) I found the file under /Library/PostgreSQL/9.1/include/ once the installation was over. You may also have the file somewhere else depending on your system if you already have PostgreSQL installed.

Thanks to this link on how to add an additional path for gem installation, I could point the gem to the lib with this command:

export CONFIGURE_ARGS="with-pg-include=/Library/PostgreSQL/9.1/include/"
gem install pg

After that, it works, because it now knows where to find the missing library. Just replace the path with the right location for your libpq-fe.h

  • 2
    David's solution worked for me. I tracked libpq-fe.h to '/Applications/Postgres.app/Contents/Versions/9.3/include' and used the export command with said path, followed by the 'gem install pg' and the gem installed successfully. – Ryan Spears Apr 11 '14 at 12:13
  • Thanks for this, worked for me, using: export CONFIGURE_ARGS="with-pg-include=/usr/local/Cellar/postgresql/9.3.1/include/" – brookr Jul 31 '14 at 19:32
  • Using Postgresql 9.3 on CentOS 6 I solved this by symlinking the pg_* scripts into the $PATH like so: ln -s /usr/pgsql-9.3/bin/p* /usr/local/bin. I installed postgres 9.3 like so: yum install http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-1.noarch.rpm && yum install postgresql93 postgresql93-contrib postgresql93-server postgresql93-devel – JoePasq Jan 16 '15 at 18:52
  • 1
    you can also write as gem install pg -- with-pg-include=/Library/PostgreSQL/9.1/include/ – lfender6445 Mar 2 '15 at 0:52

Can't find the libpq-fe.h header

i had success on CentOS 7.0.1406 running the following commands:

~ % psql --version # => psql (PostgreSQL) 9.4.1
yum install libpqxx-devel
gem install pg -- --with-pg-config=/usr/pgsql-9.4/bin/pg_config

Alternatively, you can configure bundler to always install pg with these options (helpful for running bundler in deploy environments),

  • bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config
  • 1
    very helpful, thanks. Just to add a note regarding bundle config... you must run this command while logged-in as the user who will be running the subsequent bundle command. The bundler config is stored in ~/.bundle/config, so it will not be found if you run bundle config while logged-in as root, but bundler is being run by (e.g. with capistrano) the 'deploy' user. – Les Nightingill May 28 '15 at 12:36
  • 2
    bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config solve i for me on Centos 7 – liloargana Jan 3 '16 at 19:39
  • 1
    bundle config build.pg --with-pg-config=/usr/pgsql-10/bin/pg_config if you are using postgresql10 – Mahesh Neelakanta Feb 9 at 20:33

For CentOS 6.4,

yum install postgresql-devel
gem install pg

worked well!

On Mac OS X run like this:

gem install pg -- --with-pg-config=***/path/to/pg_config***

***/path/to/pg_config*** is path to pg_config

  • 1
    Upvoted. I installed Postgresql via EnterpriseDB's grapihcal installer. Then I need to find out the pg-config path by sudo find / -name "pg_config", then do gem install pg -- --with-pg-config=/Library/PostgreSQL/9.4/bin/pg_config using the path I have just found out. – Yi Zeng Dec 31 '14 at 21:56

In my case it was package postgresql-server-dev-8.4 (I am on Ubuntu 11.04 (Natty Narwhal), 64 bits).

Just for the record:

Ruby on Rails 4 application in OS X with PostgresApp (in this case 0.17.1 version needed - kind of an old project):

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

The right answer for Mac users with Postgres.app is to build against the libpq provided with that package. For example, with the 9.4 release (current as of this writing), all you need is:

export CONFIGURE_ARGS="with-pg-include=/Applications/Postgres.app/Contents/Versions/9.4/include"
gem install pg

This will keep your pg gem in sync with exactly the version of PostgreSQL you have installed. Installing something from Homebrew is a waste in this case.

I had the same issue on Amazon Linux. I could find the header libpq-fe.h, but somehow it didn't work.

It came from the different versions of the packages that were installed through the different users on the machine. PostgreSQL 9.2 and PostgreSQL 9.3 were installed. So, make sure of your PostgreSQL version before including the libraries.

For me, the magic command line was:

sudo yum install postgresql93 postgresql93-server postgresql93-libs postgresql93-contrib postgresql93-devel

Source: An almost idiot's guide to install PostgreSQL 9.3, PostGIS 2.1 and pgRouting with Yum

  • After installing libraries... do a clean up: "make distclean", "make clean", "./configure" and start compiling! – mzalazar Feb 5 '17 at 23:18

A more general answer for any Debian-based distribution (which includes Ubuntu) is the following. First, install the apt-file package running as root:

apt-get install apt-file

This allows you to search for packages containing a file. Then, update its database using

apt-file update

(this can be run as normal user). Then, look for the missing header using:

apt-file search libpq-fe.h

On my machine, this gives:

libpq-dev: /usr/include/postgresql/libpq-fe.h
postgres-xc-server-dev: /usr/include/postgres-xc/server/gtm/libpq-fe.h

There you go !

  • "libpq-dev: /usr/include/postgresql/libpq-fe.h" is present whereas "postgres-xc-server-dev: /usr/include/postgres-xc/server/gtm/libpq-fe.h" is not present ? what am I missing ? – furiabhavesh Aug 11 '14 at 11:38
  • File contents may have changed. This looks like there was a private copy of libpq-fe.h in the postgres-... package. In any case, the libXXX-dev is the one you're looking for if you want to compile things. – Vincent Fourmond Aug 11 '14 at 15:10
  • +1 as this is better than the answer, it teaches how to find the solution for any category of this question. – ConstantineK Oct 19 '14 at 15:37

I had the same problem on Mac OS, but I installed the PostgreSQL gem easily by using the following in a terminal:

ARCHFLAGS="-arch x86_64" gem install pg

(I installed PostgreSQL first with brew install postgresql.)

I found this answer, and it was the only one that worked for me (Mac OS) - after researching for about two days:

$ sudo su

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

Building native extensions.  This could take a while...
Successfully installed pg-0.11.0
1 gem installed
Installing ri documentation for pg-0.11.0...
Installing RDoc documentation for pg-0.11.0...

See Stack Overflow question Can't find the PostgreSQL client library (libpq).

I recently upgraded to Mac OS X v10.10 (Yosemite) and was having difficulty building the pg gem.

The error reported was the typical:

Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... *** extconf.rb failed ***

My solution was to gem uninstall pg and then bundle update pg to replace the gem with the latest. I did run brew update; brew upgrade after the Yosemite install to get the latest versions of packages I had installed previously.

On a Mac, I solved it using this code:

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

On CentOS,I installed libpq-dev package using below command

yum install postgresql-devel

Executing gem install pg returned the same error as "No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config".

Installing the gem as below solved my problem

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

I am running Postgres.app on a Mac and I had to

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

first. Then

bundle install

worked for me.

On FreeBSD (9.1) the necessary package is /usr/ports/database/postgresql-server* which when installed will also install the required header file that makes the gem install of "pg" fail. This answer here helped me find the solution but the difference in package names required a bit of searching.

Hopefully this helps someone avoid a bit of head scratching when searching for the "-dev" package on a FreeBSD system!

On Debian 7.0, 64-bit (Wheezy), just run:

sudo apt-get install libpq-dev

After you successfully installed libpq-dev, run:

bundle install

Under CentOS 6.5 (Squeeze) I created a file:

$ sudo touch /etc/profile.d/psql.sh

with content:

pathmunge /usr/pgsql-9.3/bin

Note here, you should set your PostgreSQL path with the pg_config file. You can find this with the command:

$ sudo find / -iname pg_config

Save the file:

$ sudo chmod +x /etc/profile.d/ruby.sh

and try to execute your command again.

Note: Anytime you change the Bash configuration - changing the profile.d configuration - you should reload Bash.

  • How did you install postgres? I symlinked the pg_* scripts into the path like so: ln -s /usr/pgsql-9.3/bin/p* /usr/local/bin. After that I didn’t need any additional scripts or environment variables. – JoePasq Jan 16 '15 at 18:55

The location of libpq-fe.h depends on where your PostgreSQL install is (which depends on how you installed it). Use locate (http://en.wikipedia.org/wiki/Locate_%28Unix%29) to find the libpq-fe.h file on your machine. If it exists, it will be in the include directory of your PostgreSQL install.

$ locate libpq-fe.h
/Library/PostgreSQL/9.1/include/libpq-fe.h

The bin directory, which contains pg_config, will be in the same directory as the include directory. As the error suggests, use the --with-pg-config option to install the gem:

$ gem install pg --with-pg-config="/Library/PostgreSQL/9.1/bin/pg_config"

Note that you may need to run updatedb if you have never used locate or if you haven't updated since installing PostgreSQL.

I finally solved this problem, but not using the previously described methods.

Using brew install postgresql, I find out that it had already been installed, but not linked.

  1. Find out where PostgreSQL is installed, and delete it,

  2. Then brew install postgresql again,

  3. brew link postgresql

  4. gem install pg

  • "brew link --force postgresql" worked for me but befor that i have to run 'export PATH="/usr/local/opt/postgresql@9.4/bin:$PATH"' >> ~/.bash_profile – Jagdish N Aug 1 at 15:14

I just had this on OSX running brew and postgres@9.4.

My fix was this:

CONFIGURE_ARGS="with-pg-include=/usr/local/opt/postgresql@9.4/include/" bundle install

On Ubuntu, install the "libpq-dev" to get rid of this issue.

sudo apt-get install libpq-dev

For MacOS without installing PostgreSQL server:

brew install libpq
gem install pg -- --with-pg-config="/usr/local/Cellar/libpq/9.6.6/bin/pg_config"

For AltLinux the package postgresqlx.x-devel (in my case postgresql9.5-devel) must be installed:

apt-get install postgresql9.5-devel
  • You can also install specific versions with yum - yum install postgresqlxx-devel e.g yum install postgresql94-devel – dworrad Jul 18 '16 at 9:12

I solved this installing the 'postgresql-common' package. This package provides the pg_config binary which is most likely what you were missing.

I was having a similar problem, and this fixed it for me:

gem install do_postgres -- --with-pgsql-server-dir=/Applications/Postgres.app/Contents/MacOS --with-pgsql-server-include=/Applications/Postgres.app/Contents/MacOS/include/server

Source:

https://gist.github.com/oisin/6562181

On OS X 10.9 (Mavericks), I brew-installed postgresql, and I then had to rvm reinstall my Ruby. I am happy now :)

protected by Community Jul 5 '16 at 8:33

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.