1. 程式人生 > 其它 >Mac下Homebrew安裝

Mac下Homebrew安裝

Homebrew 是什麼

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

brew install nginx

Homebrew 官方文件 https://brew.sh/

Homebrew 怎麼安裝

方法一:brew官網的安裝指令碼

執行命令:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
http://raw.githubusercontent.com
訪問不穩定引起的。

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

方法二:brew 映象安裝指令碼(親測最快速最有效)

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

該指令碼用了中科大映象加速訪問,僅修改倉庫地址部分,不會產生安全隱患。 關於中科大所提供的 Homebrew 映象服務 https://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 https://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 https://mirrors.ustc.edu.cn/brew.git
  • 替換 homebrew-core.git:
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

2.按需設定

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

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

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

若是 zsh,則執行:

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://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 https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"



作者:一個尋夢少年
連結:https://www.jianshu.com/p/e0471aa6672d
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。