309

I'm trying to install PostgreSQL's pg gem for Ruby.

I issued the following command:

gem install pg

I installed Ruby 1.9.2 using RVM.

The above command shows me the following error.

The error is :

Building native extensions.  This could take a while...

ERROR:  Error installing pg:

ERROR: Failed to build gem native extension.

/home/User/.rvm/rubies/ruby-1.9.2-preview3/bin/ruby extconf.rb

checking for pg_config... yes
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)

*** 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/User/.rvm/rubies/ruby-1.9.2-preview3/bin/ruby
 --with-pg
 --without-pg
 --with-pg-config
 --without-pg-config
 --with-pg-dir
 --without-pg-dir
 --with-pg-include
 --without-pg-include=${pg-dir}/include
 --with-pg-lib
 --without-pg-lib=${pg-dir}/lib
 --enable-static-build
 --disable-static-build
 --with-pqlib
 --without-pqlib
 --with-libpqlib
 --without-libpqlib
 --with-ms/libpqlib
 --without-ms/libpqlib

Gem files will remain installed in /home/user/.rvm/gems/ruby-1.9.2-preview3/gems/pg-0.9.0 for inspection.

Results logged to /home/user/.rvm/gems/ruby-1.9.2-preview3/gems/pg-0.9.0/ext/gem_make.out

I don't know what is the error...

palani
  • 4,661
  • 8
  • 31
  • 36

19 Answers19

730

You need install the postgreSQL dev package with header of PostgreSQL

sudo apt-get install libpq-dev
Jonah
  • 9,991
  • 5
  • 45
  • 79
shingara
  • 46,608
  • 11
  • 99
  • 105
  • I did that as per your suggestion... the get the follwoing Need to get 0B of archives. After unpacking 5378kB will be freed. Do you want to continue? [Y/n/?] Y Writing extended state information... Done (Reading database ... 166183 files and directories currently installed.) Removing bison ... Removing libnss3-dev ... Removing libnspr4-dev ... Removing libqt4-core ... Removing libqt4-test ... Removing libsqlite3-dev ... Processing triggers for man-db .. – palani Jun 25 '10 at 07:17
  • 3
    For those really new to Ubuntu as me, but really new use `sudo apt-get install aptitude` first :) – Haris Krajina Oct 19 '12 at 11:44
  • 1
    tried all sort of combinations. even after reinstalling postgres and libpq . no luck.. still the same problem – Krishna Prasad Varma Feb 14 '13 at 12:19
  • sudo apt-get install libpq-dev works perfectly .... the nativ installer ofcourse needs the dev libs to compile the ruby "pg" gem. – andre baresel Apr 09 '14 at 12:39
  • 6
    For Cent/RedHat-flavored distros, the package you'll want is `yum install postgresql-devel` – Kyril Jul 01 '14 at 19:53
  • 1
    I have a feeling this may not being working for some people due to their Ubuntu versions. This is failing for me on 13.04 with Postgres 9.1 – Rebs Jun 23 '15 at 06:40
  • Worked on debian as well ;) – installero Sep 12 '15 at 19:08
  • Thank you! I had a similar problem and your solution fixed it for me. ;) – John Cowan Feb 23 '18 at 00:14
42

After reading and thrashing around for 2 days, and trying many things found in other notes the following single line was the cure for me on Ubuntu Lucid 10.04 mixed with some Maverick packages and RVM (ruby 1.9.2-p290, rvm 1.10.2 rubygems 1.8.15, rails 3.0.1, postgres 8.4.10) :

gem install pg  --   --with-pg-lib=/usr/lib   

the result:

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

{yea - finally success} !! !note that the output from running pg_config lacks the item -lpq in the LIBS variable on my Ubuntu / Postresql install!!

and why the switch from pq to pg in certain places -- confusing to newbie ??

