解决国内 brew 下载慢的问题

5,507 阅读1分钟
更新时间:2020-08-05

我们使用Mac 时经常使用homebrew来管理包。但是基于国内网络环境按执行命令时总是会无法连接或链接慢。

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

其实我们可以使用国内的镜像。这里推荐中国科学技术大学的镜像站点 mirrors.ustc.edu.cn/brew.git

安装与替换源

  1. 获取install文件

将官网的brew_install安装脚本通过以下命令下载下来

curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install

如果无法 下载建议直接访问github 仓库, 直接复制内容,然后新建brew_install.sh,将内容复制进去。

  1. 更改脚本中的资源链接,替换成中国科学技术大学的镜像

把这句

BREW_REPO="https://github.com/Homebrew/brew"

更改为

BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git"
  1. 执行脚本
sh brew_install.sh

然后可能卡在了 Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...,可以看到这几句:

==> Tapping homebrew/core

Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...

fatal: unable to access 'https://github.com/Homebrew/homebrew-core/': LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
Error: Failure while executing: git clone https://github.com/Homebrew/homebrew-core /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1

Error: Failure while executing: /usr/local/bin/brew tap 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.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" 
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

执行更新,成功:

brew update

正常输出

warning: no common commits
fatal: 'origin/' is not a commit and a branch '' cannot be created from it
fatal: Needed a single revision
invalid upstream 'origin/'
Already up-to-date.

最后用这个命令检查无错误:

brew doctor

正常输出

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Suspicious https://github.com/Homebrew/brew git origin remote found.
The current git origin is:
  https://mirrors.ustc.edu.cn/homebrew-core.git

With a non-standard origin, Homebrew won't update properly.
You can solve this by setting the origin remote:
  git -C "/usr/local/Homebrew" remote set-url origin https://github.com/Homebrew/brew
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Enumerating objects: 44, done.
remote: Counting objects: 100% (44/44), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 775497 (delta 23), reused 23 (delta 9), pack-reused 775453
Receiving objects: 100% (775497/775497), 309.15 MiB | 1.28 MiB/s, done.
Resolving deltas: 100% (516580/516580), done.
Checking out files: 100% (5419/5419), done.
Tapped 2 commands and 5165 formulae (5,443 files, 339MB).
  1. 更换core源与cask源

直接使用 Homebrew 还需要更改默认源,不然谁用谁想打人,原因你懂的。以下是将默认源替换为国内 USTC 源的方法。

替换核心软件仓库

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

如果你没有安装Homebrew-Cask不用执行下面这句命令替换 cask 软件仓库(提供 macOS 应用和大型二进制文件)

cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

如果 cask 未clone,是可以使用以下命令下载

git clone https://mirrors.ustc.edu.cn/homebrew-cask.git "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask

替换 Bottles 源(Homebrew 预编译二进制软件包)

  • bash(默认 shell)
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
  • zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

Homebrew基本用法

假定操作对象为 wget,请替换为自己需要的软件包名

操作命令
更新 Homebrewbrew update
更新所有安装过的软件包brew upgrade
更新指定的软件包brew upgrade wget
查找软件包brew search wget
安装软件包brew install wget
卸载软件包brew remove wget
列出已安装的软件包brew list
查看软件包信息brew info wget
列出软件包的依赖关系brew deps wget
列出可以更新的软件包brew outdated

cask 与 tap

经常我们会遇到使用第三方仓库软件包 这里我们以新增 AdoptOpenJDK(oracle jdk收费了嘛~)为例

brew tap AdoptOpenJDK/openjdk

然后安装JDK8

brew cask install adoptopenjdk8

AdoptOpenJDK 提供了相当多的jdk版本,例如adoptopenjdk8,adoptopenjdk8-j9,adoptopenjdk14...

参考资料

Homebrew 中文主页

Homebrew Bottles源使用帮助

Homebrew Cask 源使用帮助

Homebrew Core 源使用帮助