cannot install pg gem, checking for PQconnectdb() in -lpq... no
Asked Answered
P

8

7

I'm trying to develop in Ruby on Rails using PostgreSQL locally, so I'm trying to install the pg gem. On Ubuntu 12.04. In terminal, I typed in:

gem install pg -v '0.12.2'

Which throws the following error:

Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

        /home/troyshu/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.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
    --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/troyshu/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
    --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
    --with-pqlib
    --without-pqlib
    --with-libpqlib
    --without-libpqlib
    --with-ms/libpqlib
    --without-ms/libpqlib


Gem files will remain installed in /home/troyshu/.rvm/gems/ruby-1.9.3-p194/gems/pg-0.14.1 for inspection.
Results logged to /home/troyshu/.rvm/gems/ruby-1.9.3-p194/gems/pg-0.14.1/ext/gem_make.out

Interestingly, adding a sudo to the command works:

$ sudo gem install pg -v '0.12.2'
Building native extensions.  This could take a while...
Successfully installed pg-0.12.2
1 gem installed

Btw I had to run "sudo apt-get install ruby1.9.1-dev" to get the above command to work. But I still can't do any development because "bundle install" will not work (and keeps telling me to run 'gem install pg -v '0.12.2')

I have a feeling "gem install pg" is getting tripped up at

checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no

but, as a complete noob to Ubuntu and RoR, I have no idea why. Maybe my search skills stink too, because I haven't been able to find a solution to my particular case. Hopefully there's a fix?

Thanks!

Paganism answered 21/9, 2012 at 14:22 Comment(4)
Are you sure libpq-dev is installed? I've posted an answer that explains how to do so.Ulda
Which versions of PostgreSQL and libpq-dev do you have installed?Ulda
So I just setup an Ubuntu 12.04 Vagrant box and ran the following commands as a non-root user: $ sudo apt-get update, $ sudo apt-get install postgresql libpq-dev build-essentials git-core curl, $ curl -L https://get.rvm.io | bash -s stable --ruby, and $ gem install pg -v 0.12.2` with no error (also, I installed the latest version of the pg gem, v0.14.1, without error; you may want to see if you can use the newest version, not that it will necessarily fix your issue).Ulda
Have you tried removing and reinstalling Postgres and libpq-dev? $ sudo apt-get autoremove postgresql libpq-dev if you installed using apt. While you're at it, you should consider using the latest version of Postgres: $ sudo add-apt-repository ppa:pitti/postgresql, $ sudo apt-get update, $ sudo apt-get install postgresql libpq-dev. Note: All of this assumes you're not rootUlda
C
3

Related: gem install pg can not bind to libpq. Please check your mkmf log and look for errors with libssl. I had the exact same problem. I had installed libpq-dev and double checked pg_config. It seems that the gem had found the libpq-dev header files but had failed to link with libpq. It turned out that the packages that rvm has installed for ssl were somehow interfering with the packages installed through libssl-dev.

Cathay answered 30/9, 2012 at 1:44 Comment(2)
Can you fill that out a bit? How did you resolve the problem - get rid of the "interfering" packages?Masseur
Downvote for 0 explanation on how you actually ended up resolving it.Harm
U
1

Based on the error message you posted, it looks like you don't have libpq-dev installed. Try apt-get install libpq-dev (with sudo if you're not root) and then installing the gem.

Since you're using RVM, if you ever have to use sudo to install a gem, you're doing it wrong. sudo installs the gem for using the system's Ruby, not the Ruby you installed using RVM.

