An error occurred while installing pg (0.17.1), an

2019-01-08 06:32发布

I just installed Rails 4.0.2 and when creating a new app, in the bundle stage I get:

Installing pg (0.17.1) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/Dee/.rvm/rubies/ruby-2.0.0-p247/bin/ruby 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.

How do I fix this?

13条回答
男人必须洒脱
2楼-- · 2019-01-08 06:34

app root:

  1. brew update
  2. brew install postgres
  3. gem install pg -- --with-pg-config=/usr/local/Cellar/postgresql/9.3.4/bin/pg_config
  4. bundle install
  5. ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
  6. launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  7. createuser -s -r postgres
  8. rake db:create:all
  9. rake db:migrate
  10. rails s

NOTE: replace the version number in step 3 if needed.

查看更多
仙女界的扛把子
3楼-- · 2019-01-08 06:35

Looks like you do not have PostgreSQL installed. The pg gem requires some headers from PostgreSQL to compile native extension.

查看更多
SAY GOODBYE
4楼-- · 2019-01-08 06:39

I'm on a Mac running Mavericks. My solution was to install Postgres.

And then in terminal install using homebrew with the configuration:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config

Note: This answer has been edited to use the latest symlink that is currently included in shipping versions of the Postgres app.

Previous versions suggested:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
查看更多
SAY GOODBYE
5楼-- · 2019-01-08 06:41

It works for me

rvm list gemsets
rvm use ruby-2.4.1
bundle

error[![resolved with rvm use ruby]2

查看更多
聊天终结者
6楼-- · 2019-01-08 06:42

The way I managed to get past that error was:

  • cd to app folder and then set the ruby version locally. I'm using ruby 2.1.2.

rbenv local 2.1.2

  • instead of just running bundle install, install the gems in vendor/bundle

bundle install --path vendor/bundle

This did it for me.

查看更多
Deceive 欺骗
7楼-- · 2019-01-08 06:47

I needed to use sudo

sudo gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config
查看更多
登录 后发表回答