mac下國內安裝Homebrew教程


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

名稱 說明
brew Homebrew 源代碼倉庫
homebrew-core Homebrew 核心源
homebrew-cask 提供 macOS 應用和大型二進制文件的安裝
homebrew-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)"

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

Command + C中斷腳本執行如下命令:

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

成功執行之后繼續執行前文的安裝命令。

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

最最后執行:

brew update

cask同樣也有首次下載緩慢的問題,解決方法大致同上:

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

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地址。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM