Solution 1:

The message you're getting is a clear indication that you lack something for the correct installation of that gem:

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.

There is no Windows native version of latest release of pg (0.10.0) released yesterday, but if you install 0.9.0 it should install binaries without issues.

Anyhow, if you want to install the gem, you need a build environment installed. If you're using RubyInstaller, then you need the DevKit

Installation of the gem will only require you provide additional options to gem installation (like --with-pg-dir)

subst X: "C:\Program Files (x86)\PostgreSQL\8.3"
gem install pg -- --with-pg-dir=X:
subst X: /D

Solution 2:

PsAdding for linux users.

I solved this error installing libpq-dev.

Solution 3:

I fought this for two days. I do my rails command line stuff from the excellent unixy like command window provided by msysgit. I created a postgresql-path.bat file that contained

@set PATH=%PATH%;C:\Progra~1\PostgreSQL\8.3\bin
@set PATH=%PATH%;C:\Progra~1\PostgreSQL\8.3\include
@set PATH=%PATH%;C:\Progra~1\PostgreSQL\8.3

At the top of the msys.bat file I added

CALL postgresql-path.bat

As soon as I got the space out of my PATH variable for the postgreSQL directories my problems installing 'pg' went away. Hope this saves somebody the problems I had.

Solution 4:

After nearly two days of trial and error (and thanks in a large part to the advice of the earlier post in this thread and elsewhere, I was able to successfully install pg 0.10.0 in Ruby 1.8.7 (for both my RubyInstaller and my InstantRails versions). In case anyone encounters this problem again, I'll summarize my final successful installation process.

First, I had to install DevKit, following their installation instructions. I had to copy (for safety) and then delete the operating_system.rb file in \Ruby187\lib\ruby\site_ruby\1.8\rubygems\defaults folder in order for the installation to run successfully.

Secondly, I installed the rake-compiler, and rdoc gems. gem install rake-compiler gem install rdoc (If you have problems with either of them, I'd recommend a liberal use of the --platform=win32 configuration option)

Once those were installed successfully, the final installation of pg itself was:

gem install pg --platform=mswin32 --version=0.9.0 -- -- with-pg={Postgres directory short name}\

e.g. gem install pg --platform=mswin32 --version=0.9.0 -- -- with-pg=C:\PostgreSQL\

The final '\' seems to be necessary for the way the installer parses directory strings. I realize my use of configuration options may be a little more than necessary, but an apparently successful install of version 0.10.0 (when I excluded the --version option) did not actually work.

Thanks again to those in this thread who helped me to this sequence.

Solution 5:

"gem install pg" not working

Fetching: pg-0.17.0.gem (100%) Building native extensions. This could take a while... ERROR: Error installing pg: ERROR: Failed to build gem native extension.

Try this it worked for me and I am pretty sure it will work for you as well::

#On Ubuntu
    $ sudo apt-get install postgresql-client libpq5 libpq-dev
    $ sudo gem install pg

This should work with "Bash on Ubuntu on Windows" as well