2021-02-01 23:18:33 +08:00

117 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# M1 芯片 Mac 上 Homebrew 安装
目前`Homebrew`还没有完成对搭载`Apple Silicon CPU``Mac`机型完成适配,安装教程也比较混乱,所以我整理这篇文章,希望能帮到大家。
关于进度的信息可以浏览`GitHub`上的[说明](https://github.com/Homebrew/brew/issues/7857)。
本文简单介绍了`M1`芯片`Mac`上安装`Homebrew`的方法,同时建议小伙伴结合 [mac 安装教程](/guide/) 一起阅读并安装。
## 安装 ARM 版 Homebrew
`ARM``Homebrew`需要安装在`/opt/homebrew`路径下,早期的时候需要手动创建目录执行命令,目前使用最新脚本不需要手动操作。
直接执行:
```shell
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
或者使用我提供的脚本安装,好处是安装脚本可稳定访问,安装默认使用中科大镜像:
```shell
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
```
然后还需设置环境变量,在`~/.zprofile`或者`~/.bash_profile`文件末尾追加下面代码:
```shell
export PATH=/opt/homebrew/bin:$PATH
```
具体操作步骤如下,一定要仔细阅读。
PS: 终端类型根据执行命令`echo $SHELL`显示的结果:
- `/bin/bash` => `bash` => `.bash_profile`
- `/bin/zsh` => `zsh` => `.zprofile`
**如果遇到环境变量无效问题,建议回过头来查看终端类型,再做正确的设置。**
`macOS Catalina`(10.15.x) 版开始,`Mac`使用`zsh`作为默认`Shell`,使用`.zprofile`,所以对应命令:
```shell
echo export PATH=/opt/homebrew/bin:$PATH >> ~/.zprofile
source ~/.zprofile
```
如果是`macOS Mojave` 及更低版本,并且没有自己配置过`zsh`,使用`.bash_profile`
```shell
echo export PATH=/opt/homebrew/bin:$PATH >> ~/.bash_profile
source ~/.bash_profile
```
扩展阅读:[在 Mac 上将 zsh 用作默认 Shell](https://support.apple.com/zh-cn/HT208050)
## 安装 X86 版 Homebrew
因为目前很多软件包没有支持`ARM`架构,我们也可以考虑使用`x86`版的`Homebrew`
在命令前面添加`arch -x86_64`,就可以按 X86 模式执行该命令,比如:
```shell
arch -x86_64 /bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
```
## 多版本共存
如果你同时安装了 ARM 和 X86 两个版本,那你需要设置别名,把命令区分开。
同样是`.zprofile`或者`.bash_profile`里面添加:
至于操作哪个文件,请参考前文关于终端类型的描述,下文如有类似文字,保持一样的操作。
```shell
alias abrew='arch -arm64 /opt/homebrew/bin/brew'
alias ibrew='arch -x86_64 /usr/local/bin/brew'
```
`abrew``ibrew`可以根据你的喜好自定义。
然后再执行`source ~/.zprofile``source ~/.bash_profile`命令更新文件。
## 设置镜像
**注意:本文中的安装脚本会设置中科大源镜像,如果你也想设置`cask`和`bottles`的镜像,请按下面注释部分选择执行代码。**
更详细的教程可以参考前面的文章:[mac 安装教程](/guide/start/#part3)
执行时根据实际情况修改`"$(brew --repo)"`代码中的`brew`
意思是如果你只是使用一个版本`Homebrew`,直接执行命令即可,如果你想多个版本共存或者使用了别名,就把`brew`关键字替换为别名名称,如前面的`abrew``ibrew`
```shell
# brew
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# cask
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# bottles for zsh 和下面2选1
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zprofile
source ~/.zprofile
# bottles for bash 和上面2选1
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
```
具体镜像设置参考[mac 安装教程](/guide/start/#part3)=>"设置镜像"一节。
**如果觉得教程有用,欢迎多多分享宣传~**
[mac]:[https://zhuanlan.zhihu.com/p/90508170][github]:[https://github.com/ineo6/homebrew-install]