现有Rails项目PG gem架构不兼容报错的解决求助
解决Rails项目pg gem架构不兼容问题
执行bundle exec rails db:setup时出现以下错误:
rails aborted! LoadError: dlopen(/Users/name/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/pg-1.4.4/lib/pg_ext.bundle, 0x0009): tried: '/Users/name/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/pg-1.4.4/lib/pg_ext.bundle' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))...
这是因为pg gem编译时采用了x86_64架构,而你的M系列Mac(arm64)需要对应架构的版本,解决步骤如下:
1. 卸载现有不兼容的pg gem
- 执行命令卸载pg 1.4.4版本:
gem uninstall pg -v 1.4.4 - 如果是通过bundle安装的,也可以用:
bundle exec gem uninstall pg -v 1.4.4
2. 确保本地PostgreSQL是arm64架构
- 先检查当前PostgreSQL的架构:
输出如果包含file $(which psql)arm64则正常,若显示x86_64,需要重新安装arm版本:
注意:安装时不要用Rosetta模式的终端,确保用原生arm终端操作。brew uninstall postgresql brew install postgresql
3. 重新编译安装arm64版本的pg gem
- 配置bundle指定pg的编译参数,然后重新安装:
bundle config build.pg --with-pg-config=$(which pg_config) bundle install - 或者直接单独安装pg gem:
gem install pg -v 1.4.4 -- --with-pg-config=$(which pg_config)
4. 验证Ruby版本架构(可选)
如果上述步骤无效,检查你的Ruby版本是否是arm64架构:
ruby -e "puts RUBY_PLATFORM"
若输出是x86_64-darwin开头,说明是Rosetta下编译的Ruby,需要重新编译arm版本:
rbenv uninstall 3.1.0 RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl)" rbenv install 3.1.0
完成后重新执行bundle exec rails db:setup即可正常初始化数据库。
内容的提问来源于stack exchange,提问作者agehlot
相关产品推荐
相关产品推荐

