Homebrew
是一款包管理工具又憨,目前支持macOS
和linux
系統(tǒng)袜炕。主要有四個(gè)部分組成: brew
锰扶、homebrew-core
献酗、homebrew-cask
、homebrew-bottles
坷牛。
本文主要介紹Homebrew
安裝方式以及如何加速訪問罕偎,順便普及一些必要的知識(shí)。
1. 腳本說明
Homebrew
默認(rèn)安裝腳本:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
如果你等待一段時(shí)間之后遇到下面提示京闰,就說明無法訪問官方腳本地址:
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out
請(qǐng)使用下面的腳本:
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
上面腳本中使用了中科大鏡像來加速訪問颜及。
2. 安裝說明
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
如果命令執(zhí)行中卡在下面信息:
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
請(qǐng)Control + C
中斷腳本執(zhí)行如下命令:
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
成功執(zhí)行之后繼續(xù)執(zhí)行前文的安裝命令:
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
最后看到==> Installation successful!
就說明安裝成功了蹂楣。
最最后執(zhí)行:
brew update
3. 如何卸載Homebrew
使用官方腳本同樣會(huì)遇到uninstall
地址無法訪問問題器予,可以替換為下面腳本:
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"
4. 設(shè)置鏡像方法
brew
、homebrew/core
是必備項(xiàng)目捐迫,homebrew/cask
乾翔、homebrew/bottles
按需設(shè)置。
通過 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
# 長(zhǎng)期替換homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
注意bottles
可以臨時(shí)設(shè)置反浓,在終端執(zhí)行下面命令:
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
4.2 清華大學(xué)源
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
# 長(zhǎng)期替換homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
4.3 恢復(fù)默認(rèn)源
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
配置只能手動(dòng)刪除,將 ~/.bash_profile
文件中的 HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com
內(nèi)容刪除赞哗,并執(zhí)行 source ~/.bash_profile
雷则。
5. 其他
5.1 cask
目前cask
是從GitHub
上讀取軟件源,而GitHub Api
對(duì)匿名訪問有限制肪笋,如果使用比較頻繁的話月劈,可以申請(qǐng)Api Token
度迂,然后在環(huán)境變量中配置到HOMEBREW_GITHUB_API_TOKEN
。
在.bash_profile
中追加:
export HOMEBREW_GITHUB_API_TOKEN=yourtoken
注意:因?yàn)?code>cask是基于GitHub
下載軟件猜揪,所以目前是無法加速的惭墓。
6. 總結(jié)
在前面的過程中我們把brew
和homebrew-core
的地址都指向到中科大鏡像。
原理是通過修改install
腳本而姐,在里面預(yù)設(shè)鏡像地址來做到的腊凶。
#!/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
最后不完美的地方是我們只能預(yù)設(shè)brew
鏡像,沒找到比較好的辦法預(yù)設(shè)homebrew-core
拴念、homebrew-cask
的git
地址钧萍。