很多使用 Apple Silicon(M1/M2/M3 等)芯片的 Mac 用户,在执行 Homebrew 命令时,会遇到经典 CPU 架构不匹配报错,本文将深度解析问题原因,并提供临时应急和永久根治两套方案,彻底解决该问题。
一、完整报错信息
/usr/local/Homebrew/Library/Homebrew/brew.sh: line 1188: /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/current/bin/ruby: Bad CPU type in executable
/usr/local/Homebrew/Library/Homebrew/brew.sh: line 1188: /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/current/bin/ruby: Undefined error: 0
二、问题核心根源
该报错的本质是 CPU 架构不匹配,也是 M 系列 Mac 最常见的 Brew 环境问题:
- 你的设备:Apple Silicon M 系列芯片,原生架构为 arm64
- 当前 Brew 版本:安装在
/usr/local/Homebrew的 Intel x86_64 旧版本 - 冲突点:旧版 Intel 架构的 Brew 自带的 Ruby 二进制程序,无法在 ARM 原生环境运行,直接触发 CPU 类型错误
关键路径区分(必记)
✅ M芯片ARM原生版Brew(正确):/opt/homebrew
❌ Intel旧版Brew(报错源头):/usr/local/Homebrew
绝大多数出现该问题的用户,都是通过 Mac 迁移助手同步了旧 Intel 电脑的环境,导致系统残留了旧架构的 Homebrew。
1. 安装 Rosetta2 兼容层
softwareupdate --install-rosetta --agree-to-license
2. 以 Intel 架构执行 Brew 命令
arch -x86_64 brew --version
方案优缺点
✅ 优点:无需重装,即时生效
❌ 缺点:性能损耗大,后续更新、安装软件极易出现兼容报错,环境不稳定,仅适合临时救急。
四、解决方案二:彻底重装原生ARM Brew(推荐,永久根治)
彻底清除旧的 Intel 版 Homebrew,安装 M 芯片原生 arm64 版本,从根源解决所有架构冲突问题,也是官方推荐的标准方案。
步骤1:卸载旧版 Intel Homebrew
执行官方卸载脚本,彻底删除 /usr/local 下的所有旧 Brew 文件
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
执行过程中按提示确认卸载,等待脚本自动清理 Homebrew、Cellar 等核心目录。
步骤2:清理环境变量配置
打开终端配置文件(M芯片默认zsh)
open ~/.zshrc
删除文件中所有包含 /usr/local/bin/brew 的旧配置,典型无效配置如下:
eval "$(/usr/local/bin/brew shellenv)"
保存文件后,刷新终端环境:
source ~/.zshrc
步骤3:安装 M芯片原生 ARM 版 Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
步骤4:配置原生环境变量
安装完成后,终端会提示环境变量配置命令,复制执行(通用命令如下):
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc
五、环境验证(确认修复成功)
执行以下命令,验证当前 Brew 环境为原生 ARM 版本:
1. 查看 Brew 安装路径
brew --prefix
✅ 正常输出:/opt/homebrew
2. 查看终端架构
arch
✅ 正常输出:arm64
3. 检测环境完整性
brew doctor
无报错、提示系统正常即代表修复完成。
六、进阶:双版本Brew共存(极少场景使用)
部分开发场景需要同时兼容新旧软件,可保留 Intel + ARM 双版本 Brew,普通用户不建议使用,极易出现 PATH 冲突。
- ARM 原生版(默认):直接执行
brew - Intel 兼容版:必须加前缀执行
arch -x86_64 brew
七、问题总结与避坑指南
- 报错核心:M芯片Mac使用了Intel x86_64架构的旧版Homebrew,架构不兼容
- 最优方案:卸载旧版,安装
/opt/homebrew原生ARM版本 - 禁忌操作:不要强行保留双环境、不要忽略环境变量残留,会持续引发各类brew报错
- 迁移避坑:新机迁移建议不要同步旧电脑的开发环境配置,避免残留兼容问题
/bin/bash brew-install/uninstall.sh
Warning: This script will remove:
/usr/local/Cellar/
/usr/local/Homebrew/
/usr/local/Homebrew/.claude/
/usr/local/Homebrew/.cursor/
/usr/local/Homebrew/.devcontainer/
/usr/local/Homebrew/.dockerignore
/usr/local/Homebrew/.editorconfig
/usr/local/Homebrew/.github/
/usr/local/Homebrew/.gitignore
/usr/local/Homebrew/.shellcheckrc
/usr/local/Homebrew/.sublime/
/usr/local/Homebrew/.vale.ini
/usr/local/Homebrew/.vscode/
/usr/local/Homebrew/CHANGELOG.md
/usr/local/Homebrew/CODEOWNERS
/usr/local/Homebrew/CONTRIBUTING.md
/usr/local/Homebrew/Dockerfile
/usr/local/Homebrew/LICENSE.txt
/usr/local/Homebrew/Library/
/usr/local/Homebrew/README.md
/usr/local/Homebrew/bin/brew
/usr/local/Homebrew/completions/
/usr/local/Homebrew/docs/
/usr/local/Homebrew/manpages/
/usr/local/Homebrew/package/
/usr/local/bin/brew -> /usr/local/bin/brew
/usr/local/etc/bash_completion.d/brew -> /usr/local/etc/bash_completion.d/brew
/usr/local/share/doc/homebrew -> /usr/local/share/doc/homebrew
/usr/local/share/fish/vendor_completions.d/brew.fish -> /usr/local/share/fish/vendor_completions.d/brew.fish
/usr/local/share/man/man1/README.md -> /usr/local/share/man/man1/README.md
/usr/local/share/man/man1/brew.1 -> /usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew -> /usr/local/share/zsh/site-functions/_brew
/usr/local/var/homebrew/
Are you sure you want to uninstall Homebrew? This will remove your installed packages! [y/N] y
==> Removing Homebrew installation...
Password:
==> /usr/bin/sudo rm -fr /usr/local/Cellar
==> /usr/bin/sudo /usr/bin/find /usr/local -type l ! -exec /bin/test -e {} ; -delete
==> /usr/bin/sudo rm -fr /usr/local/Homebrew
==> Removing empty directories...
==> /usr/bin/sudo /usr/bin/find /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/var /usr/local/Frameworks -name .DS_Store -delete
==> /usr/bin/sudo /usr/bin/find /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/var /usr/local/Frameworks -depth -type d -empty -exec rmdir {} ;
==> Homebrew uninstalled!
/bin/bash brew-install/install.sh
==> Checking for `sudo` access (which may request your password)...
Password:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew
/etc/paths.d/homebrew
==> The following new directories will be created:
/opt/homebrew/bin
/opt/homebrew/etc
/opt/homebrew/include
/opt/homebrew/lib
/opt/homebrew/sbin
/opt/homebrew/share
/opt/homebrew/var
/opt/homebrew/opt
/opt/homebrew/share/zsh
/opt/homebrew/share/zsh/site-functions
/opt/homebrew/var/homebrew
/opt/homebrew/var/homebrew/linked
/opt/homebrew/Cellar
/opt/homebrew/Caskroom
/opt/homebrew/Frameworks
==> HOMEBREW_BREW_GIT_REMOTE is set to a non-default URL:
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git will be used as the Homebrew/brew Git remote.
==> HOMEBREW_CORE_GIT_REMOTE is set to a non-default URL:
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git will be used as the Homebrew/homebrew-core Git remote.
Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/bin/install -d -o peterzhang -g admin -m 0755 /opt/homebrew
==> Found Git: /Applications/Xcode.app/Contents/Developer/usr/bin/git
==> Downloading and installing Homebrew...
remote: Enumerating objects: 365680, done.
remote: Counting objects: 100% (119861/119861), done.
remote: Compressing objects: 100% (15095/15095), done.
remote: Total 365680 (delta 117922), reused 104810 (delta 104766), pack-reused 245819
remote: Enumerating objects: 55, done.
remote: Counting objects: 100% (35/35), done.
remote: Total 55 (delta 35), reused 35 (delta 35), pack-reused 20
==> /usr/bin/sudo /usr/bin/install -o root -g wheel -m 0644 /var/folders/tj/ww6d0qts1n53txrj8qn82d680000gn/T/tmp.kobvUFoWFk /etc/paths.d/homebrew
HOMEBREW_BREW_GIT_REMOTE set: using https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git as the Homebrew/brew Git remote.
From https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew
* [new branch] master -> origin/master
==> Updating Homebrew...
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/portable-ruby-4.0.7.arm64_big_sur.bottle.tar.gz
######################################################################################################### 100.0%
==> Pouring portable-ruby-4.0.7.arm64_big_sur.bottle.tar.gz
==> Installation successful!
==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
https://github.com/Homebrew/brew#donations
==> Next steps:
- Run these commands in your terminal to add Homebrew to your PATH:
echo >> /Users/peterzhang/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv zsh)"' >> /Users/peterzhang/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv zsh)"
- Run these commands in your terminal to add the non-default Git remotes for Homebrew/brew and Homebrew/homebrew-core:
echo '# Set non-default Git remotes for Homebrew/brew and Homebrew/homebrew-core.' >> /Users/peterzhang/.zprofile
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> /Users/peterzhang/.zprofile
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> /Users/peterzhang/.zprofile
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
- Run brew help to get started
- Further documentation:
https://docs.brew.sh