Rails 3 - can't install pg gem
Asked Answered
D

16

100

When I try to run bundle (bundle install), I all the time get

Installing pg (0.13.2) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /Users/ryan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby 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=/Users/ryan/.rvm/rubies/ruby-1.9.2-p290/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 /Users/ryan/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2 for inspection.
Results logged to /Users/ryan/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2/ext/gem_make.out
An error occured while installing pg (0.13.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.13.2'` succeeds before bundling.

I use Mac OS X 10.6, the version of installed PostgreSQL is 9.1. I found the problem is in the libpq-dev, how could I install/fix that?

Demasculinize answered 12/3, 2012 at 14:17 Comment(5)
how have you install postgres on your mac ?Immoderation
this wayDemasculinize
on_click installer ? Flink ? or MacPort ?Immoderation
I tried to install that by this way: sudo port install libpq-dev, but another problem - Error: Port libpq-dev not found To report a bug, see <http://guide.macports.org/#project.tickets>. That's terrible, still some problems...Demasculinize
See this question: [here][1] [1]: #10321689Welcher
M
78

As stated in your error log you need to pass in the path to the pg_config. Try to install the gem using:

gem install pg -- --with-pg-config= 'PATH_TO_YOUR_PG_CONFIG'

If you are not sure where your pg_config is, and assuming you are on Linux or Mac, you can run the following command:

which pg_config

Your pg-config can be in different locations depending on how you installed postgres.

Mallorca answered 12/3, 2012 at 15:3 Comment(9)
As a note, the binary package from the Postgres site does not contain development headers or the pg_config program.Jeanne
The binary package from here: enterprisedb.com/products-services-training/pgdownload#osx most certainly does contain both pg_config (e.g., at /Library/PostgreSQL/9.1/bin/pg_config for 9.1.x) and the development headers. It's what I use to test the pg gem in development.Insolvable
Wouldn't which pg_config be faster than find / -name pg_config?Gorski
Made a minor tweak to your command line args. Replacing the version with your current one should work ok. Here's my command line: sudo env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-include=/Library/PostgreSQL/9.1/include/ --with-pg-lib=/Library/PostgreSQL/9.1/lib/Frigorific
On OSX you may also use brew to get the headers via brew install libpqxx and everything will go fine.Sporran
For some reason which did not find the location of the pg_config file. for me it was located at /Library/PostgreSQL/9.1/bin/pg_config. just in case this may help someone elseEntryway
Mine was gem install pg -- --with-pg-config=/Library/PostgreSQL/9.2/bin/pg_configLuminescent
If you're using the POSTGRES app from Heroku, the pg_config is located at /Applications/Postgres.app/Contents/MacOS/bin/pg_configCantle
You can change this to: gem install pg -- --with-pg-config=`which pg_config`Emplane
F
100

if you are running on Linux you may be interested in what worked for me:

sudo apt-get install postgresql
sudo apt-get install libpq-dev

Then

gem install pg

then

bundle install

src: http://wikimatze.de/installing-postgresql-gem-under-ubuntu-and-mac

Felt answered 29/5, 2012 at 1:24 Comment(6)
thank, as it's in my case - no need to run bundle gem install pgKamalakamaria
Worked for me as of November 2013 running Ubuntu 13.04!Ego
why does this have so many upvotes when it's clearly for the wrong OS?Dede
@Dede it clearly states the OS it should be used on. And clearly a lot of people with Linux OSes are finding this answer when they research this problem.Felt
This should work if you use the repositories. Obviously it won't work if you compile from source. But it worked beautifully on Ubuntu 14.04 when using the repositories.Atterbury
That made it work. Key point being that postgresql needs to be installed on your machine.Nels
M
78

As stated in your error log you need to pass in the path to the pg_config. Try to install the gem using:

gem install pg -- --with-pg-config= 'PATH_TO_YOUR_PG_CONFIG'

If you are not sure where your pg_config is, and assuming you are on Linux or Mac, you can run the following command:

which pg_config

Your pg-config can be in different locations depending on how you installed postgres.

Mallorca answered 12/3, 2012 at 15:3 Comment(9)
As a note, the binary package from the Postgres site does not contain development headers or the pg_config program.Jeanne
The binary package from here: enterprisedb.com/products-services-training/pgdownload#osx most certainly does contain both pg_config (e.g., at /Library/PostgreSQL/9.1/bin/pg_config for 9.1.x) and the development headers. It's what I use to test the pg gem in development.Insolvable
Wouldn't which pg_config be faster than find / -name pg_config?Gorski
Made a minor tweak to your command line args. Replacing the version with your current one should work ok. Here's my command line: sudo env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-include=/Library/PostgreSQL/9.1/include/ --with-pg-lib=/Library/PostgreSQL/9.1/lib/Frigorific
On OSX you may also use brew to get the headers via brew install libpqxx and everything will go fine.Sporran
For some reason which did not find the location of the pg_config file. for me it was located at /Library/PostgreSQL/9.1/bin/pg_config. just in case this may help someone elseEntryway
Mine was gem install pg -- --with-pg-config=/Library/PostgreSQL/9.2/bin/pg_configLuminescent
If you're using the POSTGRES app from Heroku, the pg_config is located at /Applications/Postgres.app/Contents/MacOS/bin/pg_configCantle
You can change this to: gem install pg -- --with-pg-config=`which pg_config`Emplane
C
58

If you're using Postgress.app then youll want access to its command-line tools. Enter the following line in your terminal or your PATH profile:

 PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"

gem install pg should now work. (This is what worked for me.)

Note New versions path looks like:

/Applications/Postgres.app/Contents/Versions/<version>/bin
Coinsurance answered 27/10, 2013 at 18:55 Comment(6)
Thanks so much! I spent half an hour going through things that did not work and finally found this buried down here. :DGadmann
Great solution that doesn't require brew.Hydrolysis
Just be aware that the Postgre.app patch changed in the 93 version. it is now: /Applications/Postgres93.app/Contents/MacOS/binOverhang
This worked for me, though my bin was in a very different place -> /Library/PostgreSQL/9.3/binJonellejones
Postgres.app explains the updated path to use in this documentation page: postgresapp.com/documentation/cli-tools.htmlEucalyptol
This is now - /Applications/Postgres.app/Contents/Versions/9.3/bin as of 9.3.5 if you are using the Postgres.appPast
S
22

If you have homebrew, just type:

$ brew install postgresql

If you dont have it, download typing this in your terminal:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Stepsister answered 11/2, 2014 at 13:33 Comment(0)
G
12

Search for libpq:

brew search libpq

Should output libpqxx

Then try install it:

brew install libpqxx
Germaun answered 21/8, 2015 at 4:35 Comment(0)
O
8

You only need to install libpq-dev:

sudo apt-get install libpq-dev

Then the gem should install just fine.

Oversew answered 23/7, 2012 at 17:43 Comment(1)
In my case only this helped. Thanx!Charnel
M
7

Follow the post-installation instructions: http://postgresapp.com/documentation/configuration-ruby.html

To install the pg gem, make sure you have set up your $PATH correctly (as specified in http://postgresapp.com/documentation/cli-tools.html), then run

sudo ARCHFLAGS="-arch x86_64" gem install pg

I strongly recommend reading both pages. Just skimmed them and lost 1 hour of my life. Read them, problem solved.

Malleus answered 1/10, 2014 at 1:19 Comment(3)
Works on OSX 10.9.5, default ruby version 2.0, rbenv local version 1.9.3.Germaun
Works on OSX 10.11 THANKSCampball
Works on OSX 10.11.4. Thanks!!Allergen
S
3

The problem I had was that for some reason it was trying to compile with /usr/bin/gcc-4.2. I found that out by changing try_cpp in mkmf.rb (which I saw in the stack trace) to raise an exception with the compile line that was failing.

I soft-linked gcc to gcc-4.2 and it worked:

sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

Why was it trying to use gcc-4.2? No idea.

The actual compile line:

/usr/bin/gcc-4.2 -E -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/x86_64-darwin11.4.0 -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/ruby/backward -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/usr/local/Cellar/postgresql/9.1.4/include  -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration  -fno-common -pipe  conftest.c -o conftest.i (RuntimeError)
Saturant answered 30/7, 2012 at 18:45 Comment(0)
A
3

I got the same issue but my Postgres was install in

/Library/PostgreSQL/9.3

Updated the ~/.bash_profile by adding:

export PATH=/Library/PostgreSQL/9.3/bin:$PATH

Open a new terminal, run bundle update and also it worked for me. Thanks Ari.

Anserine answered 9/12, 2013 at 20:30 Comment(1)
but for me to work need to check the version then export PATH=/Applications/Postgres.app/Contents/Versions/9.4/bin:$PATHLavolta
B
1

You can set a build config option bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config and use gem install pg without any options (the path to pg_config may be different for you, this one is for the Postgresapp 9.3.5.0)

Benevolent answered 1/9, 2014 at 14:8 Comment(0)
R
1

I had the same problem in my openSUSE13.1 KDE machine. Before that issue I encountered I had installed only the packages postgresql and postgresql-server using zypper command. Then installed again 2 more packages :

[arup@to_do_app]$ sudo zypper in postgresql-devel postgresql-contrib
root's password:
Loading repository data...
Reading installed packages...
Resolving package dependencies...
#....

Then, I ran again bundle install, and success!!!

Recognizance answered 20/12, 2014 at 19:26 Comment(0)
A
0

For a working OSX solution see this guide http://krugerdavid.com/journal/how-to-install-xcode-homebrew-git-rvm-postgresql-ruby-1-9-3-on-snow-leopard/

It will walk you through installing PostgreSQL using homebrew. Tested and working for me on OSX 10.8.3, PostgreSQL 9.2.3, and Ruby 2.0.0-p0

Antoninaantonino answered 26/3, 2013 at 22:36 Comment(0)
C
0

First, uninstall any Homebrew versions. The --force option makes it uninstall all versions.

brew rm postgresql --force

Change paths accordingly for your version.

sudo /sbin/SystemStarter stop postgresql-8.4
sudo rm -rf /Applications/PostgreSQL\ 8.4
sudo rm -rf /etc/postgres-reg.ini
sudo rm -rf /Library/StartupItems/postgresql-8.4
sudo rm -rf /Library/PostgreSQL/8.4
sudo dscl . delete /users/postgres

Edit /etc/profile and delete any lines that reference "postgres".

nano /etc/profile

Install PostgresSQL

brew update
brew install postgresql

Install de PG GEM

gem install pg

That's it. Regards.

Collincolline answered 2/12, 2013 at 20:43 Comment(0)
S
0

You can first check to see if you have a postrgresql file in your terminal by going to lib file. going cd ~/opt/local/lib/ and then type ls and the enter button. This will show you a list of all the files that are located in the lib directory.

1.if you dont have postreseql you can download through macports. sudo port install postgresql93 @9.3.2_1

Now cd back into your folder which you are trying to bundle install

  1. getting your pg to work with your postgesql file you have or just downloaded gem install pg -- --with-pg-config=/opt/local/lib/postgresql93/bin/pg_config

now run bundle install

Szymanowski answered 10/2, 2014 at 8:51 Comment(0)
C
0

On Fedora:

dnf install postgresql-devel
Commingle answered 6/8, 2016 at 12:6 Comment(0)
S
0

What worked for me on El Capitan was upgrading ruby from the system default to 2.3.1 seemed to find the correct libraries that the pg gem needed.

Sunflower answered 15/8, 2016 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.