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

Apple M1 Pro环境下pg gem安装失败求助(Ruby 2.6.5)

解决M1 Pro macOS 13.4.1上pg 1.3.5 gem安装失败的问题

1. 彻底清理混合架构的PostgreSQL依赖

先卸载可能存在的多架构PostgreSQL和libpq,避免冲突:

brew uninstall --force postgresql libpq

清理残留的配置文件与数据目录:

rm -rf /usr/local/var/postgres ~/.pgpass ~/.psqlrc ~/.psql_history

2. 确认Brew运行在arm64架构下

M1 Pro的原生Brew路径为/opt/homebrew,若之前使用过Rosetta兼容的x86版本Brew(路径/usr/local/homebrew),需彻底切换:

# 检查当前Brew架构
brew --prefix
# 若输出为/usr/local/homebrew,先通过Rosetta终端卸载x86版Brew
/usr/local/bin/brew uninstall --force brew
# 关闭终端后正常打开(不使用Rosetta),重新安装原生arm64版Brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 将原生Brew路径加入环境变量
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

3. 重新安装原生arm64版PostgreSQL

使用原生Brew安装适配arm64的PostgreSQL:

brew install postgresql
# 可选:启动PostgreSQL服务(后续项目运行需要)
brew services start postgresql

4. 指定架构与路径安装pg gem

直接通过gem命令指定arm64架构和原生pg_config路径安装:

env ARCHFLAGS="-arch arm64" gem install pg -v '1.3.5' -- --with-pg-config=/opt/homebrew/bin/pg_config

若使用bundle管理依赖,先配置bundle再执行安装:

bundle config build.pg --with-pg-config=/opt/homebrew/bin/pg_config
bundle install

5. 验证安装有效性

检查pg gem是否安装成功:

gem list pg
# 应输出 pg (1.3.5)

通过irb测试gem可用性:

irb
require 'pg'
# 无报错则说明安装正常

额外排查点

  • 确认libpq为纯arm64架构:
    lipo -info /opt/homebrew/lib/libpq.dylib
    # 正确输出:Non-fat file: /opt/homebrew/lib/libpq.dylib is architecture: arm64
    
  • 检查环境变量,确保没有其他x86版PostgreSQL/libpq的路径混入PATH或LDFLAGS/CPPFLAGS中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 13:10:07