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

359,331

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
Share:
359,331
demonchand
Author by

demonchand

Updated on October 20, 2021

Comments

  • demonchand
    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
    Michael Durrant over 12 years
    didn'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
    mu is too short over 12 years
    Right, the Ruby pg gem wraps the usual PostgreSQL C libraries that come with the Ubuntu/Debian/... libpq-dev package.
  • Nick Woodhams
    Nick Woodhams about 12 years
    If you then are required to have a javascript library installed... gem install 'execjs' and gem install 'therubyracer'
  • mu is too short
    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
    vedarthk over 11 years
    I am using Ubuntu 12.10, I am not able to install pg gem after installing libpq-dev
  • mu is too short
    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
    vedarthk over 11 years
    @muistooshort Yes I have posted the question : stackoverflow.com/q/14477825/1277376
  • Jiemurat
    Jiemurat about 11 years
    Thanks, sudo apt-get install libpq-dev helped me
  • Jason Kim
    Jason Kim over 10 years
    Works with Ubuntu 12.04 64 bit
  • Hanxue
    Hanxue over 10 years
    In Mac OS X, get homebrew from brew.sh , then brew install postgres to install PostgresQL and its header files.
  • Ryan Spears
    Ryan Spears about 10 years
    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.
  • Dawei Yang
    Dawei Yang about 10 years
    Using macports, sudo port install postgresql-devel.
  • Sebastialonso
    Sebastialonso almost 10 years
    Works like a charm in centOS 6.5
  • brookr
    brookr almost 10 years
    Thanks for this, worked for me, using: export CONFIGURE_ARGS="with-pg-include=/usr/local/Cellar/postgresql‌​/9.3.1/include/"
  • furiabhavesh
    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
    Vincent Fourmond over 9 years
    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.
  • iconoclast
    iconoclast over 9 years
    Does this cause any problems if you're also using Postgres.app?
  • iwasrobbed
    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
    overallduka over 9 years
    For me after run brew install postgressql i run ARCHFLAGS="-arch x86_64" gem install pg and works fine.
  • Yi Zeng
    Yi Zeng over 9 years
    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.
  • fearmint
    fearmint over 9 years
    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-redh‌​at93-9.3-1.noarch.rp‌​m && yum install postgresql93 postgresql93-contrib postgresql93-server postgresql93-devel
  • fearmint
    fearmint over 9 years
    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.
  • Ricardo Saporta
    Ricardo Saporta over 9 years
    Alternatively, just running brew install libpqxx on mac OSX suffices
  • lfender6445
    lfender6445 about 9 years
    you can also write as gem install pg -- with-pg-include=/Library/PostgreSQL/9.1/include/
  • Zoidrums
    Zoidrums about 9 years
    Please 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
    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
    Les Nightingill almost 9 years
    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.
  • Shelvacu
    Shelvacu over 8 years
    I 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
    liloargana over 8 years
    bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config solve i for me on Centos 7
  • Michael Lafayette
    Michael Lafayette over 8 years
    I 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
    Michael Lafayette over 8 years
    But then when I try to install libpq5, it says "libpq5 is already the newest version. Same for PostgreSQL."
  • Michael Lafayette
    Michael Lafayette over 8 years
    PostgreSQL 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
    Michael Lafayette over 8 years
    The 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
    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
    dworrad almost 8 years
    You can also install specific versions with yum - yum install postgresqlxx-devel e.g yum install postgresql94-devel
  • mzalazar
    mzalazar about 7 years
    After installing libraries... do a clean up: "make distclean", "make clean", "./configure" and start compiling!
  • troex
    troex about 7 years
    port select --set postgresql postgresql96 or any other version you installed to active symlinks including pg_config
  • jamesxu-e.g.
    jamesxu-e.g. about 7 years
    While 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
    Troggy almost 7 years
    @RicardoSaporta libpqxx installs postgresql as dependency, so it is essentially the same as brew install postgresql
  • Oleksii Kyslytsyn
    Oleksii Kyslytsyn over 6 years
    it helped to recover back to initial ruby distribution from rvm but not fixing the problem.
  • Master of Ducks
    Master of Ducks over 6 years
    Works great if you're using the wonderful Postgres.app. For Postgres 10, I used the path /Applications/Postgres.app/Contents/Versions/10/bin/pg_confi‌​g
  • Mahesh Neelakanta
    Mahesh Neelakanta about 6 years
    bundle config build.pg --with-pg-config=/usr/pgsql-10/bin/pg_config if you are using postgresql10
  • Jagdish
    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
    Andy Ray over 5 years
    Why 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
    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
    goetzc over 5 years
    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.
  • skozin
    skozin about 5 years
    Better yet, use export CONFIGURE_ARGS="with-pg-config=/path/to/postgres/bin/pg_conf‌​ig". The pg_config executable will be called to get all the required compiler and linker options — you can run it yourself to see them.
  • Alex Poca
    Alex Poca over 4 years
    If it is not enough, add sudo apt-get install postgresql before libpq-dev
  • Sopalajo de Arrierez
    Sopalajo de Arrierez over 4 years
    Ubuntu/Debian systems solution valid for Raspbian v9.9 on Raspberry Pi devices.
  • Jaydeep
    Jaydeep over 4 years
    I 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
    Alexis Wilke over 4 years
    Without 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
    Chris Vilches over 4 years
    This worked for me, I already had installed Postgres, and I just needed to execute sudo apt-get install libpq-dev then bundle install and the error disappeared. Thanks
  • Roel Van de Paar
    Roel Van de Paar over 3 years
    So once found, what is the next step?
  • Vincent Fourmond
    Vincent Fourmond over 3 years
    apt-get install the_package_you_found
  • Roel Van de Paar
    Roel Van de Paar over 3 years
    And 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
    8bitme over 3 years
    This 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
    lacostenycoder over 3 years
    Thank you, this was only one that worked for me!
  • DisplayMyName
    DisplayMyName about 3 years
    Doesn't let me edit your answer, you missed a letter, it should be libpq5-devel. It helped though, thanks.
  • A. Rosas
    A. Rosas about 3 years
    I swear I had noticed that and fixed it but I guess I just thought about it. Thanks for pointing it out.
  • Augusto Samamé Barrientos
    Augusto Samamé Barrientos about 3 years
    I 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
    johnpitchko over 2 years
    David's version didn't work for installing an old version of the gem (0.21), but @skozin's instruction did. Thank you!
  • magni-
    magni- over 2 years
    Note: /<path>/.asdf/installs/postgres/<version> is returned by asdf where postgres
  • Exodus Reed
    Exodus Reed over 2 years
    this worked perfectly for me on ubuntu 20.04 LTS. Thank you!
  • Manuel Lazo
    Manuel Lazo over 2 years
    worked like a charm! sudo apt-get install libpq-dev && cd project-directory && bundle install .
  • shikharraje
    shikharraje over 2 years
    As 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
    pachi over 2 years
    Thanks! It worked for me on Fedora 35
  • jdempcy
    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
    Phil about 2 years
    For postgres 12 and above I had to do yum install libpq5-devel