简体   繁体   中英

Can't install pg gem on Windows

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.

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

PsAdding for linux users.

I solved this error installing libpq-dev.

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.

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}\\

eg 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.

First install ruby DevKit

On Windows XP and from MSysGIT bash terminal just do the following based on my installation path

subst X: "D:/Program Files/PostgreSQL/9.0"

gem install pg -- --with-pg=X:

My gem pg-0.10.1 got installed successfully.

This may seem like a no brainer, but I had this same error with a slight twist. After getting the build to find the headers, it immediately failed and claimed to be unable to find libpq. I finally deduced that I was trying to build against a 64-bit postgres install with a 32-bit ruby. Installing the 32-bit postgres resulted in an instant-fix. Just thought I'd add this in case anyone else with the same error stumbles across this thread.

The message "Can't find the 'libpq-fe.h header" means that installer can't find the header

  • Check that your Postgre installation contains development tools ("include" folder). Install postgre development tools in case of absence.
  • Check location of pg_config.exe. You need run installation like
    gem install pg -- --with-pg-config="C:/PROGRA~2/POSTGR~1/8.3/bin/pg_config.exe" .
    Note that you need to use short folder names.

To build a native extension for pg gem you need a ruby DevKit ( http://rubyinstaller.org/downloads/ )

I meet another problem:

D:\\app1>rake test
(in D:/app1)
rake aborted!
no such file to load -- 1.9/pg_ext

In this case you need manually copy copy content of
C:\\Ruby\\192\\lib\\ruby\\gems\\1.9.1\\gems\\pg-0.10.0\\lib\\
to
C:\\Ruby\\192\\lib\\ruby\\gems\\1.9.1\\gems\\pg-0.10.0\\lib\\1.9

"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

Thanks Luis! pg 0.9.0 installed just fine.

For the latest version (0.10.0) you need the devkit and this command "gem install pg -- --with pg-dir=path/to/your/postgresql/dir"

When I leave the additional "--" away, it gets me an error: "gem install pg --with-pg-dir=C:/postgresql/9.0"

I had the same problem. You most likely have a perl installation in your path before PostgreSQL. The most common perl installation for Windows has an executable name pg_config in its bin which causes conflicts.

pg v0.10.0 installs just fine with rubyinstaller + devkit as long as you specify paths without spaces (eg C:/PROGRA~) -- otherwise nmake chokes.

Windows 7, PostgreSQL 8.4, Ruby 1.9.2:

subst X:\ "C:\Program Files\PostgreSQL\8.4"

gem install pg -- --with-pg=X:/

This installed it successfully.

Other easy way, on windows

  1. Install postgresql ODBC.

  2. Copy libpq.dll from the pgqlODBC lib directory

  3. Paste it into PostGreSQL lib directory

  4. Run this command (adapt the PostGreSQL path in this command, according to yours) :

     gem install pg -- --with-pg-config="C:\\Dev\\Databases\\PostgreSQL\\9.0\\bin\\pg_config.exe" 

I got it:

AND THE MAGIC TRICK :

is that you do not have this installed -- > postgres-devel

so go get it :

install postgresql-devel

Thats all... than just gem install pg and there it goes.

I ran into the same problem. I wanted to access a postgres database using dbi and had to install Pg. It took me a day to successfully compile the module with instructions similar to the other posts of this thread. But the second time I had to do it, I found another solution: using ODBC.

  1. Install the ODBC driver for postgresql ( http://www.postgresql.org/ftp/odbc/versions/ )
  2. Configure the data source ( http://www.xtuple.org/SettingUpPostgresAndOdbc )
  3. Install the ruby dbi and odbc gems

     gem install dbi gem install ruby-odbc gem install dbd-odbc 
  4. Connect to postgres

     require 'dbi' dbh = DBI.connect('dbi:ODBC:DSN_NAME', 'USER', 'PASS') 

This worked for me using ruby 1.9.1 on Windows XP 32 bit. Also, this doesn't change anything in your code except the DBI.connect line, if you were already using DBI.

PostgreSQL 9.0:

gem install pg --platform=mswin32 --version=0.9.0 -- '--with-pg="C:\Progra~1\PostgreSQL\"'

note the single-tick encapsulation. (~1 signifies "Program Files", change to ~2 if PostgreSQL is in Program Files (x86) on Win64)

As of version 0.13.0 of the pg gem, you should be able to install it using something like the following:

subst X: "C:\Program Files\PostgreSQL\9.0"
gem install pg -- -- with-pg=X:
subst X: /D

Then you can successfully run bundle install and see the following line in the output:

Using pg (0.13.0)

instead of the errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM