unable to install pg gem
Asked Answered
O

27

259

I tried using gem install pg but it doesn't seem to work.

gem install pg gives this error

Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

C:/Ruby/bin/ruby.exe extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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=C:/Ruby/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


Gem files will remain installed in C:/Ruby/lib/ruby/gems/1.8/gems/pg-0.10.1 for
inspection.
Results logged to C:/Ruby/lib/ruby/gems/1.8/gems/pg-0.10.1/ext/gem_make.out
Objective answered 28/1, 2011 at 10:19 Comment(3)
@NatchiQ broken link?Leddy
in my case the error log said libpq was not found, so i installed sudo apt install postgresql postgresql-contrib libpq-dev pgadmin3 -yDogwood
Try opening a new terminal tab and trying again. That worked for me. I had just installed psql, and the window I was trying to install the gem in didn't have psql in its path.Composite
P
14

Answered here: Can't install pg gem on Windows

There is no Windows native version of latest release of pg (0.10.0) released yesterday, but if you install 0.9.0 it should install binaries without issues.

Pistoia answered 28/1, 2011 at 10:34 Comment(0)
I
445

I had this problem, this worked for me:

Install the postgresql-devel package, this will solve the issue of pg_config missing.

sudo apt-get install libpq-dev
Interconnect answered 23/5, 2011 at 10:9 Comment(8)
Worked for me by apt-get install libpq-dev. Thanks for the hint.Conductance
and for me installing postgresql-server-dev-8.4 on ubuntu LTS 10Pounce
Confirmation, from the pg gem Wiki homepage: "On Ubuntu, /usr/bin/pg_config is provided by the libpq-dev package."Bellyband
postgresql-devel doesn't exist for debian squeeze. We used postgresql-server-dev-8.4 and that worked.Ingunna
Worked for me in openSUSE zypper in postgresql-devel. Fixed both the config & the libpq.Decretive
Had the same issue on Fedora 17 this answer solved it (I used yum install postgres-devel).Prussiate
@Fivell Late to the game with the answer here, but if you have Homebrew installed, brew install postgresql will get you the packages you need.Medial
@Fivell try brew install postgresql on macReckford
R
112

Issue is gem dependency so before installing pg make sure you have installed "libpq-dev"

Ubuntu systems:

sudo apt-get install libpq-dev

RHEL systems:

yum install postgresql-devel

Mac:

brew install postgresql

Reckford answered 9/6, 2015 at 9:53 Comment(2)
The answer for Mac is true. It's just because we haven't installed PostgreSQL on the machine.Lovelovebird
On Centos 7 yum install postgresql-devel solved my error related to pg_config for installing the 'pg' .gem. By the way I opted to use the just released PostgreSQL 10Bifoliate
D
69

gem install pg -- --with-pg-config=/usr/pgsql-9.1/bin/pg_config

