mac下高效安裝 homebrew 及完美避坑姿勢 (親測有效)

世上無難事,只要找到 Homebrew 的正確安裝方式。

Homebrew 是什麼

Homebrew是 mac的包管理器,僅需執行相應的命令,就能下載安裝需要的軟件包,可以省掉自己去下載、解壓、拖拽(安裝)等繁瑣的步驟。 比如安裝服務器 nginx,打開終端執行以下命令即可安裝:

brew install nginx

Homebrew 官方文檔 //brew.sh/

Homebrew 怎麼安裝

方法一:brew官網的安裝腳本

執行命令:

/bin/bash -c "$(curl -fsSL //raw.githubusercontent.com/Homebrew/install/master/install.sh)"

這時很可能會出現一個問題: 要麼下載極其龜速,要麼直接出現如下提示

curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused

原因:這是 //raw.githubusercontent.com 訪問不穩定引起的。

此時需要換一種科學高效的安裝方法,即方法二。

方法二:brew 鏡像安裝腳本(親測最快速最有效)

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

該腳本用了中科大鏡像加速訪問,僅修改倉庫地址部分,不會產生安全隱患。 關於中科大所提供的 Homebrew 鏡像服務 //lug.ustc.edu.cn/wiki/mirrors/help/brew.git

註:注意這裡的速度,幾百kib/s或幾m/s才是正常的。若只有幾kib/s,通常為無效的安裝方法,一般加載了百分之幾十後就會報錯。

Homebrew 安裝時如何避坑

1.若出現 Error: Checksum mismatch.

報錯代碼如下:

curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
Error: Checksum mismatch.
Expected: b065e5e3783954f3e65d8d3a6377ca51649bfcfa21b356b0dd70490f74c6bd86
Actual: e8a348fe5d5c2b966bab84052062f0317944122dea5fdfdc84ac6d0bd513c137
Archive: /Users/joyce/Library/Caches/Homebrew/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz
To retry an incomplete download, remove the file above.
Error: Failed to install Homebrew Portable Ruby (and your system version is too old)!
Failed during: /usr/local/bin/brew update --force

這裡是由Homebrew目錄下的portable-ruby-2.6.3_2.yosemite.bottle.tar.gz文件引起的安裝中斷,只需要到上面對應的路徑里,刪掉這個文件,重新執行安裝命令即可:

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

2.若卡在了Cloning into…


由這裡的龜速可斷定卡住了,立馬用Control + C中斷腳本,然後執行以下命令:

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

執行後可看到:


速度立馬快得飛起,一下子就能裝好。

註:最後出現 Installation successful! 或者 Checking out files: 100% (5392/5392), done. 說明安裝成功。

Homebrew安裝完為何需要配置

前面已經提到,Homebrew通常用來下載軟件的,但它在安裝軟件時非常慢。為了提升安裝速度,需要更改 Homebrew 的安裝源,將其替換成國內鏡像。

這裡用的是由中科大負責託管維護的 Homebrew 鏡像。其中,前兩個為必須配置的項目,後兩個可按需配置。

1.必備設置

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

2.按需設置

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

首先要先區分你的mac用哪種終端工具,如果是 bash,則執行:

echo 'export HOMEBREW_BOTTLE_DOMAIN=//mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

若是 zsh,則執行:

echo 'export HOMEBREW_BOTTLE_DOMAIN=//mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

註:Homebrew 主要由四個部分組成: brew、homebrew-core 、homebrew-cask、homebrew-bottles,它們對應的功能如下:

組成 功能
Homebrew 源代碼倉庫
homebrew-core Homebrew 核心源
homebrew-cask 提供macos應用和大型二進制文件的安裝
homebrew-bottles 預編譯二進制軟件包

Homebrew 基本用法有哪些

// 查詢:
brew search 軟件名

// 安裝:
brew install 軟件名

// 卸載:
brew uninstall 軟件名

// 更新 Homebrew:
brew update 

// 查看 Homebrew 配置信息:
brew config 

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

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

參考文章

mac下鏡像飛速安裝Homebrew教程

科大Homebrew源

寫在最後

在此之前試過了好幾種安裝方法,比如下載一個 brew_install.rb的文件,然後執行命令ruby brew_install.rb,一開始行得通,但下載到中途就會報錯,而且出錯的方法不一,也嘗試過好幾種相應的解決方法,最終都夭折了,只有這個鏡像安裝才能成功,而且下載速度非常快。

本文作者: 喬一亖
本文鏈接: //www.cnblogs.com/joyce33/p/13376752.html
版權聲明: 本文版權歸作者和博客園共有,轉載請註明出處!如有問題或建議,請多多賜教,非常感謝。