0%

Homebrew下载加速

前言

Homebrew 是 macOS 上知名的软件包管理工具。但我平时却很少会用到。究其原因,主要还是由于 众所周知 的原因,在国内通过 homebrew 命令安装软件时总是出现 timeout 的情况。

最近打算再创建一个个人博客。我之前的博客采用 hexo 搭建,需要配置nodejs开发环境,而项目目录下的 node_modules 目录其实挺让人感觉无奈的。

后来了解到用go开发的一款静态博客工具 hugo ,安装简单,部署方便。所以打算尝试一下。

但没想到第一步就遇到了问题。

我通过 homebrew 安装 hugo 时,直接就安装失败:

1
2
3
4
5
6
7
8
9
10
11
12
13
➜ brew install hugo
Updating Homebrew...
^[^C==> Downloading https://homebrew.bintray.com/bottles/hugo-0.82.0.big_sur.bottle.
Warning: Transient problem: HTTP error Will retry in 1 seconds. 3 retries
Warning: left.
Warning: Transient problem: HTTP error Will retry in 2 seconds. 2 retries
Warning: left.
Warning: Transient problem: HTTP error Will retry in 4 seconds. 1 retries
Warning: left.
-=O=- # # # #
curl: (22) The requested URL returned error: 502 Bad Gateway
Error: Failed to download resource "hugo"
Download failed: https://homebrew.bintray.com/bottles/hugo-0.82.0.big_sur.bottle.tar.gz

由于我Mac电脑上安装的 homebrew程序版本比较低,所以每次安装软件时都会提示更新,然后等待很长时间。参照网上的教程,通过 Ctrl+C 命令取消它的自动检测更新操作,但这并没有什么卵用,最终还是会安装失败。


配置加速

解决 homebrew 国内下载慢的问题,最简单的方法就是更换国内的镜像源。

查看配置

首先查看 homebrew 当前的配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
➜ brew config
HOMEBREW_VERSION: 3.0.9
ORIGIN: https://github.com/Homebrew/brew
HEAD: ed87211daa83982a6136e14d181a1550b46a0f17
Last commit: 1 year, 7 months ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: 3be1dcad062cfe1639b9eea8c8bc1d27c45aacfa
Core tap last commit: 1 year, 7 months ago
Core tap branch: master
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 8
Homebrew Ruby: 2.6.3 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.3_2/bin/ruby
CPU: octa-core 64-bit haswell
Clang: 12.0 build 1200
Git: 2.33.0 => /usr/local/bin/git
Curl: 7.64.1 => /usr/bin/curl
macOS: 11.4-x86_64
CLT: 1103.0.32.62
Xcode: 12.4

可以看到 homebrew 的版本已经很老了。

更换镜像源

执行brew命令安装应用的时候,跟以下3个仓库地址有关:

  • brew.git
  • homebrew-core.git
  • homebrew-bottles

目前国内镜像源推荐使用的有以下两种:

中科大镜像站 :

1
2
3
4
5
https://mirrors.ustc.edu.cn/brew.git

https://mirrors.ustc.edu.cn/homebrew-core.git

https://mirrors.ustc.edu.cn/homebrew-bottles

清华大学镜像站 :

1
2
3
4
5
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles

这里我使用的 清华大学镜像 ,分别执行如下命令进行替换。

更换 brew.git :

1
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

更换 homebrew-core.git

1
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

然后执行更新命令:

1
brew update

这里需要一些时间,等待其更新。

完成后再次通过 brew config 命令查看配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
➜ brew config
HOMEBREW_VERSION: 3.6.8
ORIGIN: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
HEAD: fa2649dde786208da7d151a89bcafdd23a453f4d
Last commit: 6 days ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: 1bcaa928a1ca0dd2ae12167d5b6a917acc63d8c4
Core tap last commit: 2 hours ago
Core tap branch: master
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 8
Homebrew Ruby: 2.6.8 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.8_1/bin/ruby
CPU: octa-core 64-bit haswell
Clang: 12.0.0 build 1200
Git: 2.33.0 => /usr/local/bin/git
Curl: 7.64.1 => /usr/bin/curl
macOS: 11.4-x86_64
CLT: 11.3.0.32.62
Xcode: 12.4

可以看到 其中的 ORIGIN 地址已经更改,version 也已经更新到最新。

更换 homebrew-bottles

bottles 主要是涉及二进制文件下载的配置。

我们需要在当前shell配置中添加 HOMEBREW_BOTTLE_DOMAIN 参数来进行指定。

查看当前macOS系统中使用的终端shell:

1
echo $SHELL

一般情况下输出为 /bin/bash,我使用的是 zsh ,输出的就是 /bin/zsh

在shell相应的配置文件中添加如下命令 :

1
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles

也可以直接在终端执行添加命令:

/bin/bash

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

source ~/.bash_profile

/bin/zsh

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

source ~/.zshrc

这里要提醒不要忘记刷新配置使其生效。

之后,就可以尽情的通过 brew install 安装软件了。

验证

继续上面未完成的安装操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜ brew install hugo
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/hugo-0.105.0.big_sur.bottle.tar.gz
######################################################################## 100.0%
==> Pouring hugo-0.105.0.big_sur.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
/usr/local/share/zsh/site-functions
==> Summary
🍺 /usr/local/Cellar/hugo/0.105.0: 48 files, 57.8MB
==> Running `brew cleanup hugo`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

基本上没有卡顿,软件安装的非常顺利。

验证:

1
2
➜ hugo version
hugo v0.105.0+extended darwin/amd64 BuildDate=unknown

恢复默认配置

某些情况下可能需要使用 homebrew 的默认配置,可以通过如下命令来恢复。

第一步

重置 brew.githomebrew-core.git :

1
2
3
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

执行更新:

1
brew update

第二步

移除终端配置文件中的 HOMEBREW_BOTTLE_DOMAIN 环境变量,然后执行 source ~/.bash_profilesource ~/.zshrc 使其生效。


git -C 释意

有的朋友看到上面执行的命令中 git -C "$(brew --repo)" 可能感觉有些陌生,另外还有朋友告诉我如下命令:

1
2
3
4
5
cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

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

其实这两种写法最后的执行结果是一样的。只是写法不同,相比之下我使用的命令要更简便一些。


如有疑问或需要技术讨论,请留言或发邮件到 service@itfanr.cc