Can't install pg gem on Windows

34,122

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

Share:
34,122
sNiCKY
Author by

sNiCKY

Updated on September 03, 2021

Comments

  • sNiCKY
    sNiCKY over 2 years

    I've got 2 Ruby versions: 1.8.7 and 1.9.2 and PostgreSQL 8.3. I cant install pg gem on any of them. Getting this error:

    C:/Development/Ruby187/bin/ruby.exe extconf.rb
    checking for pg_config... yes
    not recorded
    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=C:/Development/Ruby187/bin/ruby
     --with-pg
     --without-pg
     --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}/lib
    

    I know it's a common problem, but I haven't found any working solution yet... Oh, I have added C:\Program Files (x86)\PostgreSQL\8.3\bin to my PATH.

  • sNiCKY
    sNiCKY over 13 years
    I have just installed 0.9.0 without the DevKit and it's working correctly. Thanks a lot Luis!
  • Luis Lavena
    Luis Lavena over 13 years
    yeah, you need "--" to tell rubygems these options are for the extension compilation process
  • Phrogz
    Phrogz over 13 years
    Even with DevKit and the above command (substituting my own pgsql dir) I get the same error as the OP: Can't find the 'libpq-fe.h header.
  • Phrogz
    Phrogz over 13 years
    I was able to accomplish this by editing the Path environment variable to add C:\Progra~2\PostgreSQL\8.4 to the end. (~2 corresponding in my case to "Program Files (x86)"). After that, gem install pg was able to find pg_config.exe without issue and install 0.10.0 correctly.
  • jwfearn
    jwfearn over 13 years
    Does anyone know if a Windows native version of 0.10.0 will ever become available? I've tried all these tricks with no luck. BTW: I like the SUBST trick for working around spaces-in-paths problems.
  • Luis Lavena
    Luis Lavena over 13 years
    @jwfearn, I would say you need to ask the gem author about it, can't help you on that :P
  • Denis Gorbunov
    Denis Gorbunov over 13 years
    But there is problem number two: "in `require': 126: �� ������ ��������� ������. - C:/Ruby192/lib/ruby/gems/1.9.1/gems/pg-0.10.1/lib/1.9/pg_ext‌​.so (LoadError)". I test the loading DLL "pg_ext.so". Other application (Visual Studio C++ writed) loads successfull "pg_ext.so", but Ruby cannot load "pg_ext.so"....
  • David
    David about 13 years
    just wanted to add here that this may not work on pg > 0.10.0. I got mine working with devkit and 0.9.0
  • Luis Lavena
    Luis Lavena about 13 years
    It should work now with version 0.10.1, which solves the incorrect locating mechanism of 0.10.0
  • Daniel Rikowski
    Daniel Rikowski about 13 years
    For me this only worked for 0.10.0. For 0.10.1 I had to add the bin and the lib directories to the search path in their 8.3 form. Then it worked without adding the --with-pg-dir option.
  • SDReyes
    SDReyes about 13 years
    I installed the DevKit, updated my path with the routes that Sehad mentioned. and installed successfully the pg 0.9.0
  • jwfearn
    jwfearn about 13 years
    NOTE: 0.10.x never worked for me but 0.11.x (currently in prerelease) installs OK. Use gem install pg --prerelease. Or stick with 0.9.x. As far as I can tell 0.10.x just never worked on Windows.
  • Phantomwhale
    Phantomwhale over 12 years
    This solved my Windows installation issues (of pg v0.12.2) - thanks !
  • Kenny Evitt
    Kenny Evitt about 12 years
    Just the "final installation" command gem install pg --platform=mswin32 --version=0.9.0 -- -- with-pg={Postgres directory short name}`, combined with the substr` trick in Luis's answer, was sufficient to get this installed for myself.
  • Kenny Evitt
    Kenny Evitt about 12 years
    If you use a specific platform and version you'll need to update Gemfile to match for Bundler (bundle install) to work.
  • Jakub Strebeyko
    Jakub Strebeyko over 5 years
    sudo apt-get install libpq-dev
  • Sprachprofi
    Sprachprofi over 4 years
    This helped me! I didn't get any error during the installation of pg, but after an upgrade to ruby 2.5.7 I kept getting "cannot load such file -- 2.5/pg_ext" when trying to run the server. Reinstalling pg alone didn't fix it, but telling it --with-pg-dir fixed it. THANK YOU!!!
  • CajunAsian
    CajunAsian over 3 years
    @JakubStrebeyko this worked for me using WSL 20.04LTS and rvm/ruby 2.7.0. Thank you!