之前听说在 Mac 上也有包管理器,于是我尝试了一下。其中发现了很多坑,在这里记录下来。

听说最好的包管理器是 Homebrew 。于是我访问了官网进行安装。官网网址是:https://brew.sh/
官网上有安装的脚本

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

但是这个脚本要使用到 GitHub 的网站,而在国内的环境下就很容易出现各种问题。解决方法之一是使用代理,然后在终端环境中使用 export 命令设置代理。不过后来我看到了清华的一个镜像网站上介绍的方法。网址:https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/
先设置一下镜像。

if [[ "$(uname -s)" == "Linux" ]]; then BREW_TYPE="linuxbrew"; else BREW_TYPE="homebrew"; fi
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/${BREW_TYPE}-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/${BREW_TYPE}-bottles"

然后使用镜像网站的脚本安装 Brew 。

# 从镜像下载安装脚本并安装 Homebrew / Linuxbrew
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
#下载脚本
/bin/bash brew-install/install.sh
#安装
rm -rf brew-install
#删除刚才下载的脚本

之后设置环境变量,将 Brew 加入到环境变量中。

test -r ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
#这是对于 bash
test -r ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
#这是对于 zsh

之后为 Brew 的软件仓库换源。我之前试过不换源,但是速度非常慢。而且不换源会经常中断导致下载失败。但是我在使用了清华的软件仓库镜像之后发现还是经常中断,于是我找到了一个中科大的镜像。参考了网上的这篇文章后将镜像源换成了中科大的。文章网址:https://brew.idayer.com/guide/m1/

# 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
# 我使用的是 zsh ,所以使用这段脚本。
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile

# bash 使用下面这段脚本
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile