brew 默认使用 github.com
和 bintray.com
的更新或者下载二进制包。
但是 github 和 bintray 访问都有点小慢,特别是 bintray.com
.
一: Git 仓库地址
对于前者,我们可以修改 brew 的 git 仓库地址。
我当前所在地是上海,可用的镜像有
阿里云镜像
中国科学技术大学开源软件镜像
清华大学的TUNA
不过 阿里云镜像
的 homebrew-bottles
镜 好像不能工作了,所以这里不讨论如何设置为阿里云的镜像。
1. 设置 brew.git 仓库地址
TNUA
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
USTC
git -C "$(brew --repo)" remote set-url origin http://mirrors.ustc.edu.cn/brew.git
2. 设置 homebrew-core.git 仓库地址
TNUA
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
USTC
git -C "$(brew --repo homebrew/core)" remote set-url origin http://mirrors.ustc.edu.cn/homebrew-core.git
3. 设置 homebrew-cask.git 仓库地址
TNUA
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
USTC
git -C "$(brew --repo homebrew/cask)" remote set-url origin http://mirrors.ustc.edu.cn/homebrew-cask.git
二: 二进制文件下载地址
因为二进制默认包都是从 bintray.com
下载的,但是我们的网络访问它有点慢。
幸好 brew 给我我们可以配置的机会,我们可以在环境中添加一个变量 HOMEBREW_BOTTLE_DOMAIN
来让 brew 到指定的地址下载二进制文件。
由于大家有不同的 shell 使用习惯,环境变量的配置文件也不同,所以我这里只用 bash
作为演示。
echo "export HOMEBREW_BOTTLE_DOMAIN=$HOMEBREW_BOTTLE_DOMAIN_URL" >> ~/.bash_profile
source ~/.bash_profile
其中 $HOMEBREW_BOTTLE_DOMAIN_URL
的值有:
TNUA
https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
USTC
http://mirrors.ustc.edu.cn/homebrew-bottles
当修改完以上地址之后,需要调用 brew update
来更新以下仓库.
然后用 brew config
来查看改变是有成功吧!
以下是我修改之后的输出:
第二行
HOMEBREW_VERSION: 2.1.11
ORIGIN: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
HEAD: 37714b5ce1e4909d4d61de2af98343c7012f7cd9
Last commit: 8 days ago
Core tap ORIGIN: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
Core tap HEAD: 883eec40b32f7b94f597c2d3e8b677e27d38141f
Core tap last commit: 6 hours ago
HOMEBREW_PREFIX: /usr/local
CPU: quad-core 64-bit skylake
Homebrew Ruby: 2.3.7 => /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby
Clang: 10.0 build 1001
Git: 2.20.1 => /Library/Developer/CommandLineTools/usr/bin/git
Curl: 7.54.0 => /usr/bin/curl
Java: 1.8.0_221
macOS: 10.14.5-x86_64
CLT: 10.3.0.0.1.1562985497
Xcode: 10.3
CLT headers: 10.3.0.0.1.1562985497
三: 脚本
配置太多?不好复制或者粘贴?我给你写好了脚本。
使用方法
将下面的脚本内容
复制到一个新建的文件中, 文件名字和后缀名都可以随便取。
新建文件可以通过 touch 命令
例如: touch name.txt
在当前文件夹新建一个名为: name.txt 的文件
执行脚本的方式
假设你将下面的脚本保存在了 chbrew.sh 这个文件里面
$SHELL chbrew.sh
默认使用的源地址是 清华大学的TUNA
, 如果你想使用中国科学技术大学开源软件镜像
,
在脚本执行之前定义一个 USE_USTC_BREW
即可
export USE_USTC_BREW=1
$SHELL chbrew.sh
$SHELL
是什么?
$SHELL 是你当前所使用 shell 的二进制文件路径。它是一个 shell 变量,你可以像我一样直接使用它。
脚本内容
########
# Author: Joel<joelzho.com>
# Brief: Change the Homebrew git repository and bottle domain
########
# The Tsinghua University TNUA homebrew mirrors
TNUA_BREW_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
TNUA_BREW_CORE=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
TNUA_BREW_CASK=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
TNUA_BREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
# The University of Science and Technology of China open source software mirror of homebrew
USTC_BREW_GIT=http://mirrors.ustc.edu.cn/brew.git
USTC_BREW_CORE=http://mirrors.ustc.edu.cn/homebrew-core.git
USTC_BREW_CASK=http://mirrors.ustc.edu.cn/homebrew-cask.git
USTC_BREW_BOTTLE_DOMAIN=http://mirrors.ustc.edu.cn/homebrew-bottles
# The target HOMEBREW_BOTTLE_DOMAIN url
# HOMEBREW_BOTTLE_DOMAIN_URL
if [ ! $USE_USTC_BREW ]; then
git -C "$(brew --repo)" remote set-url origin $TNUA_BREW_GIT
git -C "$(brew --repo homebrew/core)" remote set-url origin $TNUA_BREW_CORE
git -C "$(brew --repo homebrew/cask)" remote set-url origin $TNUA_BREW_CASK
HOMEBREW_BOTTLE_DOMAIN_URL=$TNUA_BREW_BOTTLE_DOMAIN
else
git -C "$(brew --repo)" remote set-url origin $USTC_BREW_GIT
git -C "$(brew --repo homebrew/core)" remote set-url origin $USTC_BREW_CORE
git -C "$(brew --repo homebrew/cask)" remote set-url origin $USTC_BREW_CASK
HOMEBREW_BOTTLE_DOMAIN_URL=$USTC_BREW_BOTTLE_DOMAIN
fi
if [ "$SHELL" = "/bin/bash" ]; then
echo "export HOMEBREW_BOTTLE_DOMAIN=$HOMEBREW_BOTTLE_DOMAIN_URL" >> ~/.bash_profile
source ~/.bash_profile
elif [ "$SHELL" = "/bin/zsh" ]; then
echo "export HOMEBREW_BOTTLE_DOMAIN=$HOMEBREW_BOTTLE_DOMAIN_URL" >> ~/.zshrc
source ~/.zshrc
else
echo "!!!unknown shell you are using!!"
fi
brew update
brew config
# restore to defaults
###########################################################################################
#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
#git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git
#
# delete the variable HOMEBREW_BOTTLE_DOMAIN in file ~/.bash_profile or ~/.zshrc
#
#brew update
###########################################################################################