Diplomate answered 10/2, 2012 at 23:21 Comment(8)
This helped me (on Centos 6.2)Amortization
Worked for me on OS X, but with path to /Applications/Postgres.app/Contents/MacOS/bin/pg_config (I have a standalone Postgres.app)Franciscafranciscan
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config on yosemite with Postgres AppRefutative
Worked. But on Mac, preceding with env ARCHFLAGS="-arch x86_64" was a game changer for me.Pincenez
Worked for me on OS X 10.11 with psql app 9.6 and gem version 0.18.4 gem install pg -v '0.18.4' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.6/bin/pg_configCalcium
I used gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config for OSX 10.12Hemostat
This help me on OS X with no brew support (using postgres installer)Nail
macOS Monterey M1. Added to .zshrc export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/14/bin/ and gem install pg worked and could then `bundle install in RailsParris
W
46

@Winfield said it:

The pg gem requires the postgresql client libraries to bind against. This error usually means it can't find your Postgres libraries. Either you don't have them installed or you may need to pass the --with-pg-dir= to your gem install.

More than that, you only need --with-pg-config= to install it.

On a Mac

If, by any chance, you also installed postgres through the website bundle on mac, it will be on somewhere like /Applications/Postgres.app/Contents/Versions/9.3/bin.

So, either you pass it on the gem install:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

Or you set the PATH properly. Since that might be too much, to temporarily set the PATH:

export PATH=%PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin/
Warbler answered 26/9, 2014 at 22:39 Comment(2)
for specific version: gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_configRelic
There is a symlink folder called latest inside of the Postgres.app contents folder that is useful in case 9.3 is no longer shipped. gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_configRedwood
S
29

This worked in my case:

sudo apt-get install libpq-dev

I used:

  • Ubuntu 14.04.2 LTS
  • Ruby 2.2.2
  • Rails 4.2.1
Salzman answered 16/6, 2015 at 13:53 Comment(0)
W
25

I hadn't postgresql installed, so I just installed it using

sudo apt-get install postgresql postgresql-server-dev-9.1

on Ubuntu 12.04.

This solved it.


Update:

Use the latest version:

sudo apt-get install postgresql-9.3 postgresql-server-dev-9.3
Waxy answered 8/9, 2012 at 19:49 Comment(1)
Fixed the problem for me but on a mac (with homebrew) I had to run this command: brew install postgresqlBanks
P
14

Answered here: Can't install pg gem on Windows

There is no Windows native version of latest release of pg (0.10.0) released yesterday, but if you install 0.9.0 it should install binaries without issues.

Pistoia answered 28/1, 2011 at 10:34 Comment(0)
E
14

If you are using Postgres.app on Mac, you may resolve this issue once and for all like this:

First gem uninstall pg, then edit your ~/.bash_profile or ~/.zshrc file or equivalent and add:

# PostgreSQL bin path
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin

Then bundle install and gem install pg should both work as expected.

Epiclesis answered 4/8, 2015 at 10:11 Comment(2)
Thanks, it also works using brew installed postgreSQL # PostgreSQL bin path export PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/Musical
Thanks! I switched from brew installed postgres to Postgress.app and had this problemBunghole
U
11

On macOS (El Capitan). You can simply use: brew install postgresql

Unprofitable answered 7/1, 2016 at 16:50 Comment(1)
Yes! This was all I needed! Install on clean OS X, no Postgres.appMacule
B
10
$ PATH=$PATH:/Library/PostgreSQL/9.1/bin sudo gem install pg

replace the 9.1 for the version installed on your system.

Blackberry answered 29/2, 2012 at 5:52 Comment(2)
This helped me on a mac, anyone know what the default path is and how to change it?Teeny
Works on OS X MavericVoe
R
4
  • Ubuntu 20.10 (pop!_os)
  • Ruby 2.7.2
  • Rails 3.1.0
  • Postgresql 12

Uninstall and then reinstall postgresql-client libpq5 libpq-dev

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

Then install the pg gem again pointing at /usr/lib to find the pg library:

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

Output (what you should see after the previous command):

Building native extensions with: '--with-pg-lib=/usr/lib'
This could take a while...
Successfully installed pg-1.2.3
Parsing documentation for pg-1.2.3
Installing ri documentation for pg-1.2.3
Done installing documentation for pg after 1 seconds
1 gem installed

Gem should install, then continue with normal bundle install or update:

bundle
bundle install
bundle update
Runaway answered 28/1, 2021 at 14:39 Comment(0)
H
3

The pg gem requires the postgresql client libraries to bind against. This error usually means it can't find your Postgres libraries. Either you don't have them installed or you may need to pass the --with-pg-dir= to your gem install.

Haughay answered 28/1, 2011 at 18:14 Comment(0)
F
3

For Mac Users

PATH=$PATH:/Library/PostgreSQL/9.4/bin/ gem install pg

This should do the trick

Famagusta answered 10/3, 2015 at 5:40 Comment(0)
K
3

Use with ARCH flag.

sudo env ARCHFLAGS="-arch x86_64" gem install pg

This resolved the same issue you are having.

Knowledgeable answered 11/1, 2016 at 17:42 Comment(0)
L
2

I'd this issue on Linux Mint (Maya) 13, And I fixed it by Installing postgresql and postgresql-server :

apt-get install postgresql-9.1 

sudo apt-get install postgresql-server-dev-9.1
Leveridge answered 2/5, 2013 at 8:48 Comment(0)
N
2

Regardless of what OS you are running, look at the logs file of the "Makefile" to see what is going on, instead of blindly installing stuff.

In my case, MAC OS, the log file is here:

/Users/za/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-1.0.0/mkmf.log

The logs indicated that the make file could not be created because of the following:

Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers

Inside the mkmf.log, you will see that it could not find required libraries, to finish the build.

checking for pg_config... no
Can't find the 'libpq-fe.h header
blah blah

After running "brew install postgresql", I can see all required libraries being there:

za:myapp za$ cat /Users/za/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-1.0.0/mkmf.log | grep yes
find_executable: checking for pg_config... -------------------- yes
find_header: checking for libpq-fe.h... -------------------- yes
find_header: checking for libpq/libpq-fs.h... -------------------- yes
find_header: checking for pg_config_manual.h... -------------------- yes
have_library: checking for PQconnectdb() in -lpq... -------------------- yes
have_func: checking for PQsetSingleRowMode()... -------------------- yes
have_func: checking for PQconninfo()... -------------------- yes
have_func: checking for PQsslAttribute()... -------------------- yes
have_func: checking for PQencryptPasswordConn()... -------------------- yes
have_const: checking for PG_DIAG_TABLE_NAME in libpq-fe.h... -------------------- yes
have_header: checking for unistd.h... -------------------- yes
have_header: checking for inttypes.h... -------------------- yes
checking for C99 variable length arrays... -------------------- yes
Newson answered 15/1, 2018 at 20:46 Comment(0)
M
2

I've been experiencing this annoying problem with PG for years. I created this gist to help.

The following command always work for me.

# Substitute Postgres.app/Contents/Versions/9.5 with appropriate version number
sudo ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config

gist: https://gist.github.com/sharnie/5588340cf023fb177c8d

Molality answered 21/3, 2018 at 14:34 Comment(0)
E
1

I had to do this on CentOS 5.8. Running bundle install kept causing issues since I couldn't force it to use a particular PG version.

I can't yum erase postgresql postgresql-devel either, because of dependency issues (it would remove php, http etc)

The solution? Mess $PATH temporarily to give preference to the update pgsql instead of the default one:

export PATH=/usr/pgsql-9.2/bin:$PATH
bundle install

Basically, with the above commands, it will look at /usr/pgsql-9.2/bin/pg_config before the one in /usr/bin/pg_config

Euglena answered 18/10, 2012 at 7:17 Comment(0)
A
1

If you are using jruby instead of ruby you will have similar issues when installing the pg gem. Instead you need to install the adaptor:

gem 'activerecord-jdbcpostgresql-adapter'
Alfonse answered 16/6, 2015 at 19:26 Comment(0)
T
1

On Mac brew install postgres THEN bundle install

Thrown answered 7/6, 2016 at 20:59 Comment(0)
C
1

I'm on Linux (Pop_OS) 20.10 and using a version manager (asdf) for Ruby (and others) and after trying a million different ways to sort it including all of the above

TLDR

gem install pg -v '1.2.3' -- --with-pg-config='/home/username/.asdf/installs/postgres/12.6/bin/pg_config'

Find the PG install for the right version and point to the pg_config in the bin directory

Cleisthenes answered 3/5, 2021 at 3:21 Comment(0)
C
0

You just go to here to see if your pg version support Win32 platform, then use this command to install:

gem install pg -v 0.14.1 --platform=x86-mingw32

Chinoiserie answered 4/1, 2013 at 6:29 Comment(0)
M
0

If you are using asdf to install postgres, just install pg gem like this:

asdf global postgres <version>
gem install pg
Manda answered 17/3, 2022 at 23:13 Comment(0)
G
0

Note that this library is called libpq-devel on some other Linux distrubitions, like Fedora, RHEL, etc. So it'd be sudo dnf install libpq-devel on Fedora.

https://unix.stackexchange.com/questions/594466/how-to-install-libpq-dev-on-fedora

Glottic answered 16/6, 2023 at 16:6 Comment(0)
S
0

If you are using MacOS, check the mkmf.log to see whether it requires an xcode licence

Run:

sudo xcodebuild -license

and type agree in the prompt.

Plus if you have installed postgres.

where pg_config

Then

gem install pg:1.4.5 -- --with-pg-config='/usr/local/opt/libpq/bin/pg_config'

Hope it helps

Sherrod answered 19/6, 2023 at 4:52 Comment(0)
F
0

For me bundle install was failing (on pg gem). I did:

brew install libpq
brew link --force libpq

then

bundle install

successfully installed pg.

Flyman answered 24/2 at 5:6 Comment(0)
E
0

In my case, I installed postgresql@15 by brew (yes, this is my macbook) like this

brew install postgresql@15
brew services start postgresql@15

And when I tried gem install pg I saw above error. But I was able to successfully install after adding this to my ~/.bashrc

export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"

I found this line from the brew info postgresql@15 command

Espy answered 7/3 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.