124
$ sudo bundle install

Result

Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Using rake (0.9.2.2) 
Using i18n (0.6.1) 
Using multi_json (1.3.6) 
Using activesupport (3.2.8) 
Using builder (3.0.4) 
Using activemodel (3.2.8) 
Using erubis (2.7.0) 
Using journey (1.0.4) 
Using rack (1.4.1) 
Using rack-cache (1.2) 
Using rack-test (0.6.2) 
Using hike (1.2.1) 
Using tilt (1.3.3) 
Using sprockets (2.1.3) 
Using actionpack (3.2.8) 
Using mime-types (1.19) 
Using polyglot (0.3.3) 
Using treetop (1.4.11) 
Using mail (2.4.4) 
Using actionmailer (3.2.8) 
Using arel (3.0.2) 
Using tzinfo (0.3.33) 
Using activerecord (3.2.8) 
Using activeresource (3.2.8) 
Using bundler (1.2.1) 
Using coffee-script-source (1.4.0) 
Using execjs (1.4.0) 
Using coffee-script (2.2.0) 
Using rack-ssl (1.3.2) 
Using json (1.7.5) 
Using rdoc (3.12) 
Using thor (0.16.0) 
Using railties (3.2.8) 
Using coffee-rails (3.2.2) 
Using jquery-rails (2.1.3) 
Installing pg (0.14.1) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.8 extconf.rb 
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
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=/usr/bin/ruby1.8
    --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 /var/lib/gems/1.8/gems/pg-0.14.1 for inspection.
