Can't find the 'libpq-fe.h header when trying to install pg gem

Ruby on-RailsRuby on-Rails-3PostgresqlRails PostgresqlPg

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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/Debian 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

Solution 2 - Ruby on-Rails

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.

Alternatively, instead of installing the whole postgresql, you can brew install libpq and export the correct PATH and PKG_CONFIG_PATH as explained in the 'Caveats' section

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

> 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

Solution 6 - Ruby on-Rails

For CentOS 6.4,

yum install postgresql-devel
gem install pg

worked well!

Solution 7 - Ruby on-Rails

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

Solution 8 - Ruby on-Rails

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

Solution 9 - Ruby on-Rails

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

Solution 10 - Ruby on-Rails

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"

Solution 11 - Ruby on-Rails

It is only issue of missing libpq-fe.h

On ubuntu we just need to run: sudo apt-get install libpq-dev

Above command will install a NEW package libpq-dev and after that you can again execute bundle install to resume your bundle installation.

Solution 12 - Ruby on-Rails

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.

Solution 13 - Ruby on-Rails

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 !

Solution 14 - Ruby on-Rails

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

Solution 15 - Ruby on-Rails

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

sudo apt-get install libpq-dev

Solution 16 - Ruby on-Rails

My solution for Fedora 30:

sudo dnf install /usr/include/libpq-fe.h

Solution 17 - Ruby on-Rails

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.)

Solution 18 - Ruby on-Rails

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 https://stackoverflow.com/questions/6209797/cant-find-the-postgresql-client-library-libpq.

Solution 19 - Ruby on-Rails

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.

Solution 20 - Ruby on-Rails

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

Solution 21 - Ruby on-Rails

On Fedora 32, I managed to solve this same issue by installing libpq5-devel specifically.

If you install postgresql from the official repository, you will have several versions that won't fix the problem, so it's really a matter of trying out which one it needs.

Solution 22 - Ruby on-Rails

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.

Solution 23 - Ruby on-Rails

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

Solution 24 - Ruby on-Rails

I had this issue with Postgresql 9.6. I was able to fix it by doing:

brew upgrade postgresql@9.6
brew link postgresql@9.6 --force
gem install pg

Solution 25 - Ruby on-Rails

On Ubuntu 20.04, I already had libpq5:amd64 version 13.2-1.pgdg20.04+1. Now when I tried to install libpq-dev from apt repository, I was receiving

The following packages have unmet dependencies:
libpq-dev : Depends: libpq5 (= 12.6-0ubuntu0.20.04.1) but 13.2-1.pgdg20.04+1 is to be installed

I grabbed the deb from http://ftp.us.debian.org/debian/pool/main/p/postgresql-13/libpq-dev_13.2-1_amd64.deb and did the following:

mkdir -p /tmp/libpq-dev
dpkg-deb -R libpq-dev_13.2-1_amd64.deb /tmp/libpq-dev
sed -i 's|= 13.2-1|= 13.2-1.pgdg20.04+1|' /tmp/libpq-dev/DEBIAN/control
dpkg-deb -b /tmp/libpq-dev /tmp/libpq-dev-new.deb
sudo dpkg -i /tmp/libpq-dev-new.deb

I have been using the gem happily after that. No additional installs or anything.

Solution 26 - Ruby on-Rails

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!

Solution 27 - Ruby on-Rails

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

sudo apt-get install libpq-dev

After you successfully installed libpq-dev, run:

bundle install

Solution 28 - Ruby on-Rails

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.

Solution 29 - Ruby on-Rails

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.

Solution 30 - Ruby on-Rails

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

Solution 31 - Ruby on-Rails

I encountered the same error with postgres installed through asdf. The pg-config solutions didn't work for me. Instead I had to locate the postgres include folder which contains the file and run the command with the --with-pg-include flag

gem install pg -- --with-pg-include=/<path>/.asdf/installs/postgres/<version>/include

Solution 32 - Ruby on-Rails

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

Solution 33 - Ruby on-Rails

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

Solution 34 - Ruby on-Rails

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

Solution 35 - Ruby on-Rails

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

apt-get install postgresql9.5-devel

Solution 36 - Ruby on-Rails

I just had this on OSX running brew and [email protected].

My fix was this:

CONFIGURE_ARGS="with-pg-include=/usr/local/opt/[email protected]/include/" bundle install

Solution 37 - Ruby on-Rails

Only uninstalling (sudo apt-get purge) libpq-dev and re-installing it worked for me.

Solution 38 - Ruby on-Rails

On OS X and MacPorts the -devel package for PostgreSQL is not required any more. Installing the pg gem works as follows:

~ > sudo port install postgresql-devel
...
Error: postgresql-devel has been replaced by postgresql91; please install that port instead
~ > gem install pg -v '0.17.1' -- --with-pg-config=/opt/local/lib/postgresql95/bin/pg_config

See also this post.

Solution 39 - Ruby on-Rails

On Arch Linux you will need to install postgresql-libs:

sudo pacman -Syu postgresql-libs

Solution 40 - Ruby on-Rails

For Mac OS X just install https://postgresapp.com/ and follow steps for command line section and ruby

sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

Then

sudo ARCHFLAGS="-arch x86_64" gem install pg

Solution 41 - Ruby on-Rails

