I'm trying to install PostgreSQL for Rails on Mac OS X 10.6. First I tried the MacPorts install but that didn't go well so I did the one-click DMG install. That seemed to work.
I suspect I need to install the PostgreSQL development packages but I have no idea how to do that on OS X.
Here's what I get when I try to do sudo gem install pg:
$ sudo gem install pg
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /Library/PostgreSQL/8.3/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.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/1.8/usr/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
--with-pqlib
--without-pqlib
--with-libpqlib
--without-libpqlib
--with-ms/libpqlib
--without-ms/libpqlib
Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/pg-0.11.0 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/pg-0.11.0/ext/gem_make.out
$ sudo su
$ env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions. This could take a while...
Successfully installed pg-0.11.0
1 gem installed
Installing ri documentation for pg-0.11.0...
Installing RDoc documentation for pg-0.11.0...
WORKED!
I tried the top-rated answer here:
env ARCHFLAGS="-arch x86_64" gem install pg
But when I tried running bundle install again, it had the same error. Then I tried the entire bundle install with ARCHFLAGS like so:
ARCHFLAGS="-arch x86_64" bundle install
Worked for me! Make sure to replace x86_64 with i386 depending on what architecture you have.
I was just having this problem when using the EnterpiseDB .dmg. If that's the same think you used, I got it to work by specifying the right architecture:
sudo env ARCHFLAGS="-arch i386" gem install pg
There are some tutorials on the web that said to specify a different architecture (like "-arch x86_64" for people who used MacPorts) but it wasn't working for me because I used the single file install.
If using Yosemite:
brew install postgres
Then:
ARCHFLAGS="-arch x86_64" gem install pg
And (optional) finally, if you want to launch autovacuum...
postgres -D /usr/local/var/postgres
Solution: reinstalled PostgreSQL with Homebrew.
Maybe you can try this one:
ARCHFLAGS="-arch i386 -arch x86_64" gem install pg
To know the architecture of your library you can use
file /usr/local/lib/libpq.dylib
which gave just 1 architecture in my case (installed via homebrew):
/usr/local/lib/libpq.dylib: Mach-O 64-bit dynamically linked shared library x86_64
The error message is right there:
Can't find the PostgreSQL client library (libpq)
You can fix that by using homebrew to install it
brew install libpq
The following, or a similar, message will appear:
If you need to have libpq first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
For compilers to find libpq you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"
You can then run it:
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
gem install pg
Done.
Most of those answers focus on giving you one command that just works.
However they only work for some particular scenarii.
If you have trouble figuring out which command you should run, you better understand what is happening.
Why doesn't it work?
The pg gem is a native extension written in C. It relies on the libpq library which is the PostgreSQL library for C applications.
So, during its build, the pg gem needs to be able to find the library libqp compiled for the same architecture (for example x86_64 or arm64).
There can be a mismatch if:
you are on a m1 macbook (apple silicon) but have installed postgres in rosetta 2 mode.
you are on a 64-bit computer but have installed postgres in 32-bit mode.
If you end up in this situation, you have several options:
try to compile the pg gem in the same architecture as postgres
reinstall postgres to match the architecture in which the pg gem is built (by default it's the same as ruby's)
Example
For instance, in my case, I have a m1 macbook.
I have installed ruby in "rosetta 2" mode (x86_64) and postgres in "native" mode (arm64).
You can check the architecture with the lipo -info command:
which ruby
# You might need to use a different command if you use a ruby version manager
# "which ruby" if you use rvm
# "rbenv which ruby" if you use rbenv
# "asdf which ruby" if you use asdf
=> /Users/leo/.rbenv/versions/2.6.6/bin/ruby
lipo -info /Users/leo/.rbenv/versions/2.6.6/bin/ruby
=> /Users/leo/.rbenv/versions/2.6.6/bin/ruby is architecture: x86_64
which postgres
=> /opt/homebrew/bin/pg_config
lipo -info /opt/homebrew/bin/pg_config
=> Non-fat file: /opt/homebrew/bin/pg_config is architecture: arm64
To solve this, in my case, I just had to add to the PATH another version of postgres compiled in x86_64. It allowed the pg gem to be able to find binaries in the right architecture:
export PATH=/usr/local/Cellar/postgresql/13.2_1/bin:$PATH
gem install pg
# and finally 🎉
=> Building native extensions. 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
If you are getting this error in Mac M1 pro, Silicon chip
Issue:
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)
Solution:
Install libpq via homebrew and add it to the PATH
brew install libpq
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
OR ~/bash_profile if you use it instead.
Try:
bundle install # OR
gem install pg
This works for me.
Fake out gem by prefixing the appropriate environment variables. If you were installing from MacPorts, you should be able to walk through the following procedure:
% /opt/local/lib/postgresql91/bin/pg_config
BINDIR = /opt/local/lib/postgresql91/bin
DOCDIR = /opt/local/share/doc/postgresql
HTMLDIR = /opt/local/share/doc/postgresql
INCLUDEDIR = /opt/local/include/postgresql91
PKGINCLUDEDIR = /opt/local/include/postgresql91
INCLUDEDIR-SERVER = /opt/local/include/postgresql91/server
LIBDIR = /opt/local/lib/postgresql91
PKGLIBDIR = /opt/local/lib/postgresql91
LOCALEDIR = /opt/local/share/locale
MANDIR = /opt/local/share/man
SHAREDIR = /opt/local/share/postgresql91
SYSCONFDIR = /opt/local/etc/postgresql91
PGXS = /opt/local/lib/postgresql91/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/opt/local' '--sysconfdir=/opt/local/etc/postgresql91' '--bindir=/opt/local/lib/postgresql91/bin' '--libdir=/opt/local/lib/postgresql91' '--includedir=/opt/local/include/postgresql91' '--datadir=/opt/local/share/postgresql91' '--mandir=/opt/local/share/man' '--with-includes=/opt/local/include' '--with-libraries=/opt/local/lib' '--with-openssl' '--with-bonjour' '--with-readline' '--with-zlib' '--with-libxml' '--with-libxslt' '--enable-thread-safety' '--enable-integer-datetimes' '--with-ossp-uuid' 'CC=/usr/bin/gcc-4.2' 'CFLAGS=-pipe -O2 -arch x86_64' 'LDFLAGS=-L/opt/local/lib -arch x86_64' 'CPPFLAGS=-I/opt/local/include -I/opt/local/include/ossp'
CC = /usr/bin/gcc-4.2
CPPFLAGS = -I/opt/local/include -I/opt/local/include/ossp -I/opt/local/include/libxml2 -I/opt/local/include
CFLAGS = -pipe -O2 -arch x86_64 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wformat-security -fno-strict-aliasing -fwrapv
CFLAGS_SL =
LDFLAGS = -L/opt/local/lib -arch x86_64 -L/opt/local/lib -L/opt/local/lib -Wl,-dead_strip_dylibs
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgport -lxslt -lxml2 -lssl -lcrypto -lz -lreadline -lm
VERSION = PostgreSQL 9.1beta1
From there, pull out the LIBDIR, INCLUDEDIR, CPPFLAGS, LIBS and LDFLAGS (the one that I think will get you running is LIBDIR, however). Then you'd run:
setenv PATH /opt/local/lib/postgresql91/bin:${PATH}
sudo env LDFLAGS=-L`pg_config --libdir` CPPFLAGS=`pg_config --cppflags` gem install pg
That should do it for you. Let me know if it doesn't.
The problem we had was pretty weird.
ruby -v # was ok (rbenv)
gem -v # was ok (rbenv)
but when we did a bundle install in fact, bundler wasn't installed for the version of ruby that was installed by rbenv, so, when we typed bundle install, it used the bundler of the system.
So before running bundle install, be sure that you have installed bundler by running
gem install bundler
libpq from Postgres.app will not work on ARM-based Macs, as Postgres.app is not getting released for ARM (only Universal Intel/ARM packages get released, which is just x86_64 emulation on ARM64):
~ > file /Applications/Postgres.app/Contents/Versions/13/lib/libpq.dylib
/Applications/Postgres.app/Contents/Versions/13/lib/libpq.dylib: Mach-O 64-bit dynamically linked shared library x86_64
For this reason, I decided to install libpq separately from Homebrew:
brew install libpq
Then installing pg worked with this:
gem install pg -- --with-pq-dir=/opt/homebrew/opt/libpq/
(MacOS 11.6 Big Sur on MacBook Pro 13" 2020 with Apple M1 CPU)
I don't think you need the postgres development files, everything you need should have been included with your installer. It's more likely that the path they're installed to isn't in your environment path and therefore gem can't find them when it tries to compile pg.
You shouldn't have to run gem install pg as root, in fact if you do it's likely your PATH (root's PATH if run w/ sudo) won't contain the necessary info.
The following usually works for me:
# Might be different depending on where your installer installed postgres 8.3
export PATH=$PATH:/Library/PostgreSQL/8.3/include/
export ARCHFLAGS='-arch x86_64'
gem install pg
This is what finally did it for me (combination of multiple solutions provided before along with other posts):
$ sudo env ARCHFLAGS="-arch x86_64" gem install pg -- with-pg-include=/Library/PostgreSQL/9.6/include/
Well, I have tried all the solutions given above for
Can't find the PostgreSQL client library (libpq)
$ sudo su
$ env ARCHFLAGS="-arch x86_64" gem install pg
but nothing helped.
For this reason, I decided to install libpq separately from Homebrew:
brew install libpq
then found a small catch though: libpq won't install itself in the /usr/local/bin directory for mac. To make that happen, you need to run:
brew link --force libpq
Which will symlink all the tools, not just libpq, into the /usr/local/bin directory. You're ready to run psql and start connecting now.
The ARCHFLAGS answer that others have proposed will not work if you somehow ended up with a 64-bit version of postgres (which homebrew will install) and a 32-bit version of ruby. For some reason rbenv insists on building ruby 1.9.2-p290 as 32-bit for me, which makes it impossible to link against 64-bit postgres.
Check the architecture of your ruby binary with
file `which ruby`
or if using rbenv
file `rbenv which ruby`
And compare against your postgres:
file `which postgres`
If there's a mis-match you'll need to re-install postgres or ruby. With rbenv I solved this just by switching to a different version: 1.9.3-p194 instead of 1.9.2-p290.
This is how I made it to work on Mavericks. Note: I already had installed postgresql 9.3 from homebrew.
Update Xcode to 5.0 from App Store
Install command line developer tools
xcode-select --install
Agree to Xcode license
sudo xcodebuild -license
Install gem
ARCHFLAGS="-arch x86_64" gem install pg
So basically I did this ;-)
brew install postgres
For my mac m1 after trying all of above solution and nothing working. I made it work by:
Install Homebrew x86
brew x86 will locate on: /usr/local/Homebrew/bin/brew
brew arm64 locate on: /opt/homebrew/bin/brew (my current brew)
Install libpq with arch x86 ( Since pg must be compiled with x86 libpq )
arch --x86_64 /usr/local/Homebrew/bin/brew install libpq
Install gem pg
gem install pg -v '0.18.2' -- --with-pq-dir=/usr/local/Cellar/libpq/14.2 --with-pg-config=/usr/local/Cellar/libpq/14.2/bin/pg_config
I'm probably a bit late to the party here, but in my case I was using rbenv and upgrading to Ruby 2.2.3. I had to install Bundler to get mine to work, I had an old system version.
gem install bundler
As mentioned above this has to do with the fact of having two ruby archs on rbenv /usr/bin/ruby: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [i386:Mach-O executable i386] what i had to do was simply install pg gem forcing x86_64 arch to be used with this command:
sudo env ARCHFLAGS="-arch x86_64" gem install pg
Remember to have your bash_profile up to date
Add the path of your postgres, in this case im using Postgres app (OSX) instead of brew (https://postgresapp.com/) by default this is the location:
export PATH=/Applications/Postgres.app/Contents/Versions/10/bin:$PATH
Reload bash with
sudo vi ~/.bash_profile
After doing this i was able to finally successfully install pg gem
Hope this helps!
On Mac you can try this (works for me):
gem install pg -- with-pg-include=/Library/PostgreSQL/9.5/include
Fetching: pg-1.0.0.gem (100%)
Building native extensions with: 'with-pg-include=/Library/PostgreSQL/9.5/include'
This could take a while...
Successfully installed pg-1.0.0
Parsing documentation for pg-1.0.0
Installing ri documentation for pg-1.0.0
Done installing documentation for pg after 3 seconds
1 gem installed
(this part "/Library/PostgreSQL/9.5/include" you must put your Postgres path)
For those of you that tried the top answers and they didn't quite work, I thought I'd explain my situation and how it was fixed.
I was trying to use postgres for a Ruby project, using bundle install (and with a gem 'pg' in my gemfile), but I already had Postgres installed before, probably by using npm install pg or something like that.
I had an existing database, with data, and I was concerned that if I ran brew install postgres then it would wipe my existing data and tables.
It didn't! brew install postgres worked perfectly as suggested by #Jackie Chan
If you want to put your mind at ease though, you can back up your data as described at this link https://www.tecmint.com/backup-and-restore-postgresql-database/ e.g.
cd desktop
pg_dump -U your_db_user your_database_name >> file_containing_backup.sql
Related
I'm trying to install the pg gem in order to work again with my rails projects. But I get this error:
Building native extensions. This could take a while... ERROR: Error
installing pg: ERROR: Failed to build gem native extension.
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/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/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
--with-pg --without-pg --with-pg-config --without-pg-config
--with-pg_config --without-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}/
Gem files will remain installed in
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0
for inspection. Results logged to
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0/ext/gem_make.out
I tried everything I found on stackoverflow but I still get this error.
If I try to install postgresql using brew I get the following waring:
Warning: postgresql-9.2.4 already installed, it's just not linked
If I try to link
brew link postgresql Linking /usr/local/Cellar/postgresql/9.2.4...
Warning: Could not link postgresql. Unlinking...
Error: Could not symlink file:
/usr/local/Cellar/postgresql/9.2.4/share/man/man7/WITH.7
/usr/local/share/man/man7 is not writable. You should change its
permissions.
Help Please
NOTE: I already installed the command line tools for mavericks.
If I uninstall using homebrew and try to install again, I get this error:
==> Downloading http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.bz2
################################################################## 100.0%
==> Patching patching file src/pl/plpython/Makefile patching file contrib/uuid-ossp/uuid-ossp.c
==> ./configure --prefix=/usr/local/Cellar/postgresql/9.2.4 --datadir=/usr/local/Cellar/postgresql/9.2.4/share/postgresql --docdir=/usr/local/Cellar/p
==> make install-world
==> Caveats
Build Notes
If builds of PostgreSQL 9 are failing and you have version 8.x
installed, you may need to remove the previous version first. See:
https://github.com/mxcl/homebrew/issues/issue/2510
Create/Upgrade a Database
If this is your first install, create a database with: initdb
/usr/local/var/postgres -E utf8
To migrate existing data from a previous major version (pre-9.2) of
PostgreSQL, see:
http://www.postgresql.org/docs/9.2/static/upgrading.html
Loading Extensions
By default, Homebrew builds all available Contrib extensions. To see a
list of all available extensions, from the psql command line, run:
SELECT * FROM pg_available_extensions;
To load any of the extension names, navigate to the desired database
and run: CREATE EXTENSION [extension name];
For instance, to load the tablefunc extension in the current database,
run: CREATE EXTENSION tablefunc;
For more information on the CREATE EXTENSION command, see:
http://www.postgresql.org/docs/9.2/static/sql-createextension.html For
more information on extensions, see:
http://www.postgresql.org/docs/9.2/static/contrib.html
Other
Some machines may require provisioning of shared memory:
http://www.postgresql.org/docs/9.2/static/kernel-resources.html#SYSVIPC
When installing the postgres gem, including ARCHFLAGS is recommended:
ARCHFLAGS="-arch x86_64" gem install pg
To install gems without sudo, see the Homebrew wiki.
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Or, if you don't
want/need launchctl, you can just run:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start Warning: Could not link
postgresql. Unlinking... Error: The brew link step did not complete
successfully The formula built, but is not symlinked into /usr/local
You can try again using `brew link postgresql'
==> Summary 🍺 /usr/local/Cellar/postgresql/9.2.4: 2831 files, 38M, built in 4.9 minutes
SOLUTION:
I execute this command in order to change the permission of the folder:
sudo chown jeanosorio /usr/local/share/man/man7
Then
brew link postgresql Linking /usr/local/Cellar/postgresql/9.3.1... 421
symlinks created
And finally:
sudo ARCHFLAGS="-arch x86_64" gem install pg
Fetching: pg-0.17.0.gem (100%) Building native extensions. This could
take a while... Successfully installed pg-0.17.0
If you want to avoid using MacPorts, you can download the Postgres App and place it into the Application directory.
Then, specify the location of newly downloaded pg_config:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config
If you run in to missing headers problem, try specifying the include directory of the app:
gem install pg -- --with-pg-include='/Applications/Postgres.app/Contents/Versions/latest/include/'
Use brew to get postgresql
brew install postgresql
Check you have pg_config in the installed brew. I found mine in
/usr/local/Cellar/postgresql/9.3.3/bin/pg_config
Check via :
$ ls /usr/local/Cellar/postgresql/9.3.3/bin/pg_config
> /usr/local/Cellar/postgresql/9.3.3/bin/pg_config
Once done, install the pg gem with
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.3/bin/pg_config
I wasn't able to install postgres via MacPorts. Instead, I installed Postgress.app. and called
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Note: in newer versions (in 9.3 at least) the path is actually: /Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
If you have homebrew, just type:
$ brew install postgresql
For me, on Yosemite, I didn't need to specify pg_config path. The following got it done.
brew install postgresql
sudo env ARCHFLAGS="-arch x86_64" gem install pg
Seems like architecture flag is the key.
Here's another alternative in case you want to use the postgresql installer directly. There's a couple of hoops you gotta jump through to begin with after updating to mavericks. Here's what I did:
Install the xcode command line tools first:
xcode-select --install
Download and install the latest version of PostgreSQL (9.3.1), in my case I just used the graphical installer. Here's the link to the downloads page:
http://www.enterprisedb.com/products-services-training/pgdownload#osx
Just choose all of the defaults it gives you. It my case it installed postgres to the following directory, if you installed it to a different directory, just remember the path you chose, because you'll need it shortly.
/Library/PostgreSQL/9.3
If you now try and install the latest pg gem (0.17.0) you'll need to pass a couple of options on the command line. This is what I used:
ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
If you installed postgres to a different location then you'll need to fix up the path to the pg_config file.
If it complains of a missing 'libpq.5.dylib' file while trying install the pg gem you'll need to create a symlink pointing to the actual location of this file. Postgres is looking for it in your /usr/local/lib/ directory so create a symlink in there and redirect it to its actual location. In my case I had to run:
sudo ln -s /Library/PostgreSQL/9.3/lib/libpq.5.dylib /usr/local/lib/libpq.5.dylib
Once you've done that, retry installing the gem again and hopefully it will work for you the same way it did for me.
In my case (i needed PG 0.16.0 on Mavericks), i installed postgresql via MacPorts
sudo port install postgresql90
and then
gem install pg -v '0.16.0' -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config
For the latest version you need to deduct -v '0.16.0'
gem install pg -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config
If you have Homebrew installed do not install MacPorts before you read about their coexistense
Place the missing --with-pg-config in Bundler's config file: /Users/<your_user>/.bundle/config to make your life easier.
---
BUNDLE_BUILD__PG: --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
BUNDLE_BUILD__EXAMPLE_GEM: --other-configs
BUNDLE_BUILD__OTHER_EXAMPLE_GEM: --other-configs
Then run bundle install again.
That works for me! (Postgres 93, mac ox 10.9.1)1. download # http://postgresapp.com/ and than
gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
I was stuck on my bundle install for 3 days. Tried Everything like adding env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.5_1/bin/pg_config
I was able to see pg gem getting installed after this command but still it was not installing from bundle install, which was a pain because I dint know what to write in Gemfile except gem 'pg'
The thing which finally worked for me was to find that my pg_config was in /Library/PostgreSQL/9.3/bin/pg_config and by default the Gemfile bundle install looks in /usr/local/bin/pg_config
I just ran the following command and magic happened.
bundle config build.pg --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
In my case it didn't work if you don't specify the arch flags, OS X 10.10.3 and postersApp 9.4
sudo env ARCHFLAGS="-arch x86_64" gem install pg -- \--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
The easiest way for me to resolve this was to use Postgres.app and set up my env vars so gem install pg just works (instead of requiring the --with-pg-config flag).
Using Homebrew Cask,
brew cask install postgres
echo 'export PG_HOME=/Applications/Postgres.app/Contents/Versions/latest' >> ~/.bash_profile
echo 'export PATH="$PATH:$PG_HOME/bin"' >> ~/.bash_profile
source ~/.bash_profile
# now it just works
gem install pg
This is what it worked for me:
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
In my case, I uninstalled the version installed via homebrew and switched to Postgres.app (v9.3.4.2 at the time of writing).
It only seemed to work when prepending the environment architecture flags and specifying the path to pg_config. Your milage might vary, so this answer could help with variations on pg_config's location.
Here is the final, complete command that worked for me:
env ARCHFLAGS="-arch x86_64" gem install pg -- \
--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
sudo ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config
I had installed postgres using homebrew on mac. Used below command to find the location
which postgres
/usr/local/opt/postgresql#12/bin/postgres
Installed gem using below command. In the above path just replace the postgres with pg_config since command requires path for pg_config.
gem install pg -- --with-pg-config=/usr/local/opt/postgresql#12/bin/pg_config
I was getting the same error when running bundle install regarding the pg gem (using Mac OS X Mavericks). After hours of researching, I found the solution. I installed postgres using homebrew
brew install postgres
After installing that, I created a new rails app using postgres. I ran
bundle install
to no avail (got same error as the question). I used
which psql
to figure out where my postgres was installed, it returned
/usr/local/bin/psql
before running bundle install again, I had to change the global path to build.pg by running the following
bundle config build.pg --with-pg-config=/usr/local/bin/pg_config
then I ran bundle install again and voila! I used the postgres where brew installed it.
On OS X Mavericks with latest Postgres App do the following with sudo privileges
env ARCHFLAGS="-arch x86_64" gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
What worked for me on 10.9.3 with Xcode 5.1.1 was the following:
brew update
brew install postgresql
gem install pg -v '0.17.1'
I needed pg 0.17.1
For me, the error message looked like this:
Installing pg 0.18.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
As the error message shows, it's trying to execute the command/script "pg_config". So just add it to the path from wherever your Postgres installation is. In my case, I did:
export PATH=/Applications/Postgres.app/Contents/Versions/9.5/bin:$PATH
then
bundle
That's it.
For any lost soul outthere still having this issue with no resolution and have a brew installed ruby and postgres. Here's the following steps to make sure of:
Make sure your version of ruby wasn't installed with the --universal flag.
This was the hardest thing to figure out, I had --universal which was the final root of the problem.
Make sure you have initialized postgresql properly with initdb and createdb commands (look elsewhere for steps to do this).
Make sure Xcode (Optional) and command line tools is installed; on the terminal:
xcode-select --install
sudo xcodebuild -license (if Xcode installed) or open the application and accept the license agreement.
Reboot Computer
which ruby, which gem and which postgres should show /usr/local/bin/ruby, /usr/local/bin/gem and /usr/local/bin/postgres respectfully.
On the terminal run:
gem install pg -- --with-pg-config=/usr/local/bin/pg_config
or
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config
Making sure all these steps are satisfied I was able to solve the following errors:
ruby.h header error
developer tools installed first error
First of all find pg_config location
sudo find / -name "pg_config" -print
The answer is /Library/PostgreSQL/9.1/bin/pg_config in my configuration (MAC Maverick)
Then try installing something like below
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
if this does not work then try to check in what way you installed postgresql
brew/mac port/setup
then you have to try relevant option to the same.
Thank you.
Ram
I was with the same problem, this was the solution I've found.
If you run brew doctor, it will tell you that probably you have installed something without brew that have changed the permissions on some folders, and so, you need to change permissions back to you on that folder.
Inside /usr/local/share/man, what you can do is the following:
sudo chown [yourusername] man7
And then:
brew link postgres
Hope it helps!
Similarly, after installing Mavericks 'bundle update' was throwing an error on the pg gem, which is only used on production and not locally.
I use Brew to manage my packages and postgresql was already installed, but still I was getting the 'no pg_config' error.
The fix was to just 'brew uninstall postgresql', then 'brew install postgresql'. After which I was immediately able to successfully run 'bundle update'
Postgres 9.4:
gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/Versions/9.4/bin/pg_config
sudo su
then
ARCHFLAGS="-arch x86_64" gem install pg -v '0.18'
worked on 10.10.2
If after trying everything here it still won't install, try opening Xcode and accepting the license agreement.
I upgraded my OSX (Lion) to Mavericks and I can't install Nokogiri for my projects.
I already install XCode 5.0.1, Command Line Tools (using xcode-select --install), and already installed libxml2 from Homebrew and I am still having problems.
The error is:
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/bin/ruby extconf.rb
checking for libxml/parser.h... *** 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
--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/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/bin/ruby
--with-zlib-dir
--without-zlib-dir
--with-zlib-include
--without-zlib-include=${zlib-dir}/include
--with-zlib-lib
--without-zlib-lib=${zlib-dir}/lib
--with-iconv-dir
--without-iconv-dir
--with-iconv-include
--without-iconv-include=${iconv-dir}/include
--with-iconv-lib
--without-iconv-lib=${iconv-dir}/lib
--with-xml2-dir
--without-xml2-dir
--with-xml2-include
--without-xml2-include=${xml2-dir}/include
--with-xml2-lib
--without-xml2-lib=${xml2-dir}/lib
--with-xslt-dir
--without-xslt-dir
--with-xslt-include
--without-xslt-include=${xslt-dir}/include
--with-xslt-lib
--without-xslt-lib=${xslt-dir}/lib
--with-libxslt-config
--without-libxslt-config
--with-pkg-config
--without-pkg-config
--with-libxml-2.0-config
--without-libxml-2.0-config
--with-libiconv-config
--without-libiconv-config
/Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:368:in `try_do': The complier failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:452:in `try_cpp'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:853:in `block in find_header'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:693:in `block in checking_for'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:280:in `block (2 levels) in postpone'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:254:in `open'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:280:in `block in postpone'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:254:in `open'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:276:in `postpone'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:692:in `checking_for'
from /Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/mkmf.rb:852:in `find_header'
from extconf.rb:116:in `<main>'
Gem files will remain installed in /Users/ericcamalionte/Locaweb/code/dns-panel/vendor/bundle/ruby/1.9.1/gems/nokogiri-1.5.9 for inspection.
Results logged to /Users/ericcamalionte/Locaweb/code/dns-panel/vendor/bundle/ruby/1.9.1/gems/nokogiri-1.5.9/ext/nokogiri/gem_make.out
An error occured while installing nokogiri (1.5.9), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.5.9'` succeeds before bundling.
I installed libxml2, libxslt and libiconv from Homebrew, and set the params to install Nokogiri, but don't work too.
I can't find what's wrong in my enviroment, can you help me?
You can also install Nokogiri on Mac OS X 10.9 Mavericks with full XCode Install using:
gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2
Update
For those using Yosemite the following command will work:
gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2 --use-system-libraries
or, it might actually be in your MacOSX10.11.sdk folder (mine was as of 18-Sep-2015) anyways, so even if you are not yet fully up to El Capitan, I had recently updated XCode and you may need to use the El Capitan SDK path, which follows next:
Update
For those using El Capitan the following command will work:
gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2 --use-system-libraries
Update
For those using Sierra the following command will work:
gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2 --use-system-libraries
After navigating the animated GIFs here, all that I had to do was simply xcode-select --install and the gem install nokogiri worked fine.
I found this log and saw that gcc-4.2 was not found:
package configuration for libxslt
cflags: -I/usr/local/Cellar/libxslt/1.1.28/include -I/usr/local/Cellar/libxml2/2.9.1/include/libxml2
ldflags: -L/usr/local/Cellar/libxslt/1.1.28/lib -L/usr/local/Cellar/libxml2/2.9.1/lib
libs: -lxslt -lxml2 -lz -lpthread -liconv -lm -lxml2
package configuration for libxml-2.0
cflags: -I/usr/local/Cellar/libxml2/2.9.1/include/libxml2
ldflags: -L/usr/local/Cellar/libxml2/2.9.1/lib
libs: -lxml2
package configuration for libiconv is not found
"/usr/bin/gcc-4.2 -o conftest -I/Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/include/ruby-1.9.1/x86_64-darwin11.4.0 -I/Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/include/ruby-1.9.1/ruby/backward -I/Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/include/ruby-1.9.1 -I. -I/usr/local/Cellar/libxslt/1.1.28/include -I/usr/local/Cellar/libxml2/2.9.1/include/libxml2 -I/usr/local/Cellar/libiconv/1.14/include -I/Users/ericcamalionte/.rvm/gems/ruby-1.9.2-p320#dns-panel/gems/nokogiri-1.6.0/ports/x86_64-apple-darwin13.0.0/libxml2/2.8.0/include -I/Users/ericcamalionte/.rvm/gems/ruby-1.9.2-p320#dns-panel/gems/nokogiri-1.6.0/ports/x86_64-apple-darwin13.0.0/libxslt/1.1.26/include -I/Users/ericcamalionte/.rvm/usr/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wshorten-64-to-32 -Wno-long-long -fno-common -pipe -g -DXP_UNIX -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline -DNOKOGIRI_USE_PACKAGED_LIBRARIES -DNOKOGIRI_LIBXML2_PATH='"/Users/ericcamalionte/.rvm/gems/ruby-1.9.2-p320#dns-panel/gems/nokogiri-1.6.0/ports/x86_64-apple-darwin13.0.0/libxml2/2.8.0"' -DNOKOGIRI_LIBXSLT_PATH='"/Users/ericcamalionte/.rvm/gems/ruby-1.9.2-p320#dns-panel/gems/nokogiri-1.6.0/ports/x86_64-apple-darwin13.0.0/libxslt/1.1.26"' -I/usr/local/Cellar/libxslt/1.1.28/include -I/usr/local/Cellar/libxml2/2.9.1/include/libxml2 -I/usr/local/Cellar/libxml2/2.9.1/include/libxml2 conftest.c -L. -L/Users/ericcamalionte/.rvm/rubies/ruby-1.9.2-p320/lib -L/usr/local/Cellar/libxslt/1.1.28/lib -L/usr/local/Cellar/libxml2/2.9.1/lib -L/usr/local/Cellar/libiconv/1.14/lib -L/Users/ericcamalionte/.rvm/gems/ruby-1.9.2-p320#dns-panel/gems/nokogiri-1.6.0/ports/x86_64-apple-darwin13.0.0/libxml2/2.8.0/lib -L/Users/ericcamalionte/.rvm/gems/ruby-1.9.2-p320#dns-panel/gems/nokogiri-1.6.0/ports/x86_64-apple-darwin13.0.0/libxslt/1.1.26/lib -L/Users/ericcamalionte/.rvm/usr/lib -L. -Wl,-rpath,/Users/ericcamalionte/.rvm/gems/ruby-1.9.2-p320#dns-panel/gems/nokogiri-1.6.0/ports/x86_64-apple-darwin13.0.0/libxml2/2.8.0/lib -Wl,-rpath,/Users/ericcamalionte/.rvm/gems/ruby-1.9.2-p320#dns-panel/gems/nokogiri-1.6.0/ports/x86_64-apple-darwin13.0.0/libxslt/1.1.26/lib -L/usr/local/Cellar/libxslt/1.1.28/lib -L/usr/local/Cellar/libxml2/2.9.1/lib -L/usr/local/Cellar/libxml2/2.9.1/lib -lxslt -lxml2 -lz -lpthread -liconv -lm -lxml2 -lxml2 -lruby.1.9.1-static -lpthread -ldl -lobjc "
sh: /usr/bin/gcc-4.2: No such file or directory
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main() {return 0;}
/* end */
To solve this problem I intalled apple-gcc42 using homebrew brew install apple-gcc42 and created a symlink to my /usr/bin :
sudo ln -s /usr/local/Cellar/apple-gcc42/4.2.1-5666.3/bin/gcc-4.2 /usr/bin/gcc-4.2
If you're running Xcode 5.1, the command line tools don't work for nokogiri 1.6.1. You'll need to download the Late october 2013 tools from Apple. Once you do that run
sudo xcode-select -s /Library/Developer/CommandLineTools/
to set up your machine to use the Xcode 5.0.X command line tools, then run
gem install nokogiri
If you want to reset your command line tools to the Xcode.app version afterward run
sudo xcode-select -r
Or, another thing you can do is add the flag to ignore unknown command line arguments. Then the install looks like this:
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install nokogiri
This is what worked for me on OSX Mavericks:
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install nokogiri -v '1.6.1' --verbose --no-ri --no-rdoc
I'm on OSX Mavericks and my problem turned out to be a bad install of Ruby.
So, I reinstalled ruby with rvm :
rvm remove ruby 2.0.0p451
rvm remove ruby-2.0.0-p451 && rvm install ruby-2.0.0-p451
I was then able to
gem install nokogiri --no-ri --no-rdoc
Problem solved.
I like to stick with system-provided stuff typically. This worked for me:
gem install nokogiri -- --with-iconv-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/ --with-iconv-lib=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib/
I ran into this problem immediately after installing the Mavericks 10.9.5 update and the 10.9 Developer tools update from Apple. I ran xcode-select --install, but that did not fix the problem. Then I opened XCode, accepted the eula, and quit XCode. That fixed the problem.
I had this problem today, on Maverick, this is how I ended up solving the issue:
brew update
brew install libiconv
brew link libiconv
Make sure that you know the version of libiconv in Cellar mine below was 1.14, then install as below:
gem install nokogiri -- --with-iconv-dir=/usr/local/Cellar/libiconv/1.14
If you require a particular version of nokogiri e.g -v '1.6.2.1' then install as:
gem install nokogiri -v '1.6.2.1' -- --with-iconv-dir=/usr/local/Cellar/libiconv/1.14
Nokogiri was installed successfully!
This is the same as #thomas_witt's post but works on Mac OS X Sierra:
gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2 --use-system-libraries
To get the install to work, I had to modify the filepaths in the gem install command to match those on my system. I have different versions of libxml2 and libiconv and a slightly different file structure. The command, with my modifications, is:
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2
--with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib
--with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28
--with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include
--with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib
For people using MacPorts, make sure you have installed libxml2 via MacPorts. Then type:
bundle config build.nokogiri --use-system-libraries
bundle install
This should do the trick, worked for me without using full paths.
Use brew install libxml2 libxslt if you use Homebrew.
Here is another reference:
system: OS X Yosemite 10.10
rvm: 1.26.10
brew: 0.9.5
ruby: ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
I got error while installing nokogiri
error like this:
.rvm/rubies/ruby-2.2.0-p0/lib/ruby/1.9.1/mkmf.rb:368:in `try_do': The complier failed to generate an executable file. (RuntimeError)
You have to install development tools first.
then probably need:
$ xcode-select --install
or if you got error like
checking for libxml/parser.h... no
I just fix the lib path by:
gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 libxslt
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.2/include/libxml2/libxml --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.2/lib --with-iconv-dir=/usr/local/Cellar/libiconv/1.14/ --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
for bundle need something like:
bundle config build.nokogiri --with-xml2-include=/usr/local/Cellar/libxml2/2.9.2/include/libxml2/libxml --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.2/lib --with-iconv-dir=/usr/local/Cellar/libiconv/1.14/ --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
bundle install
This works for me
system: OS X Yosemite 10.10
rvm: 1.26.10
ruby: ruby 2.2.1
Just run below two commands
$ xcode-select --install
It will ask you to download say "yes" then it will ask to install the xcode component click install.
Now try to install gem with below command
gem install nokogiri --no-ri --no-rdoc
This works for me with above environment.
I ran into the same issue for Mavrick, and the solution was:
xcode-select --install
However, that is not working as Apple's download page for Command-line-tool does not have that installer available publically now.
So, if you want to have the command-line tools for Mavricks you need to have a paid account. Then only you will be able to install it.
Once you install it you will not face this issue with Nokogiri.
I had this same error on a fresh Mavericks install. After having a lapse of judgement I renamed my Xcode app bundle to Xcode 5.0.1.app.
Apparently the Nokogiri installation script does not quote its paths and so the space in the file name caused all sorts or troubles. It wasn't until I tried brew install libxml2 that the error became obvious.
Lesson learned: spaces in file names are evil.
In my case, I had to actually run /usr/bin/gcc-4.2 after symlinking it. You have to accept the license agreement, otherwise it hangs.
The error message gives a clue here: The compiler failed to generate an executable file. (RuntimeError)
xcode-select --install # not sure if this is required
brew install apple-gcc42
gem install nokogiri
You also might need to brew install and link these:
libxml2 libxslt
brew install libxml2 libxslt
gem install nokogiri -- \
--with-xml2-include=/usr/local/Cellar/libxml2/*/include/libxml2 \
--with-xml2-lib=/usr/local/Cellar/libxml2/*/lib \
--with-xslt-dir=/usr/local/Cellar/libxslt/*
For me the error was that that gcc (4.2.1, installed from Homebrew) was complaining that:
-E, -S, -save-temps and -M options are not allowed with multiple -arch flags
I solved the issue by forcing x86_64 only:
ARCHFLAGS="-arch x86_64" gem install nokogiri
After upgrading to Maverick, I had a similar problem. I use RVM with Ruby 1.9.2-p320, and I tried several solutions given here, but nothing solved the problem.
Then I changed to Ruby 2.1.2, and bundle install installed Nokogiri immediately.
If anyone is having this issue on el capitan, whilst using bundler.
Make sure the xcode command line tools are installed and run this:
bundle config build.nokogiri --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2 --use-system-libraries
I was facing the same issue from past two weeks while trying to run a rails 3 version application.
The issue is that your rvm/rbenv is not using the C compiler.
Use this command for rvm to take compatible C compiler
CC=gcc rvm install-version
So if you are using Ruby 1.9.3, use it this way
CC=gcc rvm install-1.9.3
Use this command every time you are not able to bundle install or anything.
This thing is working on El Capitan, rails 3.2.16, ruby 1.9.3, mysql 5.7
Hopefully, It will resolve the issue.
I was facing the same issue from past two weeks while trying to run a rails 3 version application.
The issue is that your rvm/rbenv is not using the C compiler.
Use this command for rvm to take compatible C compiler
CC=gcc rvm install-version
So if you are using Ruby 1.9.3, use it this way
CC=gcc rvm install-1.9.3
Use this command every time you are not able to bundle install or anything.
This thing is working on El Capitan, rails 3.2.16, ruby 1.9.3, mysql 5.7
Hopefully, It will resolve the issue.
And also if you already have the Xcode and then also you are not getting the same errors installing any gem, try this solution.
I added my findings here after I came across this same issue shortly after an Upgrade: http://jasdeep.ca/2013/10/installing-nokogiri-fails-os-x-mavericks/
The fix simply is these 2 commands:
xcode-select --install
gem install nokogiri
Hope it helps.
I had this issue as well. Running brew doctor showed that I had an unexpected version of libiconv in /user/local/lib.
Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
Unexpected dylibs:
/usr/local/lib/libcharset.1.dylib
/usr/local/lib/libiconv.2.dylib
So I removed them, and rebrewed libxml:
rm /usr/local/lib/libiconv*
andromeda:nokogiri-1.6.0 Jeff$ brew install libxml2 libxslt
...
==> Summary
🍺 /usr/local/Cellar/libxslt/1.1.28: 145 files, 3.3M, built in 36 seconds
Finally, I installed nokogiri:
Jeff$ gem install nokogiri
Building native extensions. This could take a while...
Successfully installed nokogiri-1.6.0
1 gem installed
None of these answers worked for my particular case, and since I am a newb I figured my solution might be able to help someone else.
I am using Yosemite and was using Ruby 1.9.3p547. There was a security exposure for p547 so I was trying to update to Ruby 1.9.3p550 or higher. I used RVM, then tried to migrate my gems and many of them didn't go. Then I tried to bundle install but that was failing and I thought maybe it was a conflict between the various paths and dependencies so I removed the older version of Ruby. This broke everything.
I kept getting a message saying that the C compiler was missing, even though I had downloaded XCode and recently updated the CLI tools. I found another website that told me to download a third party GCC, which I did. which made everything worse.
Now I'm getting a message saying I need to fix my profiles and PATHs or it might just be easier to reinstall OSX.
So I did.
Anyway, longer story merely long: the solution that finally worked for me was to get rid of RVM and just use brew to download the Ruby version I wanted then before running bundle install. Install the correct version of Nokogiri FIRST.
brew install ruby193
sudo gem install nokogiri -v '1.6.0'
bundle install
for windows you can also try local installation:
download the appropriate gem based on your environment.
go to the directory where you saved the gem file.
gem install --local nokogiri-1.6.3.1-x86-mingw32.gem
if not worked you may check if you have zlib and mingw or proper c compiler installed.
Every single darn thing on here didn't do it for me (I didn't get into brew reinstalls, oy), but this finally did (found via a bug report on some unrelated project):
gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/usr/include/libxml2
Good luck. Yay for #yakshaving!
Check your Commandline tools version. Set up your machine to use the Xcode 5.0.X command line tools, then run:
gem install nokogiri
I'm trying to install the pg gem in order to work again with my rails projects. But I get this error:
Building native extensions. This could take a while... ERROR: Error
installing pg: ERROR: Failed to build gem native extension.
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/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/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
--with-pg --without-pg --with-pg-config --without-pg-config
--with-pg_config --without-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}/
Gem files will remain installed in
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0
for inspection. Results logged to
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0/ext/gem_make.out
I tried everything I found on stackoverflow but I still get this error.
If I try to install postgresql using brew I get the following waring:
Warning: postgresql-9.2.4 already installed, it's just not linked
If I try to link
brew link postgresql Linking /usr/local/Cellar/postgresql/9.2.4...
Warning: Could not link postgresql. Unlinking...
Error: Could not symlink file:
/usr/local/Cellar/postgresql/9.2.4/share/man/man7/WITH.7
/usr/local/share/man/man7 is not writable. You should change its
permissions.
Help Please
NOTE: I already installed the command line tools for mavericks.
If I uninstall using homebrew and try to install again, I get this error:
==> Downloading http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.bz2
################################################################## 100.0%
==> Patching patching file src/pl/plpython/Makefile patching file contrib/uuid-ossp/uuid-ossp.c
==> ./configure --prefix=/usr/local/Cellar/postgresql/9.2.4 --datadir=/usr/local/Cellar/postgresql/9.2.4/share/postgresql --docdir=/usr/local/Cellar/p
==> make install-world
==> Caveats
Build Notes
If builds of PostgreSQL 9 are failing and you have version 8.x
installed, you may need to remove the previous version first. See:
https://github.com/mxcl/homebrew/issues/issue/2510
Create/Upgrade a Database
If this is your first install, create a database with: initdb
/usr/local/var/postgres -E utf8
To migrate existing data from a previous major version (pre-9.2) of
PostgreSQL, see:
http://www.postgresql.org/docs/9.2/static/upgrading.html
Loading Extensions
By default, Homebrew builds all available Contrib extensions. To see a
list of all available extensions, from the psql command line, run:
SELECT * FROM pg_available_extensions;
To load any of the extension names, navigate to the desired database
and run: CREATE EXTENSION [extension name];
For instance, to load the tablefunc extension in the current database,
run: CREATE EXTENSION tablefunc;
For more information on the CREATE EXTENSION command, see:
http://www.postgresql.org/docs/9.2/static/sql-createextension.html For
more information on extensions, see:
http://www.postgresql.org/docs/9.2/static/contrib.html
Other
Some machines may require provisioning of shared memory:
http://www.postgresql.org/docs/9.2/static/kernel-resources.html#SYSVIPC
When installing the postgres gem, including ARCHFLAGS is recommended:
ARCHFLAGS="-arch x86_64" gem install pg
To install gems without sudo, see the Homebrew wiki.
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Or, if you don't
want/need launchctl, you can just run:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start Warning: Could not link
postgresql. Unlinking... Error: The brew link step did not complete
successfully The formula built, but is not symlinked into /usr/local
You can try again using `brew link postgresql'
==> Summary 🍺 /usr/local/Cellar/postgresql/9.2.4: 2831 files, 38M, built in 4.9 minutes
SOLUTION:
I execute this command in order to change the permission of the folder:
sudo chown jeanosorio /usr/local/share/man/man7
Then
brew link postgresql Linking /usr/local/Cellar/postgresql/9.3.1... 421
symlinks created
And finally:
sudo ARCHFLAGS="-arch x86_64" gem install pg
Fetching: pg-0.17.0.gem (100%) Building native extensions. This could
take a while... Successfully installed pg-0.17.0
If you want to avoid using MacPorts, you can download the Postgres App and place it into the Application directory.
Then, specify the location of newly downloaded pg_config:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config
If you run in to missing headers problem, try specifying the include directory of the app:
gem install pg -- --with-pg-include='/Applications/Postgres.app/Contents/Versions/latest/include/'
Use brew to get postgresql
brew install postgresql
Check you have pg_config in the installed brew. I found mine in
/usr/local/Cellar/postgresql/9.3.3/bin/pg_config
Check via :
$ ls /usr/local/Cellar/postgresql/9.3.3/bin/pg_config
> /usr/local/Cellar/postgresql/9.3.3/bin/pg_config
Once done, install the pg gem with
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.3/bin/pg_config
I wasn't able to install postgres via MacPorts. Instead, I installed Postgress.app. and called
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Note: in newer versions (in 9.3 at least) the path is actually: /Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
If you have homebrew, just type:
$ brew install postgresql
For me, on Yosemite, I didn't need to specify pg_config path. The following got it done.
brew install postgresql
sudo env ARCHFLAGS="-arch x86_64" gem install pg
Seems like architecture flag is the key.
Here's another alternative in case you want to use the postgresql installer directly. There's a couple of hoops you gotta jump through to begin with after updating to mavericks. Here's what I did:
Install the xcode command line tools first:
xcode-select --install
Download and install the latest version of PostgreSQL (9.3.1), in my case I just used the graphical installer. Here's the link to the downloads page:
http://www.enterprisedb.com/products-services-training/pgdownload#osx
Just choose all of the defaults it gives you. It my case it installed postgres to the following directory, if you installed it to a different directory, just remember the path you chose, because you'll need it shortly.
/Library/PostgreSQL/9.3
If you now try and install the latest pg gem (0.17.0) you'll need to pass a couple of options on the command line. This is what I used:
ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
If you installed postgres to a different location then you'll need to fix up the path to the pg_config file.
If it complains of a missing 'libpq.5.dylib' file while trying install the pg gem you'll need to create a symlink pointing to the actual location of this file. Postgres is looking for it in your /usr/local/lib/ directory so create a symlink in there and redirect it to its actual location. In my case I had to run:
sudo ln -s /Library/PostgreSQL/9.3/lib/libpq.5.dylib /usr/local/lib/libpq.5.dylib
Once you've done that, retry installing the gem again and hopefully it will work for you the same way it did for me.
In my case (i needed PG 0.16.0 on Mavericks), i installed postgresql via MacPorts
sudo port install postgresql90
and then
gem install pg -v '0.16.0' -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config
For the latest version you need to deduct -v '0.16.0'
gem install pg -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config
If you have Homebrew installed do not install MacPorts before you read about their coexistense
Place the missing --with-pg-config in Bundler's config file: /Users/<your_user>/.bundle/config to make your life easier.
---
BUNDLE_BUILD__PG: --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
BUNDLE_BUILD__EXAMPLE_GEM: --other-configs
BUNDLE_BUILD__OTHER_EXAMPLE_GEM: --other-configs
Then run bundle install again.
That works for me! (Postgres 93, mac ox 10.9.1)1. download # http://postgresapp.com/ and than
gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
I was stuck on my bundle install for 3 days. Tried Everything like adding env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.5_1/bin/pg_config
I was able to see pg gem getting installed after this command but still it was not installing from bundle install, which was a pain because I dint know what to write in Gemfile except gem 'pg'
The thing which finally worked for me was to find that my pg_config was in /Library/PostgreSQL/9.3/bin/pg_config and by default the Gemfile bundle install looks in /usr/local/bin/pg_config
I just ran the following command and magic happened.
bundle config build.pg --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
In my case it didn't work if you don't specify the arch flags, OS X 10.10.3 and postersApp 9.4
sudo env ARCHFLAGS="-arch x86_64" gem install pg -- \--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
The easiest way for me to resolve this was to use Postgres.app and set up my env vars so gem install pg just works (instead of requiring the --with-pg-config flag).
Using Homebrew Cask,
brew cask install postgres
echo 'export PG_HOME=/Applications/Postgres.app/Contents/Versions/latest' >> ~/.bash_profile
echo 'export PATH="$PATH:$PG_HOME/bin"' >> ~/.bash_profile
source ~/.bash_profile
# now it just works
gem install pg
This is what it worked for me:
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
In my case, I uninstalled the version installed via homebrew and switched to Postgres.app (v9.3.4.2 at the time of writing).
It only seemed to work when prepending the environment architecture flags and specifying the path to pg_config. Your milage might vary, so this answer could help with variations on pg_config's location.
Here is the final, complete command that worked for me:
env ARCHFLAGS="-arch x86_64" gem install pg -- \
--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
sudo ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config
I had installed postgres using homebrew on mac. Used below command to find the location
which postgres
/usr/local/opt/postgresql#12/bin/postgres
Installed gem using below command. In the above path just replace the postgres with pg_config since command requires path for pg_config.
gem install pg -- --with-pg-config=/usr/local/opt/postgresql#12/bin/pg_config
I was getting the same error when running bundle install regarding the pg gem (using Mac OS X Mavericks). After hours of researching, I found the solution. I installed postgres using homebrew
brew install postgres
After installing that, I created a new rails app using postgres. I ran
bundle install
to no avail (got same error as the question). I used
which psql
to figure out where my postgres was installed, it returned
/usr/local/bin/psql
before running bundle install again, I had to change the global path to build.pg by running the following
bundle config build.pg --with-pg-config=/usr/local/bin/pg_config
then I ran bundle install again and voila! I used the postgres where brew installed it.
On OS X Mavericks with latest Postgres App do the following with sudo privileges
env ARCHFLAGS="-arch x86_64" gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
What worked for me on 10.9.3 with Xcode 5.1.1 was the following:
brew update
brew install postgresql
gem install pg -v '0.17.1'
I needed pg 0.17.1
For me, the error message looked like this:
Installing pg 0.18.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
As the error message shows, it's trying to execute the command/script "pg_config". So just add it to the path from wherever your Postgres installation is. In my case, I did:
export PATH=/Applications/Postgres.app/Contents/Versions/9.5/bin:$PATH
then
bundle
That's it.
For any lost soul outthere still having this issue with no resolution and have a brew installed ruby and postgres. Here's the following steps to make sure of:
Make sure your version of ruby wasn't installed with the --universal flag.
This was the hardest thing to figure out, I had --universal which was the final root of the problem.
Make sure you have initialized postgresql properly with initdb and createdb commands (look elsewhere for steps to do this).
Make sure Xcode (Optional) and command line tools is installed; on the terminal:
xcode-select --install
sudo xcodebuild -license (if Xcode installed) or open the application and accept the license agreement.
Reboot Computer
which ruby, which gem and which postgres should show /usr/local/bin/ruby, /usr/local/bin/gem and /usr/local/bin/postgres respectfully.
On the terminal run:
gem install pg -- --with-pg-config=/usr/local/bin/pg_config
or
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config
Making sure all these steps are satisfied I was able to solve the following errors:
ruby.h header error
developer tools installed first error
First of all find pg_config location
sudo find / -name "pg_config" -print
The answer is /Library/PostgreSQL/9.1/bin/pg_config in my configuration (MAC Maverick)
Then try installing something like below
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
if this does not work then try to check in what way you installed postgresql
brew/mac port/setup
then you have to try relevant option to the same.
Thank you.
Ram
I was with the same problem, this was the solution I've found.
If you run brew doctor, it will tell you that probably you have installed something without brew that have changed the permissions on some folders, and so, you need to change permissions back to you on that folder.
Inside /usr/local/share/man, what you can do is the following:
sudo chown [yourusername] man7
And then:
brew link postgres
Hope it helps!
Similarly, after installing Mavericks 'bundle update' was throwing an error on the pg gem, which is only used on production and not locally.
I use Brew to manage my packages and postgresql was already installed, but still I was getting the 'no pg_config' error.
The fix was to just 'brew uninstall postgresql', then 'brew install postgresql'. After which I was immediately able to successfully run 'bundle update'
Postgres 9.4:
gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/Versions/9.4/bin/pg_config
sudo su
then
ARCHFLAGS="-arch x86_64" gem install pg -v '0.18'
worked on 10.10.2
If after trying everything here it still won't install, try opening Xcode and accepting the license agreement.
I'm trying to install the pg gem in order to work again with my rails projects. But I get this error:
Building native extensions. This could take a while... ERROR: Error
installing pg: ERROR: Failed to build gem native extension.
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/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/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
--with-pg --without-pg --with-pg-config --without-pg-config
--with-pg_config --without-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}/
Gem files will remain installed in
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0
for inspection. Results logged to
/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0/ext/gem_make.out
I tried everything I found on stackoverflow but I still get this error.
If I try to install postgresql using brew I get the following waring:
Warning: postgresql-9.2.4 already installed, it's just not linked
If I try to link
brew link postgresql Linking /usr/local/Cellar/postgresql/9.2.4...
Warning: Could not link postgresql. Unlinking...
Error: Could not symlink file:
/usr/local/Cellar/postgresql/9.2.4/share/man/man7/WITH.7
/usr/local/share/man/man7 is not writable. You should change its
permissions.
Help Please
NOTE: I already installed the command line tools for mavericks.
If I uninstall using homebrew and try to install again, I get this error:
==> Downloading http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.bz2
################################################################## 100.0%
==> Patching patching file src/pl/plpython/Makefile patching file contrib/uuid-ossp/uuid-ossp.c
==> ./configure --prefix=/usr/local/Cellar/postgresql/9.2.4 --datadir=/usr/local/Cellar/postgresql/9.2.4/share/postgresql --docdir=/usr/local/Cellar/p
==> make install-world
==> Caveats
Build Notes
If builds of PostgreSQL 9 are failing and you have version 8.x
installed, you may need to remove the previous version first. See:
https://github.com/mxcl/homebrew/issues/issue/2510
Create/Upgrade a Database
If this is your first install, create a database with: initdb
/usr/local/var/postgres -E utf8
To migrate existing data from a previous major version (pre-9.2) of
PostgreSQL, see:
http://www.postgresql.org/docs/9.2/static/upgrading.html
Loading Extensions
By default, Homebrew builds all available Contrib extensions. To see a
list of all available extensions, from the psql command line, run:
SELECT * FROM pg_available_extensions;
To load any of the extension names, navigate to the desired database
and run: CREATE EXTENSION [extension name];
For instance, to load the tablefunc extension in the current database,
run: CREATE EXTENSION tablefunc;
For more information on the CREATE EXTENSION command, see:
http://www.postgresql.org/docs/9.2/static/sql-createextension.html For
more information on extensions, see:
http://www.postgresql.org/docs/9.2/static/contrib.html
Other
Some machines may require provisioning of shared memory:
http://www.postgresql.org/docs/9.2/static/kernel-resources.html#SYSVIPC
When installing the postgres gem, including ARCHFLAGS is recommended:
ARCHFLAGS="-arch x86_64" gem install pg
To install gems without sudo, see the Homebrew wiki.
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Or, if you don't
want/need launchctl, you can just run:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start Warning: Could not link
postgresql. Unlinking... Error: The brew link step did not complete
successfully The formula built, but is not symlinked into /usr/local
You can try again using `brew link postgresql'
==> Summary 🍺 /usr/local/Cellar/postgresql/9.2.4: 2831 files, 38M, built in 4.9 minutes
SOLUTION:
I execute this command in order to change the permission of the folder:
sudo chown jeanosorio /usr/local/share/man/man7
Then
brew link postgresql Linking /usr/local/Cellar/postgresql/9.3.1... 421
symlinks created
And finally:
sudo ARCHFLAGS="-arch x86_64" gem install pg
Fetching: pg-0.17.0.gem (100%) Building native extensions. This could
take a while... Successfully installed pg-0.17.0
If you want to avoid using MacPorts, you can download the Postgres App and place it into the Application directory.
Then, specify the location of newly downloaded pg_config:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config
If you run in to missing headers problem, try specifying the include directory of the app:
gem install pg -- --with-pg-include='/Applications/Postgres.app/Contents/Versions/latest/include/'
Use brew to get postgresql
brew install postgresql
Check you have pg_config in the installed brew. I found mine in
/usr/local/Cellar/postgresql/9.3.3/bin/pg_config
Check via :
$ ls /usr/local/Cellar/postgresql/9.3.3/bin/pg_config
> /usr/local/Cellar/postgresql/9.3.3/bin/pg_config
Once done, install the pg gem with
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.3/bin/pg_config
I wasn't able to install postgres via MacPorts. Instead, I installed Postgress.app. and called
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Note: in newer versions (in 9.3 at least) the path is actually: /Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
If you have homebrew, just type:
$ brew install postgresql
For me, on Yosemite, I didn't need to specify pg_config path. The following got it done.
brew install postgresql
sudo env ARCHFLAGS="-arch x86_64" gem install pg
Seems like architecture flag is the key.
Here's another alternative in case you want to use the postgresql installer directly. There's a couple of hoops you gotta jump through to begin with after updating to mavericks. Here's what I did:
Install the xcode command line tools first:
xcode-select --install
Download and install the latest version of PostgreSQL (9.3.1), in my case I just used the graphical installer. Here's the link to the downloads page:
http://www.enterprisedb.com/products-services-training/pgdownload#osx
Just choose all of the defaults it gives you. It my case it installed postgres to the following directory, if you installed it to a different directory, just remember the path you chose, because you'll need it shortly.
/Library/PostgreSQL/9.3
If you now try and install the latest pg gem (0.17.0) you'll need to pass a couple of options on the command line. This is what I used:
ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
If you installed postgres to a different location then you'll need to fix up the path to the pg_config file.
If it complains of a missing 'libpq.5.dylib' file while trying install the pg gem you'll need to create a symlink pointing to the actual location of this file. Postgres is looking for it in your /usr/local/lib/ directory so create a symlink in there and redirect it to its actual location. In my case I had to run:
sudo ln -s /Library/PostgreSQL/9.3/lib/libpq.5.dylib /usr/local/lib/libpq.5.dylib
Once you've done that, retry installing the gem again and hopefully it will work for you the same way it did for me.
In my case (i needed PG 0.16.0 on Mavericks), i installed postgresql via MacPorts
sudo port install postgresql90
and then
gem install pg -v '0.16.0' -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config
For the latest version you need to deduct -v '0.16.0'
gem install pg -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config
If you have Homebrew installed do not install MacPorts before you read about their coexistense
Place the missing --with-pg-config in Bundler's config file: /Users/<your_user>/.bundle/config to make your life easier.
---
BUNDLE_BUILD__PG: --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
BUNDLE_BUILD__EXAMPLE_GEM: --other-configs
BUNDLE_BUILD__OTHER_EXAMPLE_GEM: --other-configs
Then run bundle install again.
That works for me! (Postgres 93, mac ox 10.9.1)1. download # http://postgresapp.com/ and than
gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
I was stuck on my bundle install for 3 days. Tried Everything like adding env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.5_1/bin/pg_config
I was able to see pg gem getting installed after this command but still it was not installing from bundle install, which was a pain because I dint know what to write in Gemfile except gem 'pg'
The thing which finally worked for me was to find that my pg_config was in /Library/PostgreSQL/9.3/bin/pg_config and by default the Gemfile bundle install looks in /usr/local/bin/pg_config
I just ran the following command and magic happened.
bundle config build.pg --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
In my case it didn't work if you don't specify the arch flags, OS X 10.10.3 and postersApp 9.4
sudo env ARCHFLAGS="-arch x86_64" gem install pg -- \--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
The easiest way for me to resolve this was to use Postgres.app and set up my env vars so gem install pg just works (instead of requiring the --with-pg-config flag).
Using Homebrew Cask,
brew cask install postgres
echo 'export PG_HOME=/Applications/Postgres.app/Contents/Versions/latest' >> ~/.bash_profile
echo 'export PATH="$PATH:$PG_HOME/bin"' >> ~/.bash_profile
source ~/.bash_profile
# now it just works
gem install pg
This is what it worked for me:
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
In my case, I uninstalled the version installed via homebrew and switched to Postgres.app (v9.3.4.2 at the time of writing).
It only seemed to work when prepending the environment architecture flags and specifying the path to pg_config. Your milage might vary, so this answer could help with variations on pg_config's location.
Here is the final, complete command that worked for me:
env ARCHFLAGS="-arch x86_64" gem install pg -- \
--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
sudo ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config
I had installed postgres using homebrew on mac. Used below command to find the location
which postgres
/usr/local/opt/postgresql#12/bin/postgres
Installed gem using below command. In the above path just replace the postgres with pg_config since command requires path for pg_config.
gem install pg -- --with-pg-config=/usr/local/opt/postgresql#12/bin/pg_config
I was getting the same error when running bundle install regarding the pg gem (using Mac OS X Mavericks). After hours of researching, I found the solution. I installed postgres using homebrew
brew install postgres
After installing that, I created a new rails app using postgres. I ran
bundle install
to no avail (got same error as the question). I used
which psql
to figure out where my postgres was installed, it returned
/usr/local/bin/psql
before running bundle install again, I had to change the global path to build.pg by running the following
bundle config build.pg --with-pg-config=/usr/local/bin/pg_config
then I ran bundle install again and voila! I used the postgres where brew installed it.
On OS X Mavericks with latest Postgres App do the following with sudo privileges
env ARCHFLAGS="-arch x86_64" gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
What worked for me on 10.9.3 with Xcode 5.1.1 was the following:
brew update
brew install postgresql
gem install pg -v '0.17.1'
I needed pg 0.17.1
For me, the error message looked like this:
Installing pg 0.18.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
As the error message shows, it's trying to execute the command/script "pg_config". So just add it to the path from wherever your Postgres installation is. In my case, I did:
export PATH=/Applications/Postgres.app/Contents/Versions/9.5/bin:$PATH
then
bundle
That's it.
For any lost soul outthere still having this issue with no resolution and have a brew installed ruby and postgres. Here's the following steps to make sure of:
Make sure your version of ruby wasn't installed with the --universal flag.
This was the hardest thing to figure out, I had --universal which was the final root of the problem.
Make sure you have initialized postgresql properly with initdb and createdb commands (look elsewhere for steps to do this).
Make sure Xcode (Optional) and command line tools is installed; on the terminal:
xcode-select --install
sudo xcodebuild -license (if Xcode installed) or open the application and accept the license agreement.
Reboot Computer
which ruby, which gem and which postgres should show /usr/local/bin/ruby, /usr/local/bin/gem and /usr/local/bin/postgres respectfully.
On the terminal run:
gem install pg -- --with-pg-config=/usr/local/bin/pg_config
or
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config
Making sure all these steps are satisfied I was able to solve the following errors:
ruby.h header error
developer tools installed first error
First of all find pg_config location
sudo find / -name "pg_config" -print
The answer is /Library/PostgreSQL/9.1/bin/pg_config in my configuration (MAC Maverick)
Then try installing something like below
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
if this does not work then try to check in what way you installed postgresql
brew/mac port/setup
then you have to try relevant option to the same.
Thank you.
Ram
I was with the same problem, this was the solution I've found.
If you run brew doctor, it will tell you that probably you have installed something without brew that have changed the permissions on some folders, and so, you need to change permissions back to you on that folder.
Inside /usr/local/share/man, what you can do is the following:
sudo chown [yourusername] man7
And then:
brew link postgres
Hope it helps!
Similarly, after installing Mavericks 'bundle update' was throwing an error on the pg gem, which is only used on production and not locally.
I use Brew to manage my packages and postgresql was already installed, but still I was getting the 'no pg_config' error.
The fix was to just 'brew uninstall postgresql', then 'brew install postgresql'. After which I was immediately able to successfully run 'bundle update'
Postgres 9.4:
gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/Versions/9.4/bin/pg_config
sudo su
then
ARCHFLAGS="-arch x86_64" gem install pg -v '0.18'
worked on 10.10.2
If after trying everything here it still won't install, try opening Xcode and accepting the license agreement.
I am using the Ruby on Rails 3.1 pre version. I like to use PostgreSQL, but the problem is installing the pg gem. It gives me the following error:
$ gem install pg
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/home/u/.rvm/rubies/ruby-1.9.2-p0/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=/home/u/.rvm/rubies/ruby-1.9.2-p0/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 /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
Results logged to /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out
How do I solve this problem?
It looks like in Ubuntu that header is part of the libpq-dev package (at least in the following Ubuntu versions:
11.04 (Natty Narwhal), 10.04 (Lucid Lynx), 11.10 (Oneiric Ocelot), 12.04 (Precise Pangolin), 14.04 (Trusty Tahr) and 18.04 (Bionic Beaver)):
...
/usr/include/postgresql/libpq-fe.h
...
So try installing libpq-dev or its equivalent for your OS:
For Ubuntu/Debian systems: sudo apt-get install libpq-dev
On Red Hat Linux (RHEL) systems: yum install postgresql-devel
For Mac Homebrew: brew install postgresql
For Mac MacPorts PostgreSQL: gem install pg -- --with-pg-config=/opt/local/lib/postgresql[version number]/bin/pg_config
For OpenSuse: zypper in postgresql-devel
For ArchLinux: pacman -S postgresql-libs
On macOS (previously Mac OS X and OS X), use Homebrew to install the proper headers:
brew install postgresql
and then running
gem install pg
should work.
Alternatively, instead of installing the whole postgresql, you can brew install libpq and export the correct PATH and PKG_CONFIG_PATH as explained in the 'Caveats' section
I had also tried doing gem install libpq-dev, but I received this error:
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
However I found that installing with sudo apt-get (which I try to avoid using with Ruby on Rails) worked, i.e.
sudo apt-get install libpq-dev
# or
apt-get install postgres-server-dev-{pg.version}
# for postgresql 9.4 on Ubuntu 14.04
then I was able to do
gem install pg
without issues.
I could solve this in another way. I didn't find the library on my system. Thus I installed it using an app from PostgreSQL main website. In my case (OS X) I found the file under /Library/PostgreSQL/9.1/include/ once the installation was over. You may also have the file somewhere else depending on your system if you already have PostgreSQL installed.
Thanks to this link on how to add an additional path for gem installation, I could point the gem to the lib with this command:
export CONFIGURE_ARGS="with-pg-include=/Library/PostgreSQL/9.1/include/"
gem install pg
After that, it works, because it now knows where to find the missing library. Just replace the path with the right location for your libpq-fe.h
Can't find the libpq-fe.h header
i had success on CentOS 7.0.1406 running the following commands:
~ % psql --version # => psql (PostgreSQL) 9.4.1
yum install libpqxx-devel
gem install pg -- --with-pg-config=/usr/pgsql-9.4/bin/pg_config
Alternatively, you can configure bundler to always install pg with these options (helpful for running bundler in deploy environments),
bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config
For CentOS 6.4,
yum install postgresql-devel
gem install pg
worked well!
Just for the record:
Ruby on Rails 4 application in OS X with PostgresApp (in this case 0.17.1 version needed - kind of an old project):
gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
It is only issue of missing libpq-fe.h
On ubuntu we just need to run:
sudo apt-get install libpq-dev
Above command will install a NEW package libpq-dev and after that you can again execute bundle install to resume your bundle installation.
On Mac OS X run like this:
gem install pg -- --with-pg-config=***/path/to/pg_config***
***/path/to/pg_config*** is path to pg_config
In my case it was package postgresql-server-dev-8.4 (I am on Ubuntu 11.04 (Natty Narwhal), 64 bits).
For MacOS without installing PostgreSQL server:
brew install libpq
gem install pg -- --with-pg-config="/usr/local/Cellar/libpq/9.6.6/bin/pg_config"
The right answer for Mac users with Postgres.app is to build against the libpq provided with that package. For example, with the 9.4 release (current as of this writing), all you need is:
export CONFIGURE_ARGS="with-pg-include=/Applications/Postgres.app/Contents/Versions/9.4/include"
gem install pg
This will keep your pg gem in sync with exactly the version of PostgreSQL you have installed. Installing something from Homebrew is a waste in this case.
I had the same issue on Amazon Linux. I could find the header libpq-fe.h, but somehow it didn't work.
It came from the different versions of the packages that were installed through the different users on the machine. PostgreSQL 9.2 and PostgreSQL 9.3 were installed. So, make sure of your PostgreSQL version before including the libraries.
For me, the magic command line was:
sudo yum install postgresql93 postgresql93-server postgresql93-libs postgresql93-contrib postgresql93-devel
Source: An almost idiot's guide to install PostgreSQL 9.3, PostGIS 2.1 and pgRouting with Yum
My solution for Fedora 30:
sudo dnf install /usr/include/libpq-fe.h
A more general answer for any Debian-based distribution (which includes Ubuntu) is the following. First, install the apt-file package running as root:
apt-get install apt-file
This allows you to search for packages containing a file. Then, update its database using
apt-file update
(this can be run as normal user). Then, look for the missing header using:
apt-file search libpq-fe.h
On my machine, this gives:
libpq-dev: /usr/include/postgresql/libpq-fe.h
postgres-xc-server-dev: /usr/include/postgres-xc/server/gtm/libpq-fe.h
There you go !
On Ubuntu, install the "libpq-dev" to get rid of this issue.
sudo apt-get install libpq-dev
On Fedora 32, I managed to solve this same issue by installing libpq5-devel specifically.
If you install postgresql from the official repository, you will have several versions that won't fix the problem, so it's really a matter of trying out which one it needs.
I had the same problem on Mac OS, but I installed the PostgreSQL gem easily by using the following in a terminal:
ARCHFLAGS="-arch x86_64" gem install pg
(I installed PostgreSQL first with brew install postgresql.)
I found this answer, and it was the only one that worked for me (Mac OS) - after researching for about two days:
$ sudo su
$ env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions. This could take a while...
Successfully installed pg-0.11.0
1 gem installed
Installing ri documentation for pg-0.11.0...
Installing RDoc documentation for pg-0.11.0...
See Stack Overflow question Can't find the PostgreSQL client library (libpq).
I recently upgraded to Mac OS X v10.10 (Yosemite) and was having difficulty building the pg gem.
The error reported was the typical:
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... *** extconf.rb failed ***
My solution was to gem uninstall pg and then bundle update pg to replace the gem with the latest. I did run brew update; brew upgrade after the Yosemite install to get the latest versions of packages I had installed previously.
On a Mac, I solved it using this code:
gem install pg -v '0.18.4' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
I had this issue with Postgresql 9.6. I was able to fix it by doing:
brew upgrade postgresql#9.6
brew link postgresql#9.6 --force
gem install pg
I am running Postgres.app on a Mac and I had to
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin
first. Then
bundle install
worked for me.
I encountered the same error with postgres installed through asdf. The pg-config solutions didn't work for me. Instead I had to locate the postgres include folder which contains the file and run the command with the --with-pg-include flag
gem install pg -- --with-pg-include=/<path>/.asdf/installs/postgres/<version>/include
On CentOS,I installed libpq-dev package using below command
yum install postgresql-devel
Executing gem install pg returned the same error as "No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config".
Installing the gem as below solved my problem
gem install pg -- --with-pg-config=/usr/pgsql-x.x/bin/pg_config
On Ubuntu 20.04, I already had libpq5:amd64 version 13.2-1.pgdg20.04+1. Now when I tried to install libpq-dev from apt repository, I was receiving
The following packages have unmet dependencies:
libpq-dev : Depends: libpq5 (= 12.6-0ubuntu0.20.04.1) but 13.2-1.pgdg20.04+1 is to be installed
I grabbed the deb from http://ftp.us.debian.org/debian/pool/main/p/postgresql-13/libpq-dev_13.2-1_amd64.deb and did the following:
mkdir -p /tmp/libpq-dev
dpkg-deb -R libpq-dev_13.2-1_amd64.deb /tmp/libpq-dev
sed -i 's|= 13.2-1|= 13.2-1.pgdg20.04+1|' /tmp/libpq-dev/DEBIAN/control
dpkg-deb -b /tmp/libpq-dev /tmp/libpq-dev-new.deb
sudo dpkg -i /tmp/libpq-dev-new.deb
I have been using the gem happily after that. No additional installs or anything.
For alpine, please use below to fix the error
apk add --no-cache postgresql-dev
On debian bullseye, none of the existing answers solved the issue for me.
In case it helps someone with the same configuration as mine:
Use PostgreSQL's apt repository: https://wiki.postgresql.org/wiki/Apt
sudo apt-get update && sudo apt-get dist-upgrade (upgrade PostgreSQL from v13 to v14)
sudo apt-get install libpq-dev
gem install pg or bundle install
On FreeBSD (9.1) the necessary package is /usr/ports/database/postgresql-server* which when installed will also install the required header file that makes the gem install of "pg" fail. This answer here helped me find the solution but the difference in package names required a bit of searching.
Hopefully this helps someone avoid a bit of head scratching when searching for the "-dev" package on a FreeBSD system!
On Debian 7.0, 64-bit (Wheezy), just run:
sudo apt-get install libpq-dev
After you successfully installed libpq-dev, run:
bundle install