PostgreSQL gem for rails won't install, even when both home brew and app are installed
Asked Answered
D

6

8

I have tried following this answer to get the gem to work, but it wont. I have my projects set up such that individuals projects have there own gems instead of all themes gems living in global space, and then I use binstubs to allow me to do things like bin/rails.

So all gems are installed to .bundle/gems/ for each project. the one that always gives me the toughest problems is posgresql. Lets go through the steps.

So I run:

bundle

It explodes stating:

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

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
Using config values from /Applications/Postgres.app/Contents/Versions/9.3/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
    --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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
    --with-pg
    --without-pg
    --enable-windows-cross
    --disable-windows-cross
    --with-pg-config
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/
    --with-pqlib
    --without-pqlib
    --with-libpqlib
    --without-libpqlib
    --with-ms/libpqlib
    --without-ms/libpqlib


Gem files will remain installed in /Users/Adam/Documents/Rails-Projects/AisisPlatform/.bundle/gems/gems/pg-0.18.1 for inspection.
Results logged to /Users/Adam/Documents/Rails-Projects/AisisPlatform/.bundle/gems/gems/pg-0.18.1/ext/gem_make.out
An error occurred while installing pg (0.18.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.1'` succeeds before bundling.

So then, because I have the home brew version installed at 9.4.0 and the posgresql.app installed I then did:

bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

Followed by, because I use 18.1

gem install pg -v '0.18.1'

And I got:

Building native extensions.  This could take a while...
Successfully installed pg-0.18.1
invalid options: -f fivefish
(invalid options are ignored)
Parsing documentation for pg-0.18.1
Done installing documentation for pg after 2 seconds
1 gem installed

From there I tried bundle Well we are back to square one because even though the gem installed - I get the EXACT same error.

Is this because I am installing the pg gem globally and not locally? How can I fix this? This one project, every time I have to do a rm -rf .bundle/gems Causes this exact issue over and over again.

I should point out I get the same error even if I use the home brew psql pg_config Do all the steps above, just with home brew version, same results.

Damar answered 19/1, 2015 at 20:43 Comment(1)
Have you tried any of the solutions here ?Simper
D
15

At the end of the day it was:

ARCHFLAGS="-arch x86_64" bundle install

that worked for me.

reason being:

by default it tries to compile a universal binary, which apparently fails... so that environment variable makes it only compile the x86 version which is all you need

you can add this line to your ~/.profile or similar: export ARCHFLAGS="-arch x86_64"

For further reading see: This README for OSX

Damar answered 19/1, 2015 at 21:12 Comment(1)
Thanks for posting this question and followup answer!I'm still a little unclear as to exactly why this fixes it, but it seems like we're now telling it to use the 64 bit architecture, and this workaround is only needed because something related to the pg gem isn't correctly compiled as universal binaries.Beery
M
10

Make sure Postgres is installed on your computer first

for Ubuntu systems: sudo apt-get install libpq-dev on RHEL systems: yum install postgresql-devel for Mac: brew install postgresql

Then run bundle install

Marienthal answered 24/1, 2015 at 3:50 Comment(0)
P
3

I used these commnads of this link

rails 4.2.0: can't install pg gem on ubuntu 14.04

You need install the postgreSQL dev package with header of PostgreSQL

sudo apt-get install libpq-dev

You may also try

sudo apt-get install postgresql-client

sudo apt-get install postgresql postgresql-contrib

Poultryman answered 22/5, 2015 at 4:50 Comment(0)
S
0

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

Simper answered 19/1, 2015 at 20:50 Comment(3)
The rbenv-bundler plugin may be helpful as well if you are using rbenvSimper
Exact same error. I have no idea what is going on or why this project is being such a loser. The gem installed fine, the bundle failed saying I need to make sure the gem installs first ... among the rest of the error which I outlined above.Damar
I am using rvm, and I am using ruby 2.2.0. All my projects use RVM. For simplicity, as stated, all gems for each project are installed to .bundle/gems/ to avoid any clashing in the global space. I also use binstubs to allow me to do bin/rails instead of rails No other project that uses pg gem and 2.2.0 is facing this issue and I have three other projects.Damar
L
-1

Try install postgresql-devel package:

yum -y install postgresql-devel

Notice that the above command wont't ask for permission because of the -y

Lye answered 19/3, 2015 at 20:30 Comment(0)
B
-1

Confirming Antarr Byrd's answer,

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

worked for me.

Beshore answered 7/5, 2015 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.