使用Capistrano部署应用时pg(1.0.0) gem安装失败的解决咨询
Hey there! I know it's frustrating when you've already installed the pg gem and its dependencies locally but still hit this error during deployment. Let's walk through some targeted fixes for your deployment server and Capistrano setup:
Double-check PostgreSQL development libraries on your deployment server
The pg gem needs these libraries to compile successfully, and local installs don’t transfer to your server. Run the appropriate command for your server's OS:- Ubuntu/Debian:
sudo apt-get update && sudo apt-get install libpq-dev - CentOS/RHEL:
sudo yum install postgresql-devel
Make sure you run this as a user with sudo privileges on the server.
- Ubuntu/Debian:
Verify Capistrano is using the correct Rails environment
If yourGemfileonly includes pg in the production group, ensure Capistrano deploys with the production environment. Add or confirm these lines inconfig/deploy.rb:set :rails_env, 'production' set :bundle_without, %w{development test}.join(' ')This tells Bundler to skip non-production gems during deployment.
Force a fresh pg gem install on the server
Sometimes cached bundle data on the server causes conflicts. SSH into your deployment server, navigate to your app's current directory, and run:bundle install --force pg -v '1.0.0'If that doesn't work, clear the bundle cache first:
rm -rf vendor/bundle && bundle installEnsure Ruby versions match between local and server
Mismatched Ruby versions can break gem compilation. Runruby -vlocally and on your server to confirm they're identical. If you use rbenv or rvm, add the correct version to yourconfig/deploy.rb:# For rbenv set :rbenv_ruby, '2.7.6' # Replace with your actual Ruby version # For rvm set :rvm_ruby_string, 'ruby-2.7.6'Check deployment user permissions
Your deployment user might lack the rights to compile gems or write to the app directory. Avoid runningbundle installwith sudo unless absolutely necessary—instead, make sure the deployment user owns the app directory and has write access tovendor/bundleandtmpfolders.Confirm gem path configuration in Capistrano
If the pg gem is installed in a non-standard path on the server, tell Capistrano where to find it. Rungem which pgon the server to get the path, then add this toconfig/deploy.rb:set :bundle_path, '/path/to/your/gem/bundle' # Replace with the actual path from `gem which pg`
内容的提问来源于stack exchange,提问作者LazyGeek

