用'gem pq'安装PostgreSQL gem失败,出现错误:无法构buildgem原生扩展

我正在学习Ruby on Rails,并试图开发一个应用程序。 在我的应用程序中,我试图在生产模式下使用开发模式PostgreSQL中的默认SQLite数据库 。 但是当我试图安装pg gem时,出现以下错误: gem install pg

 Building native extensions.  This could take a while... ERROR:  Error installing pg:        ERROR: Failed to build gem native extension.    /home/tusharkhatiwada/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb checking for pg_config... yes Using config values from /usr/bin/pg_config You need to install postgresql-server-dev-XY for building a server-side extension or libpq-dev for building a client-side application. You need to install postgresql-server-dev-XY for building a server-side extension or libpq-dev for building a client-side application. 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/tusharkhatiwada/.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 /home/tusharkhatiwada/.rvm/gems/ruby-2.0.0-p247@rails4.0.0/g ems/pg-0.17.0 for inspection. Results logged to /home/tusharkhatiwada/.rvm/gems/ruby-2.0.0-p247@rails4.0.0/gems/pg-0.17.0/ext /gem_make.out 

之后,我再次尝试使用gem install pg -- --with-pg-config= '/usr/bin/pg_config'并显示以下错误:

 Building native extensions with: '--with-pg-config= /usr/bin/pg_config' This could take a while... ERROR:  Error installing pg:        ERROR: Failed to build gem native extension.    /home/tusharkhatiwada/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb --with-pg-config= /us r/bin/pg_config Using config values from sh: 1: : Permission denied sh: 1: : Permission denied 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/tusharkhatiwada/.rvm/rubies/ruby-2.0.0-p247/bin/ruby        --with-pg        --without-pg        --with-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 /home/tusharkhatiwada/.rvm/gems/ruby-2.0.0-p247@rails4.0.0/g ems/pg-0.17.0 for inspection. Results logged to /home/tusharkhatiwada/.rvm/gems/ruby-2.0.0-p247@rails4.0.0/gems/pg-0.17.0/ext /gem_make.out 

如果您使用的是Ubuntu,请尝试安装以下lib文件

 sudo apt-get install libpq-dev 

然后gem install pg

为我工作。

对于那些谁通过谷歌search如果libpq-dev没有工作或抛出另一个错误我hade同样的问题尝试升级你的系统,并得到rubydiv套件

 sudo apt-get update sudo apt-get upgrade sudo apt-get install ruby1.9.1-dev 

然后重新启动后再试一次

你必须这样做

 sudo apt-get install libgmp-dev libpq-dev 

接着

 gem install pg