由于自己新买的设备是M1版Macos,了解到Homebrew装系统的开发环境比较方便,且易于管理,此文章作为学习记录笔记

安装ARM版Homebrew

执行命令后,会在/opt/homebrew路径下创建homebrew目录

/bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"

然后还需要设置环境变量,需要先执行命令echo $SHELL显示的结果,接下来需在相应的环境文件配置
以下会出现这2种情况,以自己的系统显示为主

  • /bin/bash => bash => .bash_profile
  • /bin/zsh => zsh => .zprofile

macOS Catalina(10.15.x) 版开始,Mac使用zsh作为默认Shell,使用.zprofile,所以对应命令:

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

如果是macOS Mojave 及更低版本,并且没有自己配置过zsh,使用.bash_profile

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"

安装x86版Homebrew

因为目前很多软件包没有支持ARM架构,我们也可以考虑使用x86版的Homebrew。
在命令前面添加arch -x86_64,就可以按X86模式执行该命令,比如:

arch -x86_64 /bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"

多版本共存

如果你同时安装了ARM和X86两个版本,那你需要设置别名,把命令区分开。
同样是.zprofile或者.bash_profile里面添加:

alias abrew='arch -arm64 /opt/homebrew/bin/brew'
alias ibrew='arch -x86_64 /usr/local/bin/brew'

然后执行source ~/.zprofilesource ~/.bash_profile命令更新文件。

设置镜像

注意:本文中的安装脚本会设置中科大源镜像,如果你也想设置cask和bottles的镜像,请按下面注释部分选择执行代码。
执行时根据实际情况修改"$(brew --repo)"代码中的brew

意思是如果你只是使用一个版本Homebrew,直接执行命令即可,如果你想多个版本共存或者使用了别名,就把brew关键字替换为别名名称,如前面的abrewibrew

# 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/bottles' >> ~/.zprofile
source ~/.zprofile

# bottles for bash 和上面2选1
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile

报错解决

上方指令可能会在设置cask源的时候报错

Error: Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask failed!
fatal: Could not resolve HEAD to a revision
Failed during: /usr/local/bin/brew update --force --quiet

解决思路

卸载已安装的Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

安装新的Homebrew

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

选择中科大下载源

根据提示运行source命令

source /User/xxx/.zprofile

查看brew版本

zzf@ZZFdeMacBook-Pro Cellar % brew -v
Homebrew 3.5.4
Homebrew/homebrew-core (git revision 466cd2c02f6; last commit 2022-07-10)