How to Install Native Homebrew on an Apple Silicon M1 Mac

Update@20210702

目前Homebrew已经完全支持M1芯片,无需像本文下面说的这么复杂了,直接用以下命令执行即可。

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

!!!以下部分无需再阅读!!!

如果用原来的方式在M1芯片的macOS中直接安装,会报错。说Homebrew现在还不支持ARM芯片。

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Homebrew is not (yet) supported on ARM processors!
Rerun the Homebrew installer under Rosetta 2.
If you really know what you are doing and are prepared for a very broken
experience you can use another installation option for installing on ARM:
https://docs.brew.sh/Installation

要在Apple Silicon M1芯片的macOS中安装Homebrew有两种方式。

第一种:在Rosetta2下安装x86架构的Homebrew
这一种是我个人不推荐的方式,利用Rosetta2的转码功能,还是直接安装x86架构的Homebrew,后续通过这个Homebrew安装的所有软件,也将是x86架构,虽然通过Rosetta2运行在M1的macOS中也可以正常运行,但是毕竟不如直接编译成M1的ARM架构更放心。

安装方法实际上很简单,执行下面的安装命令即可。

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

第二种:通过自行安装,运行M1 ARM架构原生的Homebrew

##首先创建安装目录
sudo mkdir -p /opt/homebrew

##将目录属主修改为当前用户,方便以后用当前用户直接brew install软件
sudo chown -R $(whoami) /opt/homebrew

##直接下载homebrew tar包并解压
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew

##将路径增加到PATH环境变量中
如果使用的是zsh则直接修改~/.zshrc,如果使用的是bash,则修改~/.bash_profile,我的例子中修改.zshrc
echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.zshrc

##新开一个Terminal窗口或者在当前窗口让环境变量生效
source ~/.zshrc

##现在可以安装软件了,注意要使用-s选项,表示编译源码安装
brew install -s wget

安装完毕以后,我们可以通过file命令查看一下安装后的可执行文件的架构,确认确实是ARM64的原生架构。

$ which wget
/opt/homebrew/bin/wget

$ file /opt/homebrew/bin/wget
/opt/homebrew/bin/wget: Mach-O 64-bit executable arm64

Leave a Reply

Your email address will not be published. Required fields are marked *