Ruby on Rails中bundle install执行失败问题求助
Hey there, let's work through this frustrating pg gem issue—this is one of the most common roadblocks when setting up Ruby/Rails apps that use PostgreSQL, so we'll get you past it quickly.
The root problem here is that the pg gem needs PostgreSQL's development libraries to compile successfully. When you run gem install pg -v '1.1.4' directly, it can't find those libraries, hence the error. Here's how to fix it based on your operating system:
For Ubuntu/Debian-based Linux Distros
- First, install the PostgreSQL development package (this gives the headers and libraries the gem needs):
sudo apt-get update && sudo apt-get install libpq-dev - Once that's done, run the gem install command again:
gem install pg -v '1.1.4' - If that succeeds, go ahead and run
bundle install—it should work now.
For macOS
If you use Homebrew (the most common package manager for macOS):
- Install PostgreSQL (this includes the necessary development files):
brew install postgresql - If you already have PostgreSQL installed but still get the error, you might need to explicitly point the gem to your
pg_configfile. For Intel Macs, this is usually:
For M1/M2 Macs (ARM architecture), the path is often:gem install pg -v '1.1.4' -- --with-pg-config=/usr/local/bin/pg_configgem install pg -v '1.1.4' -- --with-pg-config=/opt/homebrew/bin/pg_config
For Windows
Windows can be trickier, but this should work:
- First, install the official PostgreSQL client from the PostgreSQL website (make sure to check the option to add PostgreSQL to your system PATH during installation).
- Then run the gem install command, pointing it to your
pg_config.exefile (adjust the path to match your PostgreSQL version—this example uses version 13):gem install pg -v '1.1.4' -- --with-pg-config="C:\Program Files\PostgreSQL\13\bin\pg_config.exe"
Alternatively, you can try installing the pre-compiled Windows gem directly:
gem install pg -v '1.1.4' --platform=x86-mingw32
Quick Troubleshooting Tips
- If you're using a Ruby version manager like rbenv or rvm, make sure you're in the correct Ruby environment when running these commands.
- Double-check that the
pg_configpath you're using actually exists on your system—if not, adjust it to match where PostgreSQL is installed.
Once you get gem install pg -v '1.1.4' to run successfully, bundle install should complete without hitting that error again.
内容的提问来源于stack exchange,提问作者mph85

