Reputation: 5721
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
Upvotes: 261
Views: 182505
Reputation: 6090
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
Upvotes: 1
Reputation: 52198
For me bundle install
was failing (on pg gem). I did:
brew install libpq
brew link --force libpq
then
bundle install
successfully installed pg.
Upvotes: 1
Reputation: 2068
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
Upvotes: 0
Reputation: 501
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
Upvotes: 0
Reputation: 21549
If you are using asdf
to install postgres
, just install pg
gem like this:
asdf global postgres <version>
gem install pg
Upvotes: 0
Reputation: 1036
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
Upvotes: 2
Reputation: 824
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
Upvotes: 4
Reputation: 1516
On macOS (El Capitan). You can simply use: brew install postgresql
Upvotes: 11
Reputation: 410
This worked in my case:
sudo apt-get install libpq-dev
I used:
Upvotes: 29
Reputation: 151
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
Upvotes: 2
Reputation: 7679
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
Upvotes: 2
Reputation: 18390
@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.
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/
Upvotes: 46
Reputation: 5743
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
Upvotes: 114
Reputation: 627
Use with ARCH
flag.
sudo env ARCHFLAGS="-arch x86_64" gem install pg
This resolved the same issue you are having.
Upvotes: 3
Reputation: 7083
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.
Upvotes: 14
Reputation: 67
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'
Upvotes: 1
Reputation: 8023
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
Upvotes: 448
Reputation: 1783
For Mac Users
PATH=$PATH:/Library/PostgreSQL/9.4/bin/ gem install pg
This should do the trick
Upvotes: 3
Reputation: 3390
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
Upvotes: 25
Reputation: 348
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
Upvotes: 2
Reputation: 1045
$ PATH=$PATH:/Library/PostgreSQL/9.1/bin sudo gem install pg
replace the 9.1 for the version installed on your system.
Upvotes: 10
Reputation: 31
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
Upvotes: 0
Reputation: 28125
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
Upvotes: 1
Reputation: 19145
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.
Upvotes: 3
Reputation: 2984
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.
Upvotes: 14