unable to install pg gem

Ruby on-RailsRubyRubygemsInstallation

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

@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/

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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

Solution 7 - Ruby on-Rails

Answered here: https://stackoverflow.com/questions/4335750/cant-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.

Solution 8 - Ruby on-Rails

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.

Solution 9 - Ruby on-Rails

$ PATH=$PATH:/Library/PostgreSQL/9.1/bin sudo gem install pg

replace the 9.1 for the version installed on your system.

Solution 10 - Ruby on-Rails

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

Solution 11 - Ruby on-Rails

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.

Solution 12 - Ruby on-Rails

For Mac Users

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

This should do the trick

Solution 13 - Ruby on-Rails

Use with ARCH flag.

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

This resolved the same issue you are having.

Solution 14 - Ruby on-Rails

  • 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

Solution 15 - Ruby on-Rails

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

Solution 16 - Ruby on-Rails

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

Solution 17 - Ruby on-Rails

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

Solution 18 - Ruby on-Rails

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

Solution 19 - Ruby on-Rails

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'

Solution 20 - Ruby on-Rails

On Mac brew install postgres THEN bundle install

Solution 21 - Ruby on-Rails

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

Solution 22 - Ruby on-Rails

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

Solution 23 - Ruby on-Rails

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

asdf global postgres <version>
gem install pg

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
QuestionRohitView Question on Stackoverflow
Solution 1 - Ruby on-RailsDevaroopView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMahattamView Answer on Stackoverflow
Solution 3 - Ruby on-RailsFlaviuView Answer on Stackoverflow
Solution 4 - Ruby on-RailscregoxView Answer on Stackoverflow
Solution 5 - Ruby on-RailsZainalView Answer on Stackoverflow
Solution 6 - Ruby on-RailsLasse Skindstad EbertView Answer on Stackoverflow
Solution 7 - Ruby on-RailsNikita BarsukovView Answer on Stackoverflow
Solution 8 - Ruby on-RailsDarmeView Answer on Stackoverflow
Solution 9 - Ruby on-RailsjstnnoView Answer on Stackoverflow
Solution 10 - Ruby on-RailsJones AgyemangView Answer on Stackoverflow
Solution 11 - Ruby on-RailsWinfieldView Answer on Stackoverflow
Solution 12 - Ruby on-RailsRonak JainView Answer on Stackoverflow
Solution 13 - Ruby on-RailsAman ChhabraView Answer on Stackoverflow
Solution 14 - Ruby on-RailsCoreyView Answer on Stackoverflow
Solution 15 - Ruby on-RailsSAIDI BelkacemView Answer on Stackoverflow
Solution 16 - Ruby on-Railsz atefView Answer on Stackoverflow
Solution 17 - Ruby on-RailsSharnieIveryView Answer on Stackoverflow
Solution 18 - Ruby on-RailsChristianView Answer on Stackoverflow
Solution 19 - Ruby on-RailsNuriView Answer on Stackoverflow
Solution 20 - Ruby on-RailsYar HTUTView Answer on Stackoverflow
Solution 21 - Ruby on-RailssaywhatnowView Answer on Stackoverflow
Solution 22 - Ruby on-RailsrevskillView Answer on Stackoverflow
Solution 23 - Ruby on-RailsSiweiView Answer on Stackoverflow