How to install PostgreSQL's pg gem on Ubuntu?
Asked Answered
G

19

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

Gemstone answered 25/6, 2010 at 6:31 Comment(1)
apt install postgresql-server-dev-all for Ubuntu 18.04.Parceling
U
731

You need install the postgreSQL dev package with header of PostgreSQL

sudo apt-get install libpq-dev
Unbuckle answered 25/6, 2010 at 6:56 Comment(8)
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 ..Gemstone
For those really new to Ubuntu as me, but really new use sudo apt-get install aptitude first :)Austine
tried all sort of combinations. even after reinstalling postgres and libpq . no luck.. still the same problemSoneson
sudo apt-get install libpq-dev works perfectly .... the nativ installer ofcourse needs the dev libs to compile the ruby "pg" gem.Boony
For Cent/RedHat-flavored distros, the package you'll want is yum install postgresql-develPoche
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.1Gregale
Worked on debian as well ;)Heterogenesis
Thank you! I had a similar problem and your solution fixed it for me. ;)Sheridansherie
A
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

Abb answered 17/2, 2012 at 22:53 Comment(2)
the '--' is commonly used to separate the parameters to the first command from the second, in this case, the configure script of the extmodAnticipate
thanks a lot for sharing this solution. was stuck on this for a day. glad i encountered this post.Wares
C
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 
Clabber answered 6/3, 2018 at 15:49 Comment(0)
A
30

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

sudo apt-get install libpq-dev build-essential
Agraffe answered 4/9, 2012 at 18:40 Comment(0)
S
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

Stoppage answered 15/1, 2013 at 15:3 Comment(0)
L
27

Need to add package

sudo apt-get install libpq-dev

to install pg gem in RoR

Longe answered 8/1, 2016 at 8:51 Comment(0)
C
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!

Cowbane answered 23/5, 2014 at 19:19 Comment(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).Reis
A
13

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

Arginine answered 18/8, 2020 at 11:33 Comment(1)
Please add some context to your answer as to why this works. This also only works on a debian based systemAlgicide
L
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)

Linearity answered 15/3, 2012 at 17:37 Comment(0)
U
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.

Upmost answered 26/3, 2013 at 21:22 Comment(2)
Thank you for this answer! The newer the Ubuntu and the older the ruby build the more likely an OpenSSL mismatch will occur.Billen
Nothing else worked except for this! Thank you so much @mike-blackwell. I had almost given up hope before you came to the rescue.Phosphide
A
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.

Abeyta answered 3/7, 2017 at 3:47 Comment(0)
P
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
Photocopier answered 7/11, 2012 at 21:55 Comment(1)
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.Berl
A
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.

Asseverate answered 1/9, 2016 at 3:59 Comment(0)
B
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

Blackout answered 29/8, 2017 at 23:13 Comment(1)
This was what I needed on Ubuntu 18.04Ringlet
L
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.

Lowther answered 18/7, 2015 at 3:12 Comment(3)
Homebrew is only for Mac.Ranchero
There's a Linux fork.Numerology
If you don't want to install the full PostgreSQL, the following worked for me: brew install libpq && brew link libpq --forceColza
A
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.

Awash answered 25/10, 2016 at 10:34 Comment(0)
I
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.

Interlingua answered 29/3, 2013 at 12:17 Comment(0)
D
1

apt-get install ruby-dev worked for me.

Deuteragonist answered 18/10, 2018 at 2:57 Comment(0)
C
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.)

Colza answered 13/3, 2020 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.