[教程] macOS 如何安装 Homebrew – 使用清华大学的镜像仓库

Homebrew 是一款免费开源的软件包管理工具,可在 macOS 和 Linux 等操作系统上安装、卸载和管理软件包。它提供了一个简单的命令行界面,可以让用户轻松安装、卸载和更新各种软件包。

Homebrew 是由 Max Howell 和若干其他贡献者开发的。Max Howell 是一名软件工程师,曾在 Twitter 和 Getty Images 等公司担任职位。他于 2009 年开发了 Homebrew,并不断改进和完善其功能,使它成为一种流行的软件包管理工具。Homebrew 常用于安装各种开发工具、编程语言运行环境、数据库管理系统和其他应用软件。

如果你的网络环境可以正常访问外网,则只需在 macOS 上的 Terminal.app 运行以下代码即可开始安装 Homebrew:

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

如果你目前的网络环境很难通过上面的命令安装好 Homebrew, 那你可以参照本文教程使用国内的清华镜像作为 Homebrew 镜像源进行安装。

1. 安装 Command Line Tools (CLT) for Xcode

Command Line Tools (CLT) for Xcode 是一款为 Xcode 开发环境提供命令行工具的软件包。它包括了 Xcode 所需的各种工具,如编译器、调试器、模拟器等,可以在命令行界面下进行编程开发。CLT for Xcode 通常需要和 Xcode 一起安装,以便使用 Xcode 开发环境中的命令行工具。安装 CLT for Xcode 后,可以使用命令行进行编译、调试和模拟等操作,方便开发人员使用。

在运行 Homebrew 安装命令前先安装好 CLT for Xcode,是为了在 macOS 上添加 git 和 curl 命令工具。

  1. 同时按下 Command + 空格键,在弹出的聚焦搜索框内输入 terminal 后按下回车键启动 Terminal.app。
  2. 在 Terminal.app 中输入以下命令,安装 CLT for Xcode:
    xcode-select --install

2. 安装 Homebrew

按照下面步骤开始安装 Homebrew

  1. 使用清华 Homebrew 镜像源;

    在 Terminal.app 内输入下面几行命令设置环境变量,以设置使用清华 Homebrew 镜像源:
    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"
    export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

  2. 开始安装 Homebrew;

    在 Terminal.app 运行以下命令,开始安装 Homebrew:
    git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
    /bin/bash brew-install/install.sh

    开始执行安装命令前,你需要先输入 Mac 的登录密码,然后按下回车键确认。出于安全考虑,你在 Terminal.app 内输入的密码将不会显示出来。

  3. 检查 Homebrew 是否安装成功;

    安装命令结束后,你可以在 Terminal.app 输入以下命令,查看 Homebrew 的版本:
    brew -v
    如果 Homebrew 版本号显示如下格式,未出现 “no Git repository” 的提示,说明 Homebrew 已安装好:
    Homebrew 3.6.14
    Homebrew/homebrew-core (git revision 05100cf2642; last commit 2022-12-09)

3. 针对 Apple Silicon 芯片设备的额外设置

如果你的设备采用的是苹果自家的 M1 及更新的 Apple Silicon 芯片,那你还需要在配置文件中添加一行命令,用来设置 Homebrew 的环境变量:

test -r ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
test -r ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
test -r ~/.zshrc && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
国内注册 ChatGPT 保姆级教程传送门

4. 替换现有仓库为国内镜像仓库

在 Terminal.app 内执行以下代码,替换 Homebrew 现有仓库为国内清华大学的镜像仓库:

export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
for tap in core cask{,-fonts,-drivers,-versions} command-not-found; do
    brew tap --custom-remote --force-auto-update "homebrew/${tap}" "https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-${tap}.git"
done
brew update

上面这段代码的作用是将 Homebrew 的 Git 远程仓库和 Tap 仓库都更改为清华大学的镜像仓库,并更新所有仓库,以便获取最新的软件包。

然后再接着执行以下命令:

test -r ~/.bash_profile && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.bash_profile  # bash
test -r ~/.bash_profile && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.bash_profile
test -r ~/.bash_profile && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.bash_profile
test -r ~/.profile && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.profile
test -r ~/.profile && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.profile
test -r ~/.profile && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.profile
test -r ~/.zprofile && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zprofile
test -r ~/.zprofile && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zprofile
test -r ~/.zprofile && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile
test -r ~/.zshrc && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zshrc
test -r ~/.zshrc && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zshrc
test -r ~/.zshrc && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc

这段代码的作用是在用户的 shell 配置文件中添加三个环境变量,用来设置 Homebrew 的 Git 远程仓库、镜像仓库和软件包仓库,以便使用镜像仓库加快软件包的下载速度,提高安装效率。此外,使用镜像仓库还可以减少对源仓库的访问压力,提高系统的稳定性。



了解 Justin写字的地方 的更多信息

订阅后即可通过电子邮件收到最新文章。


评论

《 “[教程] macOS 如何安装 Homebrew – 使用清华大学的镜像仓库” 》 有 2 条评论

  1. 王云子 的头像

    竟然搜到你的技术文章了,哈哈留下脚印

    1. 哈哈巧了!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

请输入正确的阿拉伯数字即可 *超出时限。 请再次填写验证码。