[ruby] How to install PostgreSQL's pg gem on Ubuntu?

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

This question is related to ruby postgresql pg

The answer is


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!


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.


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

sudo apt-get install libpq-dev build-essential

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)


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.


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


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


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 

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


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


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.


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.


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.


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.


apt-get install ruby-dev worked for me.


Need to add package

sudo apt-get install libpq-dev

to install pg gem in RoR


You need install the postgreSQL dev package with header of PostgreSQL

sudo apt-get install libpq-dev

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

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


Examples related to ruby

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew?

Examples related to postgresql

Subtracting 1 day from a timestamp date pgadmin4 : postgresql application server could not be contacted. Psql could not connect to server: No such file or directory, 5432 error? How to persist data in a dockerized postgres database using volumes input file appears to be a text format dump. Please use psql Postgres: check if array field contains value? Add timestamp column with default NOW() for new rows only Can't connect to Postgresql on port 5432 How to insert current datetime in postgresql insert query Connecting to Postgresql in a docker container from outside

Examples related to pg

PG::ConnectionBad - could not connect to server: Connection refused Can't find the 'libpq-fe.h header when trying to install pg gem How to install PostgreSQL's pg gem on Ubuntu?