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

Ruby环境安装Bundler/Rails报错求助:扩展未构建+架构不兼容

解决Ruby/Rails架构不兼容及依赖问题

核心问题

你的错误根源是Ruby和已安装的gem是x86_64架构,但你的Mac是arm64e架构(M1/M2系列芯片),导致二进制扩展不兼容;同时Ruby编译时没正确链接libyaml,缺失psych模块。

分步解决方案

  1. 彻底清理旧的RVM和Ruby环境
    先卸载现有RVM,避免残留干扰:

    rvm implode
    sudo rm -rf ~/.rvm ~/.rvmrc /etc/rvmrc /etc/profile.d/rvm.sh
    

    重启终端,输入rvm -v确认显示"command not found",说明清理干净了。

  2. 确保Homebrew是arm64版本
    检查brew路径:

    which brew
    

    如果输出是/usr/local/bin/brew(这是x86版本),先卸载:

    /usr/local/bin/brew uninstall --force --ignore-dependencies $(/usr/local/bin/brew list)
    sudo rm -rf /usr/local/Homebrew /usr/local/Caskroom /usr/local/bin/brew
    

    重新安装arm版brew:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    

    安装完成后,按照终端提示把brew路径加入你的shell配置文件(比如/.zshrc或/.bashrc),重启终端后再输入which brew,确认输出是/opt/homebrew/bin/brew。

  3. 安装编译Ruby所需的依赖

    brew install libyaml openssl readline
    
  4. 重新安装RVM(arm64兼容版)

    \curl -sSL https://get.rvm.io | bash -s stable
    

    重启终端,执行rvm requirements确认所有依赖都已安装。

  5. 编译安装arm64架构的Ruby 2.7.4
    安装时指定依赖路径,确保Ruby正确链接libyaml和openssl,避免后续psych缺失的问题:

    rvm install 2.7.4 --with-openssl-dir=$(brew --prefix openssl) --with-libyaml-dir=$(brew --prefix libyaml)
    
  6. 设置默认Ruby并安装Bundler

    rvm use 2.7.4 --default
    gem install bundler
    
  7. 重新安装项目依赖
    进入你的Rails项目目录,清理旧的gem缓存和锁文件,然后重新安装:

    rm -rf vendor/bundle Gemfile.lock
    bundle install
    

为什么之前的操作没用?

你之前重装Ruby时,大概率是通过Rosetta运行的终端,导致编译出的Ruby和gem都是x86_64版本,和你的arm64e芯片不兼容。gem pristine all只能修复已安装gem的扩展,但架构不兼容的情况下根本无法解决问题,必须彻底重建arm架构的Ruby环境才行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 09:41:01