Homebrew 安装教程 2022

我在之前写过一个 macOS (ARM64) 安装常用的开发软件 文章,里面有提到 Homebrew 的安装,

但是要手动改写 安装脚本,我觉得还是不太优雅,正好借着重装系统的机会,来优雅一遍。

mirrors

首选去到官网上可以看到 有这么一个章节 Git Remote Mirroring ,并且只需要设定环境变量就可以了。

那么就从这里入手。

这次看到清华大学开源镜像站里没毛病了,所以这次就来用清华大学的镜像

https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/

install

1. 设定环境变量

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. 保存安装脚本

https://github.com/Homebrew/install/blob/master/install.sh

在 Github上复制这个脚本 然后保存到用户文件夹的 install.sh, 别忘了 chmod +x install.sh 提升权限。

3. 运行脚本

./install.sh

虽然是国内的镜像,但homebrew库本身挺大的,需要等上一会。

等到安装完毕 会有这么个log

Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/shenlan/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
- Run these commands in your terminal to add the non-default Git remotes for Homebrew/brew and Homebrew/homebrew-core:
    echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> /Users/shenlan/.zprofile
    echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> /Users/shenlan/.zprofile
    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"
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

4. Next steps

注意:这里是 macOS Monterey 默认但是 zsh,其他情况下注意看提示,不要无脑执行这里给出但命令。

注意看 Next steps,需要把brew添加到 zprofile 里,并且如果要一直使用镜像的话 还需要把环境变量一并写入

那么命令则是

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/shenlan/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> /Users/shenlan/.zprofile
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> /Users/shenlan/.zprofile
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> /Users/shenlan/.zprofile

注意了 这里还有个坑,Next steps只写了brew和core的镜像,但没写bottle的镜像,需要自己加进去。

如果没有 HOMEBREW_BOTTLE_DOMAIN 这个环境变量,默认会跑到 Github Container Repository 去下载,慢的要死还会出错。

5. 备注

如果要更换镜像或者取消镜像 只需要修改 zprofile 的这三个关键环境变量就可以了

  • HOMEBREW_BREW_GIT_REMOTE
  • HOMEBREW_CORE_GIT_REMOTE
  • HOMEBREW_BOTTLE_DOMAIN

至此 全部完成。

see you。

ref: