Cannot Install ‘pg’ gem in Mac OSX – Error installing pg: ERROR: Failed to build gem native extension — Ruby on Rails

If you are planning on using PostgreSQL with Ruby on Rails, you will need to install pg, which is the Ruby interface to the PostgreSQL RDBMS. The command to install it is as follows:

$ gem install pg






However, before you run that command you need to install PostgreSQL. After installing PostgreSQL and run the above command to install pg, if you get the below error, keep reading 🙂

esilva@Esau-MBP:Website$ sudo gem install pg
Password:
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/2.0/usr/bin/ruby -r ./siteconf20141101-13676-mwatig.rb 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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/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}/

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/pg-0.17.1 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/extensions/universal-darwin-13/2.0.0/pg-0.17.1/gem_make.out

Well, this is because pg needs pg_config to install (you get pg_config when you install PostgreSQL). To find pg_config run the below command, then copy the path

$ sudo find / -name "pg_config"

After you find pg_config location, run the below command to finally install pg gem. Remember to add — –with-pg-config=/path/to/pg_config gem option

esilva@Esau-MBP:~/AAA_Website$ sudo gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
Building native extensions with: '--with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config'
This could take a while...
Successfully installed pg-0.17.1
invalid options: -f fivefish
(invalid options are ignored)
Parsing documentation for pg-0.17.1
Installing ri documentation for pg-0.17.1
Done installing documentation for pg after 1 seconds
1 gem installed

You have successfully installed pg gem





Esau Silva
Software Engineer at Region One ESC
Microsoft Full Stack Application Developer
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed.
Share