方式一:官网安装
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
简单粗暴。在我刚买了 Mac 时这种方式还可以快速并且顺利地安装完成。但是不知道从何时起这种方式已经行不通了。于是从网上找到了下面这种方式。
方式二:
手动国内镜像安装
- 先将官方脚本下载下来并保存名为 brew_install
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install
- 打开刚才下载好的脚本,并对下方代码进行替换
BREW_REPO = “https://mirrors.ustc.edu.cn/brew.git “.freezeCORE_TAP_REPO = “https://mirrors.ustc.edu.cn/homebrew-core.git“.freeze
新版本的 Homebrew 可能已经没有了 CORE_TAP_REPO,所以即便是没有也不用刻意新增。
- 执行脚本 brew_install
/usr/bin/ruby brew_install
正常情况下会卡在这里👇
==> Tapping homebrew/coreCloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
因为源不通,所以结束了上面的进程。手动执行下方的命令将源更换为中科院的国内镜像
git clone git://mirrors.ustc.edu.cn/homebrew-core.git/ /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1
把 homebrew-core 的源也改为中科院国内镜像
cd "$(brew --repo)"git remote set-url origin https://mirrors.ustc.edu.cn/brew.gitcd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
- 执行更新
brew update
- 检查是否有错误
brew doctor
安装完成!
最新手动安装方法 2021.01.05
由于最新的 homebrew 已经使用 bash 重写,所以上述方法再次失效
- 手动保存 brew_install 脚本
访问 https://raw.githubusercontent.com/Homebrew/install/master/install.sh 并复制内容后在本地手动创建 brew_install 无后缀文件
替换 BREW_REPO 的值为 “https://mirrors.ustc.edu.cn/brew.git“
运行
/bin/bash brew_install
更改安装其他包时的源
直接使用 Homebrew 安装可能也会出现慢的情况,不如干脆把默认源也一并换为国内的镜像
- 替换核心软件仓库
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
- 替换 cask 软件仓库(提供 macOS 应用和大型二进制文件)
cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-caskgit remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
- 替换 Bottles 源(Homebrew 预编译二进制软件包)
bsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profilesource ~/.bash_profile
zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrcsource ~/.zshrc
备用方案
brew 常用命令