Ulda answered 22/9, 2012 at 1:36 Comment(2)
Cool. I think I've run the command before though. I get libpq-dev is already the newest version. I also get a bunch of other output, not sure if any of it's relevant: The following packages were automatically installed and are no longer required: libsdl-ttf2.0-0 libboost-filesystem1.46.1 python-numpy python-pygame deluge-gtk libsdl-mixer1.2 libboost-system1.46.1 libboost-python1.46.1 python-libtorrent libportmidi0 deluge-common libmikmod2 libboost-thread1.46.1 libtorrent-rasterbar6 libsmpeg0 Still getting the same error after running gem install pg -v '0.12.2' :(Paganism
Yes, installed. No difference.Masseur
B
1

I found a strange solution but it worked for me instead of all other suggestions:

mv ~/.rvm/usr/lib ~/.rvm/usr/lib_rvm 
Buttons answered 11/3, 2013 at 16:4 Comment(0)
Y
0

if sudo works and a standard user does not, this suggests either that it is finding some set of libraries and header files somewhere which are not found as the other user or that there is actually a permissions issue that is not getting properly handled somewhere else. You could probably use strace to find the root of the problem if you want.

However, I think the saner solution is to recognize you probably need root access to install a lot of things like this anyway, and so if it were my system I would live with it.

Yapon answered 30/9, 2012 at 1:38 Comment(0)
M
0

I had a similar issue, with a failed bundle install:

Installing pg 0.17.1 with native extensions

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

...

An error occurred while installing pg (0.17.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.17.1'` succeeds before bundling.

but with gem install pg -v '0.17.1'.

I could successfully install the gem, but the bundle would still fail.

I fixed it by changing the pg bundle configurations:

  1. Check the pg configs:

    $ which pg_config returns:

    /usr/local/bin/pg_config

  2. Check current bundle build options for pg

$ bundle config build.pg

  1. If not the value returned by which pg_config,

$ bundle config build.pg --with-pg-config=/usr/local/bin/pg_config

Now try to bundle install!!!!

Martinmartina answered 10/10, 2015 at 17:46 Comment(0)
E
0

For me, the only thing that worked was installing rvm via the instructions on rvm's website: https://rvm.io. I was able to get it working on Ubuntu 14.04.

The full command list at this date:
sudo gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Make sure you are in a downloadable directory then run:
\curl -sSL https://get.rvm.io | bash -s stable

Eyeshade answered 5/9, 2017 at 16:27 Comment(0)
L
0

I encountered the same error and nothing seemed to work. Eventually discovered that its a anaconda related issue. If you are running anaconda, the gcc compiler defaults to that in the anaconda installation. This messes with the permissions. My error was fixed by doing the following:

  1. create a new conda environment: conda create --name postgresql
  2. Install postgresql with: conda install -c anaconda postgresql
  3. gem install pg

I am also running rbenv as ruby manager.. seems to work out fine. bundler install worked after installing the pg gem.

Lindeberg answered 4/8, 2021 at 18:5 Comment(1)
Maybe it doesn't mess with permissions so much as hide the extensions necessary for the pg gem to compile. Either way conda blocks gem.Lindeberg
H
-2

When you do sudo gem install pg the gem is installed wherever ruby and postgresql are installed; when you do gem install pg the gem is installed in your home directory (see your messages!). So, you need to do sudo bundle install blah-blah-blah for all your gems given the way you are working locally. There are other options, of course, like RVM, which I do not use.

Hobnailed answered 21/9, 2012 at 14:28 Comment(5)
Thanks, learning something new everyday. I'm a little confused though. Ruby's installed in my home directory: which ruby returns /home/troyshu/.rvm/rubies/ruby-1.9.3-p194/bin/ruby. So is bundle install the correct command to run (which doesn't work, per the error in my original post)?Paganism
Ah, you use rvm. That is great! Many find it useful. There is a complete doc page at the rvm website that explains how to do what you are trying to do. You want something like rvm exec gem install blah, or something like that.Hobnailed
Ok. I tried rvm 1.9.3 do gem install pg -v '0.12.2', but it throws the exact error as in my original post. I thought it might be a pg version error, so I tried just rvm 1.9.3 do gem install pg, which gave the same error.Paganism
To install a gem using RVM, a simple gem install ... is all that is needed. Anything else usually means you're doing it wrong.Ulda
Only if postgresql is installed where gem can find it.Hobnailed

© 2022 - 2024 — McMap. All rights reserved.