Step 1) Make sure postgress is installed in your system ( If it's already installed and you can run postgress server on your machine move to step (2)

apt-get install postgresql postgresql-contrib (sol - fatal error: libpq-fe.h: No such file or directory)
sudo apt-get install ruby-dev (required to install postgress below)
sudo gem install pg
sudo service postgresql restart

Step 2) Some of the c++ files are trying to access libpq-fe.h directly and cant find. So we need to manually search every such file and replace libpq-fe.h with postgresql/libpq-fe.h

Command for searching all occurrences of libpq-fe.h in all dir and subdir is grep -rnw ./ -e 'libpq-fe.h'

  1. Go to All the file listed by command run in step 2 and manually change libpq-fe.h with postgresql/libpq-fe.h.

Solution 42 - Ruby on-Rails

I have tried all solutions but only works below command

pip install psycopg2-binary

Solution 43 - Ruby on-Rails

Ubuntu 20.04.1 LTS, this seemed to work for me:

sudo apt-get install ruby ruby-dev libpqxx-dev libpq-fe-dev libpq-dev 
sudo gem install pg -- --with-pg-config=/usr/include/postgresql/pg_config --with-pg-include=/usr/include/postgresql/

Solution 44 - Ruby on-Rails

For alpine, please use below to fix the error

apk add --no-cache postgresql-dev

Solution 45 - Ruby on-Rails

I fixed the same error by doing a Ruby reinstall via rvm:

rvm reinstall 1.9.3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondemonchandView Question on Stackoverflow
Solution 1 - Ruby on-Railsmu is too shortView Answer on Stackoverflow
Solution 2 - Ruby on-RailsiwasrobbedView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMichael DurrantView Answer on Stackoverflow
Solution 4 - Ruby on-RailsDavid PelaezView Answer on Stackoverflow
Solution 5 - Ruby on-Railslfender6445View Answer on Stackoverflow
Solution 6 - Ruby on-RailsArivarasan LView Answer on Stackoverflow
Solution 7 - Ruby on-RailspablomartiView Answer on Stackoverflow
Solution 8 - Ruby on-RailsJiemuratView Answer on Stackoverflow
Solution 9 - Ruby on-RailsJohannView Answer on Stackoverflow
Solution 10 - Ruby on-RailsMichael A.View Answer on Stackoverflow
Solution 11 - Ruby on-RailsYash DubeyView Answer on Stackoverflow
Solution 12 - Ruby on-RailsjelderView Answer on Stackoverflow
Solution 13 - Ruby on-RailsVincent FourmondView Answer on Stackoverflow
Solution 14 - Ruby on-RailsFab V.View Answer on Stackoverflow
Solution 15 - Ruby on-RailsRajuView Answer on Stackoverflow
Solution 16 - Ruby on-RailsAarón CastroView Answer on Stackoverflow
Solution 17 - Ruby on-RailsrassomView Answer on Stackoverflow
Solution 18 - Ruby on-RailsCynthia SanchezView Answer on Stackoverflow
Solution 19 - Ruby on-RailsMartin StreicherView Answer on Stackoverflow
Solution 20 - Ruby on-RailsMarioView Answer on Stackoverflow
Solution 21 - Ruby on-RailsA. RosasView Answer on Stackoverflow
Solution 22 - Ruby on-Railsjoh-mueView Answer on Stackoverflow
Solution 23 - Ruby on-Railsuser2086641View Answer on Stackoverflow
Solution 24 - Ruby on-RailsmwalsherView Answer on Stackoverflow
Solution 25 - Ruby on-RailsSamarView Answer on Stackoverflow
Solution 26 - Ruby on-RailsClaus DueView Answer on Stackoverflow
Solution 27 - Ruby on-RailsEric WuView Answer on Stackoverflow
Solution 28 - Ruby on-RailslucianosousaView Answer on Stackoverflow
Solution 29 - Ruby on-RailsakbrownView Answer on Stackoverflow
Solution 30 - Ruby on-RailsChile YangView Answer on Stackoverflow
Solution 31 - Ruby on-RailsMatthias Michael EnghView Answer on Stackoverflow
Solution 32 - Ruby on-RailsFabrizio ReginiView Answer on Stackoverflow
Solution 33 - Ruby on-RailsdunklesView Answer on Stackoverflow
Solution 34 - Ruby on-Railsether_joeView Answer on Stackoverflow
Solution 35 - Ruby on-RailsМалъ СкрылевъView Answer on Stackoverflow
Solution 36 - Ruby on-RailsNickView Answer on Stackoverflow
Solution 37 - Ruby on-RailsvasilakisfilView Answer on Stackoverflow
Solution 38 - Ruby on-RailsJensView Answer on Stackoverflow
Solution 39 - Ruby on-RailsPhilip KirkbrideView Answer on Stackoverflow
Solution 40 - Ruby on-RailsMuhammad Hassan NasrView Answer on Stackoverflow
Solution 41 - Ruby on-RailssapyView Answer on Stackoverflow
Solution 42 - Ruby on-RailsRaviPatidarView Answer on Stackoverflow
Solution 43 - Ruby on-RailsRoel Van de PaarView Answer on Stackoverflow
Solution 44 - Ruby on-RailsChinmaya BiswalView Answer on Stackoverflow
Solution 45 - Ruby on-RailsMicah RobersonView Answer on Stackoverflow