You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Capistrano部署应用时pg(1.0.0) gem安装失败的解决咨询

Fixing pg (1.0.0) Installation Error During Capistrano Deployment

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.
  • Verify Capistrano is using the correct Rails environment
    If your Gemfile only includes pg in the production group, ensure Capistrano deploys with the production environment. Add or confirm these lines in config/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 install
    
  • Ensure Ruby versions match between local and server
    Mismatched Ruby versions can break gem compilation. Run ruby -v locally and on your server to confirm they're identical. If you use rbenv or rvm, add the correct version to your config/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 running bundle install with sudo unless absolutely necessary—instead, make sure the deployment user owns the app directory and has write access to vendor/bundle and tmp folders.

  • 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. Run gem which pg on the server to get the path, then add this to config/deploy.rb:

    set :bundle_path, '/path/to/your/gem/bundle' # Replace with the actual path from `gem which pg`
    

内容的提问来源于stack exchange,提问作者LazyGeek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:32:11