1. 程式人生 > 實用技巧 >mac下映象飛速安裝Homebrew教程

mac下映象飛速安裝Homebrew教程

Homebrew是一款包管理工具,目前支援macOSlinux系統。主要有四個部分組成:brewhomebrew-corehomebrew-caskhomebrew-bottles

本文主要介紹Homebrew安裝方式以及如何加速訪問,順便普及一些必要的知識。

1. 指令碼說明

Homebrew預設安裝指令碼:

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

如果你等待一段時間之後遇到下面提示,就說明無法訪問官方指令碼地址:

curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out

請使用下面的指令碼:

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

上面指令碼中使用了中科大映象來加速訪問。

安裝使用到的指令碼原始碼在此homebrew-install,僅修改了倉庫地址部分,不會產生安全隱患,另外求 star 。


官方指令碼無法使用的原因是訪問很不穩定,我上面提供的方案裡是採用了jsdelivr CDN加速訪問。
另外也可以採用寫入hosts的方式,可以一定程度解決GitHub資源無法訪問的問題,我也寫了一篇操作文章,有需要可以閱讀下。
ineo6:解決GitHub網頁githubusercontent地址無法訪問問題​zhuanlan.zhihu.com

2. 安裝說明

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

如果命令執行中卡在下面資訊:

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

Control + C中斷指令碼執行如下命令:

cd "$(brew --repo)/Library/Taps/"
mkdir homebrew && cd homebrew
git clone git://mirrors.ustc.edu.cn/homebrew-core.git

cask同樣也有安裝失敗或者卡住的問題,解決方法也是一樣:

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

成功執行之後繼續執行前文的安裝命令:

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

最後看到==> Installation successful!就說明安裝成功了。

最最後執行:

brew update

3. 如何解除安裝Homebrew

使用官方指令碼同樣會遇到uninstall地址無法訪問問題,可以替換為下面指令碼:

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"

4. 設定映象方法

brewhomebrew/core是必備專案,homebrew/caskhomebrew/bottles按需設定。

通過brew config命令檢視配置資訊。

4.1 中科大源

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

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

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

brew update

# 長期替換homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

注意bottles可以臨時設定,在終端執行下面命令:

export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles

4.2 清華大學源

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

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

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

brew update

# 長期替換homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

4.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

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

brew update

homebrew-bottles配置只能手動刪除,將~/.bash_profile檔案中的HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com內容刪除,並執行source ~/.bash_profile

5. 其他

5.1 cask

目前cask是從GitHub上讀取軟體源,而GitHub Api對匿名訪問有限制,如果使用比較頻繁的話,可以申請Api Token,然後在環境變數中配置到HOMEBREW_GITHUB_API_TOKEN

.bash_profile中追加:

export HOMEBREW_GITHUB_API_TOKEN=yourtoken

注意:因為cask是基於GitHub下載軟體,所以目前是無法加速的。

6. 總結

在前面的過程中我們把brewhomebrew-core的地址都指向到中科大映象。

原理是通過修改install指令碼,在裡面預設映象地址來做到的。

#!/usr/bin/ruby
# This script installs to /usr/local only. To install elsewhere (which is
# unsupported) you can untar https://github.com/Homebrew/brew/tarball/master
# anywhere you like.
HOMEBREW_PREFIX = "/usr/local".freeze
HOMEBREW_REPOSITORY = "/usr/local/Homebrew".freeze
HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
# 這裡替換了BREW_REPO
BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git".freeze

最後不完美的地方是我們只能預設brew映象,沒找到比較好的辦法預設homebrew-corehomebrew-caskgit地址。

參考文章

如果對您有幫助

請給我一個贊⭐️,讓更多人能發現文章,這也是對我的認可和鼓勵。


廣告插播

GitMaster可以樹形展示git專案程式碼。

  • 程式碼樹支援GitHubGitLabGitee
  • ️ 支援私有部署頁面,一鍵標記
  • ️ 檔案提交歷史視覺化git history
  • GitHub通知提醒功能
  • ⬇️ 支援`GitHub`檔案、目錄下載

本文首發部落格:mac下映象飛速安裝Homebrew教程

(二維碼自動識別)