Can't find the 'libpq-fe.h header when trying to install pg gem
Solution 1
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
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
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
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
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
demonchand
Updated on October 20, 2021Comments
-
demonchand over 2 years
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?
-
Michael Durrant over 12 yearsdidn't work for me on ubuntu 11.04
durrantm@castleLinux2011:~/Dropbox_not_syncd/webs/3/linker$ gem install libpq-dev ERROR: Could not find a valid gem 'libpq-dev' (>= 0) in any repository durrantm@castleLinux2011:~/Dropbox_not_syncd/webs/3/linker$
-
mu is too short over 12 yearsRight, the Ruby pg gem wraps the usual PostgreSQL C libraries that come with the Ubuntu/Debian/...
libpq-dev
package. -
Nick Woodhams about 12 yearsIf you then are required to have a javascript library installed... gem install 'execjs' and gem install 'therubyracer'
-
mu is too short almost 12 years@TravisR: Thanks for the update, I just turned this into a community wiki to make it easier for everyone to keep it up to date.
-
vedarthk over 11 yearsI am using Ubuntu 12.10, I am not able to install pg gem after installing libpq-dev
-
mu is too short over 11 years@vedarthk: What does "not able" mean? Perhaps a new question with the install logs would get you the help you need.
-
vedarthk over 11 years@muistooshort Yes I have posted the question : stackoverflow.com/q/14477825/1277376
-
Jiemurat about 11 yearsThanks,
sudo apt-get install libpq-dev
helped me -
Jason Kim over 10 yearsWorks with Ubuntu 12.04 64 bit
-
Hanxue over 10 yearsIn
Mac OS X
, gethomebrew
from brew.sh , thenbrew install postgres
to install PostgresQL and its header files. -
Ryan Spears about 10 yearsDavid'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.
-
Dawei Yang about 10 yearsUsing macports,
sudo port install postgresql-devel
. -
Sebastialonso almost 10 yearsWorks like a charm in
centOS 6.5
-
brookr almost 10 yearsThanks for this, worked for me, using:
export CONFIGURE_ARGS="with-pg-include=/usr/local/Cellar/postgresql/9.3.1/include/"
-
furiabhavesh over 9 years"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 ?
-
Vincent Fourmond over 9 yearsFile 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.
-
iconoclast over 9 yearsDoes this cause any problems if you're also using Postgres.app?
-
iwasrobbed over 9 years@iconoclast I use Postgres.app to run a db instance so having the
pg
gem just allows you to hook your Rails app up to your local Postgres.app instance -
overallduka over 9 yearsFor me after run
brew install postgressql
i runARCHFLAGS="-arch x86_64" gem install pg
and works fine. -
Yi Zeng over 9 yearsUpvoted. 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 dogem install pg -- --with-pg-config=/Library/PostgreSQL/9.4/bin/pg_config
using the path I have just found out. -
fearmint over 9 yearsUsing 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
-
fearmint over 9 yearsHow 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. -
Ricardo Saporta over 9 yearsAlternatively, just running
brew install libpqxx
on mac OSX suffices -
lfender6445 about 9 yearsyou can also write as
gem install pg -- with-pg-include=/Library/PostgreSQL/9.1/include/
-
Zoidrums about 9 yearsPlease revise your answer to include
apt-get install postgres-server-dev-{pg.version}
Your answer no longer works with postgresql 9.4 on Ubuntu 14.04 See: stackoverflow.com/a/28837453/75194 -
mu is too short about 9 years@RyanRauh: Can you submit an edit? I'm not using Ubuntu right now so I'm not really qualified to say much here.
-
Les Nightingill almost 9 yearsvery 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.
-
Shelvacu over 8 yearsI just had a probelm with this because I was confusing g and q. to be clear, the package wanted is
libpq-dev
with a Q not a G -
liloargana over 8 yearsbundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config solve i for me on Centos 7
-
Michael Lafayette over 8 yearsI tried to do that on Ubuntu 14.04 but it gave me this: " libpq-dev : Depends: libpq5 (= 9.3.4-1) but 9.3.10-0ubuntu0.14.04 is to be installed.
-
Michael Lafayette over 8 yearsBut then when I try to install libpq5, it says "libpq5 is already the newest version. Same for PostgreSQL."
-
Michael Lafayette over 8 yearsPostgreSQL version is: psql --version: psql (PostgreSQL) 9.3.10. PostgreSQL 9.3.10 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2, 64-bit.
-
Michael Lafayette over 8 yearsThe following packages have unmet dependencies: postgresql-server-dev-9.3 : Depends: libpq-dev (>= 9.3~) but it is not going to be installed E: Unable to correct problems, you have held broken packages.
-
mu is too short over 8 years@MichaelLafayette I don't have Ubuntu set up anywhere right now so I don't know what the problem could be.
-
dworrad almost 8 yearsYou can also install specific versions with yum - yum install postgresqlxx-devel e.g yum install postgresql94-devel
-
mzalazar about 7 yearsAfter installing libraries... do a clean up: "make distclean", "make clean", "./configure" and start compiling!
-
troex about 7 years
port select --set postgresql postgresql96
or any other version you installed to active symlinks includingpg_config
-
jamesxu-e.g. about 7 yearsWhile I executed the command : sudo apt-get install libpq-dev, I get a response - "Unable to locate package". So I execute sudo apt-get update, then executed "sudo apt-get install libpq-dev". Finally it successfully installed.
-
Troggy almost 7 years@RicardoSaporta
libpqxx
installspostgresql
as dependency, so it is essentially the same asbrew install postgresql
-
Oleksii Kyslytsyn over 6 yearsit helped to recover back to initial ruby distribution from rvm but not fixing the problem.
-
Master of Ducks over 6 yearsWorks great if you're using the wonderful Postgres.app. For Postgres 10, I used the path
/Applications/Postgres.app/Contents/Versions/10/bin/pg_config
-
Mahesh Neelakanta about 6 yearsbundle config build.pg --with-pg-config=/usr/pgsql-10/bin/pg_config if you are using postgresql10
-
Jagdish almost 6 years"brew link --force postgresql" worked for me but befor that i have to run 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile
-
Andy Ray over 5 yearsWhy do gem designers rely on implicit, undocumented, globally installed dependencies, and make no attempt to check for their existence before installing the gem?
-
mu is too short over 5 years@AndyRay But they are checking for dependencies, that's what "checking for pg_config" and "checking for libpq-fe.h" are about. There's no way to check if the PostgreSQL development stuff is installed without, well, installing some scripts that look for the standard PostgreSQL dev tools (such as
pg_config
or the header file). Same thing happens when you need anything that isn't packaged inside a gem. -
goetzc over 5 yearsInstead of installing the whole
postgresql
, you canbrew install libpq
and export the correctPATH
andPKG_CONFIG_PATH
as explained in the 'Caveats' section. -
skozin about 5 yearsBetter yet, use
export CONFIGURE_ARGS="with-pg-config=/path/to/postgres/bin/pg_config"
. Thepg_config
executable will be called to get all the required compiler and linker options — you can run it yourself to see them. -
Alex Poca over 4 yearsIf it is not enough, add
sudo apt-get install postgresql
beforelibpq-dev
-
Sopalajo de Arrierez over 4 yearsUbuntu/Debian systems solution valid for Raspbian v9.9 on Raspberry Pi devices.
-
Jaydeep over 4 yearsI tried the same steps but still getting error in Redhat: Building native extensions. This could take a while... ERROR: Error installing pg: ERROR: Failed to build gem native extension. current directory: /opt/chef/embedded/lib/ruby/gems/2.5.0/gems/pg-1.1.4/ext /opt/chef/embedded/bin/ruby -r ./siteconf20190924-18624-nckmb.rb extconf.rb checking for pg_config... yes Using config values from /bin/pg_config checking for libpq-fe.h... *** extconf.rb failed ***
-
Alexis Wilke over 4 yearsWithout a
--
before the--with-pg-config=...
I get an error: ERROR: While executing gem ... (OptionParser::InvalidOption) invalid option: --with-pg-config=/usr/bin/pg_config) -
Chris Vilches over 4 yearsThis worked for me, I already had installed Postgres, and I just needed to execute
sudo apt-get install libpq-dev
thenbundle install
and the error disappeared. Thanks -
Roel Van de Paar over 3 yearsSo once found, what is the next step?
-
Vincent Fourmond over 3 yearsapt-get install the_package_you_found
-
Roel Van de Paar over 3 yearsAnd
ls /usr/include/postgresql/libpq-fe.h /usr/include/postgresql/pg_config
can be used to check the files are there to start with. -
8bitme over 3 yearsThis also sorted it for me on Ubuntu 20.04 using Linuxbrew.
sudo apt-get install libpq-dev
kept failing because of some other dependency -
lacostenycoder over 3 yearsThank you, this was only one that worked for me!
-
DisplayMyName about 3 yearsDoesn't let me edit your answer, you missed a letter, it should be
libpq5-devel
. It helped though, thanks. -
A. Rosas about 3 yearsI swear I had noticed that and fixed it but I guess I just thought about it. Thanks for pointing it out.
-
Augusto Samamé Barrientos about 3 yearsI would love to buy you a beer sometime. This is the ONLY thing that worked in Centos 7/8 after hours struggling with it
-
johnpitchko over 2 yearsDavid's version didn't work for installing an old version of the gem (0.21), but @skozin's instruction did. Thank you!
-
magni- over 2 yearsNote:
/<path>/.asdf/installs/postgres/<version>
is returned byasdf where postgres
-
Exodus Reed over 2 yearsthis worked perfectly for me on ubuntu 20.04 LTS. Thank you!
-
Manuel Lazo over 2 yearsworked like a charm!
sudo apt-get install libpq-dev && cd project-directory && bundle install
. -
shikharraje over 2 yearsAs someone who's just been hired to work with Ruby/Rails for the first time... Are there many more issues like this? Where a build requires a database installation to run? Do Ruby builds keep breaking for random reasons like this?
-
pachi over 2 yearsThanks! It worked for me on Fedora 35
-
jdempcy over 2 years@iconoclast I can confirm this does not cause problems with Postgres.app. I've installed Postgres.app and done 'brew install postgres' on two Macs now, in order to get around the problem of pg not installing due to missing headers. Both times worked great, and allow successful bundle install or gem install pg. No need for specifying --with-pg-config, that was a red herring for me.
-
Phil about 2 yearsFor postgres 12 and above I had to do
yum install libpq5-devel