the thing I still do not understand is the double set of -- and --with(option but I'm way beyond my depth anyway

StackExchange User
  • 1,222
  • 14
  • 35
jdupont
  • 421
  • 4
  • 2
  • 2
    the '--' is commonly used to separate the parameters to the first command from the second, in this case, the configure script of the extmod – Timothy Meade Mar 24 '12 at 23:10
  • thanks a lot for sharing this solution. was stuck on this for a day. glad i encountered this post. – whizcreed Jul 25 '12 at 09:29
34

In Ubuntu this works for me, I hope help you:

sudo apt-get install libpq-dev

and

gem install pg  --   --with-pg-lib=/usr/lib 
Marcos Riveros
  • 534
  • 4
  • 10
30

I'm on Ubuntu 12.10 and running this command:

apt-get install libpq-dev

helped me - after that ran gem install pg -v "0.14.1", and all good now

fotanus
  • 19,618
  • 13
  • 77
  • 111
Xander
  • 955
  • 1
  • 11
  • 15
29

Installing libpq-dev did not work for me. I also needed to install build-essential

sudo apt-get install libpq-dev build-essential
philwhln
  • 387
  • 3
  • 6
27

Need to add package

sudo apt-get install libpq-dev

to install pg gem in RoR

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Sartaj Singh Sisodiya
  • 1,143
  • 12
  • 10
16

Simple solution for ubuntu users...

First uninstall all postgres packages, then run these commads...

sudo apt-get install postgresql
sudo apt-get install postgresql-client libpq5 libpq-dev

# for rvm (single user)
mv ~/.rvm/usr/lib ~/.rvm/usr/lib_rvm 

# for rvm (multi-user)
mv /usr/local/rvm/usr/lib /usr/local/rvm/usr/lib_rvm

gem install pg  --   --with-pg-lib=/usr/lib

Then run 'bundle install'. Every thing will be fine. Have a good day!

  • 1
    If people are curious on how to remove all postgres packages, sudo apt-get remove --purge postgresql postgresql-9.3 (or whatever version of postgres you have). –  Jul 21 '15 at 22:36
13

sudo apt-get install postgresql-client libpq5 libpq-dev then do gem install pg

Gotey
  • 449
  • 4
  • 15
  • 41
  • Please add some context to your answer as to why this works. This also only works on a debian based system – JensV Aug 18 '20 at 14:15
9

For .RVM users it will be better:

rvmsudo gem install pg -- --with-pg-lib=/usr/lib 

it worked for me (after i saw jdupont version)

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
vekozlov
  • 664
  • 9
  • 19
7

If you have libpq-dev installed and are still having this problem it is likely due to conflicting versions of OpenSSL's libssl and friends - the Ubuntu system version in /usr/lib (which libpq is built against) and a second version RVM installed in $HOME/.rvm/usr/lib (or /usr/local/rvm/usr/lib if it's a system install). You can verify this by temporarily renaming $HOME/.rvm/usr/lib and seeing if "gem install pg" works.

To solve the problem have rvm rebuild using the system OpenSSL libraries (you may need to manually remove libssl.* and libcrypto.* from the rvm/usr/lib dir):

rvm reinstall 1.9.3 --with-openssl-dir=/usr

This finally solved the problem for me on Ubunto 12.04.

Mike Blackwell
  • 487
  • 1
  • 6
  • 12
  • Thank you for this answer! The newer the Ubuntu and the older the ruby build the more likely an OpenSSL mismatch will occur. – mikeycgto Feb 08 '16 at 15:48
  • Nothing else worked except for this! Thank you so much @mike-blackwell. I had almost given up hope before you came to the rescue. – varagrawal Apr 20 '17 at 04:25
6

Try this

sudo apt-get install postgresql postgresql-contrib libpq-dev

You should install PG Database server in the first place to install clients. Afterwards, you install clients.

See this blog post to know about setting up PostGresSQL for the first time for Ruby on Rails development.

Shiva
  • 11,485
  • 2
  • 67
  • 84
5

This solved the problem for me on Ubuntu 12.04 after every suggestion here had failed.

rvmsudo gem install pg -v '0.12.2' -- --with-pg-lib=/usr/include/postgresql
olafurg
  • 73
  • 1
  • 5
  • My solution was almost the same; I'm using rbenv and what worked for me was: ` gem install pg -- --with-pg-lib=/usr/lib/postgresql/ --with-pg-include=/usr/include/postgresql/` ; I didn't need to specify a version. – Dan Tenenbaum May 07 '15 at 18:46
3

I was trying to setup a Rails project in my freshly installed Ubuntu 16.04. I ran into the same issue while running bundle. Running

sudo apt-get install aptitude

followed by

sudo apt-get install libpq-dev

Solved it for me.

bishal
  • 676
  • 7
  • 12
3

For anyone who is still having issues after trying all the answers on this page, the following (finally) worked:

sudo apt-get install libgmp3-dev
gem install pg

This was after doing everything else mentioned on this page.

postgresql 9.5.8
Ubuntu 16.10

marksiemers
  • 631
  • 8
  • 14
2

Another solution to this problem is to install PostgreSQL using Homebrew/linuxbrew:

brew install postgresql

As a matter of habit I don't like to use sudo unless I have to.

Braden Mugg
  • 57
  • 1
  • 6
2

For those trying to install Redmine, I missed sudo apt-get install ruby-all-dev after trying all of the above.

Initial error being mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
1

I had the same problem, and tried a lot of different variants. After some tries I became able to sudo gem install, but still have problem to install it without sudo.
Finally I found a decission - reinstalling of rvm helped me. Probably it can save time somebody else.

RaskolnikOFF
  • 196
  • 2
  • 9
1

apt-get install ruby-dev worked for me.

ssss4world
  • 31
  • 1
  • 5
0

Another option is to use Homebrew which works on Linux and macOS to install just the supporting libraries:

brew install libpq

then

brew link libpq --force

(the --force option is required because it conflicts with the postgres formula.)

Andrew
  • 12,991
  • 15
  • 55
  • 85