方式一:官网安装

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

简单粗暴。在我刚买了 Mac 时这种方式还可以快速并且顺利地安装完成。但是不知道从何时起这种方式已经行不通了。于是从网上找到了下面这种方式。

方式二:

手动国内镜像安装

  1. 先将官方脚本下载下来并保存名为 brew_install
  1. curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install
  1. 打开刚才下载好的脚本,并对下方代码进行替换
  1. BREW_REPO = https://mirrors.ustc.edu.cn/brew.git “.freeze
  2. CORE_TAP_REPO = https://mirrors.ustc.edu.cn/homebrew-core.git“.freeze

新版本的 Homebrew 可能已经没有了 CORE_TAP_REPO,所以即便是没有也不用刻意新增。

  1. 执行脚本 brew_install
  1. /usr/bin/ruby brew_install

正常情况下会卡在这里👇

  1. ==> Tapping homebrew/core
  2. Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...

因为源不通,所以结束了上面的进程。手动执行下方的命令将源更换为中科院的国内镜像

  1. git clone git://mirrors.ustc.edu.cn/homebrew-core.git/ /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1

把 homebrew-core 的源也改为中科院国内镜像

  1. cd "$(brew --repo)"
  2. git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
  3. cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
  4. git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
  1. 执行更新
  1. brew update
  1. 检查是否有错误
  1. brew doctor

安装完成!


最新手动安装方法 2021.01.05

由于最新的 homebrew 已经使用 bash 重写,所以上述方法再次失效

  1. 手动保存 brew_install 脚本

访问 https://raw.githubusercontent.com/Homebrew/install/master/install.sh 并复制内容后在本地手动创建 brew_install 无后缀文件

替换 BREW_REPO 的值为 “https://mirrors.ustc.edu.cn/brew.git

运行

  1. /bin/bash brew_install

更改安装其他包时的源

直接使用 Homebrew 安装可能也会出现慢的情况,不如干脆把默认源也一并换为国内的镜像

  1. 替换核心软件仓库
  1. cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
  2. git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
  1. 替换 cask 软件仓库(提供 macOS 应用和大型二进制文件)
  1. cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
  2. git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
  1. 替换 Bottles 源(Homebrew 预编译二进制软件包)

bsh

  1. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
  2. source ~/.bash_profile

zsh

  1. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
  2. source ~/.zshrc

备用方案

Mac Pro 安装 Homebrew (brew)

brew 常用命令

image.png

参考文章