Results logged to /var/lib/gems/1.8/gems/pg-0.14.1/ext/gem_make.out
An error occurred while installing pg (0.14.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.14.1'` succeeds before bundling.

I am make $ gem install pg -v '0.14.1' But this is not help

My Gemfile

source 'https://rubygems.org'

#gem 'rails', '3.0.9'

#gem 'sqlite3', '1.3.6', :group => :development


gem 'rails', '3.2.8'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'


gem 'sqlite3'
gem 'pg'
gem 'taps'

gem 'json'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'


#group :production do
  #gem 'pg'
#end
#group :development do
  #gem 'sqlite3'
#end

gem_make.out

/usr/bin/ruby1.8 extconf.rb 
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
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=/usr/bin/ruby1.8
    --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

mkmf.log

find_executable: checking for pg_config... -------------------- yes

--------------------

find_header: checking for libpq-fe.h... -------------------- no

"gcc -E -I. -I/usr/lib/ruby/1.8/i686-linux -I. -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2 -I -fno-strict-aliasing -g -g -O2  -fPIC    conftest.c -o conftest.i"
conftest.c:1:22: fatal error: libpq-fe.h: No such file or directory
compilation terminated.
checked program was:
/* begin */
1: #include <libpq-fe.h>
/* end */

--------------------

Help me please. Thank you.

Sorry for this text. Your post does not have much context to explain the code sections; please explain your scenario more clearly.

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
Arthur Yakovlev
  • 8,933
  • 8
  • 32
  • 48
  • 1
    How did you install PostgreSQL? – Stefan Oct 26 '12 at 11:32
  • 1
    possible duplicate of [Can't find the PostgreSQL client library (libpq)](http://stackoverflow.com/questions/6209797/cant-find-the-postgresql-client-library-libpq) – skolima Oct 26 '12 at 12:49
  • 2
    possible duplicate of [Can't find the 'libpq-fe.h header when trying to install pg gem](http://stackoverflow.com/questions/6040583/cant-find-the-libpq-fe-h-header-when-trying-to-install-pg-gem) – iconoclast Jan 25 '14 at 05:46

16 Answers16

254

Just make this

$ sudo apt-get install libpq-dev
Arthur Yakovlev
  • 8,933
  • 8
  • 32
  • 48
  • 1
    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 Jan 13 '16 at 13:35
  • But then when I try to install libpq5, it says "libpq5 is already the newest version." – Michael Lafayette Jan 13 '16 at 13:40
  • 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 Jan 13 '16 at 13:40
  • 1
    Brilliant! The application I was compiling also needed the path to `libpq-fe.h` to be specified explicitly, with `--with-cflags=-I/usr/include/postgresql`, in case anyone else encounters the same. – Ben Johnson Nov 16 '16 at 15:07
  • For those of you wondering what libpq-dev means: It contains a minimal set of `PostgreSQL`_ binaries and headers requried for building 3rd-party applications for `PostgreSQL`_. – IronMan007 Jun 05 '17 at 02:17
  • for Amazon linux, yum install libpqxx-devel fixes the issue – ramesh.mimit Mar 13 '18 at 02:02
  • 2
    This was what ended up working for me, or at least the Mac version of it, which is `brew install libpq`. I then also had to `brew link --force libpq` to get the `gem install pg` to finish successfully. – Scott Schupbach Jan 28 '20 at 21:46
  • In Ubuntu, in C++, you may want to use `#include ` instead of `#include `. – kHarshit Nov 09 '20 at 11:06
90

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

Depending on your environment:

  • Mac: brew install postgresql
  • Ubuntu: sudo apt-get install libpq-dev
  • RHEL: yum install postgresql-devel
  • Alpine: apk add postgresql-dev

Then run gem install pg again

Jrgns
  • 24,699
  • 18
  • 71
  • 77
Phil
  • 2,732
  • 19
  • 20
  • 2
    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 Jan 13 '16 at 13:41
  • But then when I try to install libpq5, it says "libpq5 is already the newest version." Same for PostgreSQL – Michael Lafayette Jan 13 '16 at 13:42
  • 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 Jan 13 '16 at 13:42
  • Hey Michael, I'm not sure I can answer your question because I'm not familiar with Ubuntu, but you may want to check out Michael Durrant's answer here: http://stackoverflow.com/a/8887801/2484523 – Phil Jan 19 '16 at 22:12
  • Worked on Red Hat EL7. I was getting the header file missing during a Perl install of DBD::Pg. – Marcus Feb 19 '18 at 14:03
  • This is the answer for a CentOS droplet on Digital Ocean. `postgresql-devel` needs to be installed. – David Gay Jul 04 '18 at 15:30
  • On CentOS6 'sudo yum install libpq*' installed the correct dependencies for me to gem install pg successfully. – lowly_junior_sysadmin Aug 02 '18 at 22:53
  • For those of you experiencing this but also see "libpq5 is already the newest version." when you try to install, try `sudo apt-get remove libpq-dev` and then installing it again. Worked for me. – bplittle Mar 23 '21 at 00:58
  • Note: If you installed the postgres app directly, you should instead run `brew install libpq` – Josh Mathews Jan 26 '22 at 23:37
20
  • SO: Ubuntu
  • Language Programming: C

$ sudo apt-get install libpq-dev

In my case I was putting the directory path wrong. So:

From: gcc -o lib_version lib_version.c -I/usr/include/postgresql -lpq -std=c99

To: gcc -o lib_version lib_version.c -I /usr/include/postgresql -lpq -std=c99

GL

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
19

Try this:

  1. brew install postgresql
  2. gem install pg
  3. bundle install
Karol
  • 191
  • 1
  • 2
17

On Fedora/RHEL systems this did the trick:

sudo yum install libpqxx-devel
Patrick
  • 2,587
  • 16
  • 21
nicolasochem
  • 467
  • 4
  • 8
10

For Alpine Linux, you can add libpq-fe.h with:

apk add postgresql-dev
Alter Lagos
  • 12,090
  • 1
  • 70
  • 92
8

For Amazon Linux (AWS):

sudo yum install postgresql-devel

then run your command again

Gaurav Ragtah
  • 123
  • 3
  • 8
5

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

Jiemurat
  • 1,619
  • 20
  • 20
4

I had this issue with a Jenkins server on Amazon AMI.

$ pg_config | grep VERSION
VERSION = PostgreSQL 9.4.9

Then depending on which version you need you can install the required lib

$ sudo yum list postgresql* | grep devel
postgresql92-devel.x86_64               9.2.18-1.59.amzn1          @amzn-main
postgresql94-devel.x86_64               9.4.9-1.67.amzn1           @amzn-main
postgresql8-devel.x86_64                8.4.20-5.52.amzn1          amzn-main
postgresql93-devel.x86_64               9.3.14-1.62.amzn1          amzn-main
postgresql95-devel.x86_64               9.5.4-1.71.amzn1           amzn-main

Then you can just install the corresponding version, in my case for version 9.4:

sudo yum install postgresql94-devel
Justin Fortier
  • 451
  • 5
  • 6
3

On mac make sure your postgres is linked. You can do it by

brew link --overwrite postgresql

This fixed the issue for me.

Ashkinas
  • 41
  • 1
3

This was happening me using capistrano while deploying my app in a centos7 distro. Apparently gem is requiring some header files which can be resolved installing the right development package.

I fixed it by running sudo yum search postgres | grep devel and found the right package for my postgres installation which was 10.

Then simply run sudo yum install postgresql10-devel, and eureka!

JGutierrezC
  • 4,398
  • 5
  • 25
  • 42
2

try this:

sudo apt-get install libpq-dev
gem install pg 

it would be works!

1

I use Ubuntu-16. And I use the method showed here:https://www.postgresql.org/download/linux/ubuntu/ to install postgresql-9.6; And I use "sudo apt-get install libpq-dev" to install the devlib.But It still can't work. So I use the method ln to establish soft link, here it is : sudo ln -s /usr/include/postgresql/libpq-fe.h /usr/include/; sudo ln -s /usr/include/postgresql/postgres_ext.h /usr/include/ And I solve the problem at last.

lxc
  • 67
  • 9
1

brew install postgresql worked for me.

Installation of postgresql was giving another error

Error: The following directories are not writable by your user: /usr/local/lib/pkgconfig /usr/local/share/info /usr/local/share/man/man3

This error was fixed by giving access to the mentioned directories to the current user

sudo chown -R $(whoami) (path)

defcon
  • 123
  • 7
0

This worked for me:

sudo gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
Undo
  • 25,519
  • 37
  • 106
  • 129
rassom
  • 2,896
  • 5
  • 34
  • 45
0

If you've installed postgresql on MacOS via Homebrew

brew install postgresql@95 # 9.5 is the version needed in my project

you may need to find the pg_config depending on your version of libpq

my project needed older version of pg.

gem install pg -v '0.20.0' -- --with-pg config=/usr/local/Cellar/libpq/13.1/bin/pg_config
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48