天天看點

加快 brew 安裝軟體的速度一: Git 倉庫位址二: 二進制檔案下載下傳位址三: 腳本

我搭建了個人部落格, 歡迎通路:

blog.joelzho.com

brew 預設使用

github.com

bintray.com

的更新或者下載下傳二進制包。

但是 github 和 bintray 通路都有點小慢,特别是

bintray.com

.

一: Git 倉庫位址

對于前者,我們可以修改 brew 的 git 倉庫位址。

我目前所在地是上海,可用的鏡像有

  1. 阿裡雲鏡像

  2. 中國科學技術大學開源軟體鏡像

  3. 清華大學的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
###########################################################################################
           

繼續閱讀