How to install PostgreSQL's pg gem on Ubuntu?

RubyPostgresqlPg

Ruby Problem Overview


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

Ruby Solutions


Solution 1 - Ruby

You need install the postgreSQL dev package with header of PostgreSQL

sudo apt-get install libpq-dev

Solution 2 - Ruby

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

Solution 3 - Ruby

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 

Solution 4 - Ruby

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

Solution 5 - Ruby

Need to add package

sudo apt-get install libpq-dev

to install pg gem in RoR

Solution 6 - Ruby

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

sudo apt-get install libpq-dev build-essential

Solution 7 - Ruby

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!

Solution 8 - Ruby

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

Solution 9 - Ruby

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)

Solution 10 - Ruby

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.

Solution 11 - Ruby

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.

Solution 12 - Ruby

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

Solution 13 - Ruby

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.

Solution 14 - Ruby

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

Solution 15 - Ruby

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.

Solution 16 - Ruby

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.

Solution 17 - Ruby

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.

Solution 18 - Ruby

apt-get install ruby-dev worked for me.

Solution 19 - Ruby

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpalaniView Question on Stackoverflow
Solution 1 - RubyshingaraView Answer on Stackoverflow
Solution 2 - RubyjdupontView Answer on Stackoverflow
Solution 3 - RubyMarcos RiverosView Answer on Stackoverflow
Solution 4 - RubyXanderView Answer on Stackoverflow
Solution 5 - RubySartaj Singh SisodiyaView Answer on Stackoverflow
Solution 6 - RubyphilwhlnView Answer on Stackoverflow
Solution 7 - RubyMaruf Hasan BulbulView Answer on Stackoverflow
Solution 8 - RubyGoteyView Answer on Stackoverflow
Solution 9 - RubyvekozlovView Answer on Stackoverflow
Solution 10 - RubyMike BlackwellView Answer on Stackoverflow
Solution 11 - RubyillusionistView Answer on Stackoverflow
Solution 12 - RubyolafurgView Answer on Stackoverflow
Solution 13 - RubybishalView Answer on Stackoverflow
Solution 14 - RubymarksiemersView Answer on Stackoverflow
Solution 15 - RubyBraden MuggView Answer on Stackoverflow
Solution 16 - RubyMatthieuView Answer on Stackoverflow
Solution 17 - RubyRaskolnikOFFView Answer on Stackoverflow
Solution 18 - Rubyssss4worldView Answer on Stackoverflow
Solution 19 - RubyAndrewView Answer on Stackoverflow