Homebrew
是一款包管理工具,目前支持macOS
和linux
系統。主要有四個部分組成: brew
、homebrew-core
、homebrew-cask
、homebrew-bottles
。html
本文主要介紹Homebrew
安裝方式以及如何加速訪問,順便普及一些必要的知識。linux
Homebrew
默認安裝腳本:git
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
若是你等待一段時間以後遇到下面提示,就說明沒法訪問官方腳本地址:github
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out
備註segmentfault
之因此給出現這個緣由,是由於域名被和諧了,咱們只須要配置一下
hosts
文件便可ruby
- 在https://www.ipaddress.com/查詢raw.githubusercontent.com的真實IP
- 配置hosts:199.232.68.133raw.githubusercontent.com,若是不知道怎麼配,請參考 github 訪問太慢,怎麼辦 ??
- 繼續執行默認安裝腳本便可,固然也能夠用下面的這個腳本;
請使用下面的腳本:bash
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
上面腳本中使用了中科大鏡像來加速訪問。app
安裝使用到的腳本源碼在此 homebrew-installcurl
官方腳本沒法使用的緣由是 http://raw.githubusercontent.com訪問很不穩定,我上面提供的方案裏是採用了jsdelivr CDN加速訪問。
另外也能夠採用寫入hosts的方式,能夠必定程度解決GitHub資源沒法訪問的問題,我也寫了一篇操做文章,有須要能夠閱讀下。
ineo6:解決GitHub網頁githubusercontent地址沒法訪問問題zhuanlan.zhihu.com工具
/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
說明
採用做者提供的方法一遍就安裝成功了,也沒有出現上面說的各類異常,可是在下面設置鏡像源的時候發現,缺乏目錄,提示:fatal: Cannot change to '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask': No such file or directory
,進入目錄發現確實沒有 homebrew-cask 目錄,這時候只需執行brew cask
便可解決該問題
使用官方腳本一樣會遇到uninstall
地址沒法訪問問題,能夠替換爲下面腳本:
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"
brew
、homebrew/core
是必備項目,homebrew/cask
、homebrew/bottles
按需設置。
經過 brew config
命令查看配置信息。
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
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
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
。
目前cask
是從GitHub
上讀取軟件源,而GitHub Api
對匿名訪問有限制,若是使用比較頻繁的話,能夠申請Api Token
,而後在環境變量中配置到HOMEBREW_GITHUB_API_TOKEN
。
在.bash_profile
中追加:
export HOMEBREW_GITHUB_API_TOKEN=yourtoken
注意:由於cask
是基於GitHub
下載軟件,因此目前是沒法加速的。
在前面的過程當中咱們把brew
和homebrew-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-core
、homebrew-cask
的git
地址。
參考文章