鏡像下載、域名決議、時間同步請點擊 阿里云開源鏡像站
一、簡介
Homebrew是一款包管理工具,目前支持macOS和Linux系統,主要有四個部分組成:brew、homebrew-core 、homebrew-cask、homebrew-bottles

二、官方腳本下載安裝及配置環境變數
首先我們可以進入官網地址:https://brew.sh 獲取下載鏈接,如下:

在安裝完成后如果遇到command not found: brew的錯誤,一般是環境變數無效,這里則需要使用之前介紹使用過的echo $SHELL來確認終端型別,如下

以前每個新用戶的默認shell是/bin/bash,那么這里我們需要組態檔為~/.bash_profile,從macOS Catalina(10.15.x) 版開始,Mac使用/bin/zsh作為默認Shell,其對應的組態檔為~/.zshrc
另外我們還需確定下我們電腦CPU的型號,這里可以通過uname -m來查看,如下

M1芯片ARM版Homebrew最侄訓被安裝在/opt/homebrew路徑下,而之前Intel芯片的Mac則會被安裝到/usr/local/Homebrew路徑下,
所以綜上所述,我們在配置Homebrew環境變數的時候,需要結合不同的情況來進行相應的配置,如下:
M1芯片
- zsh
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
- bash
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"
Intel芯片
- zsh
echo 'eval "$(/usr/local/Homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/usr/local/Homebrew/bin/brew shellenv)"
- bash
echo 'eval "$(/usr/local/Homebrew/bin/brew shellenv)"' >> ~/.bash_profile
eval "$(/usr/local/Homebrew/bin/brew shellenv)"
三、GitHub加速
在使用官方腳本進行下載安裝時,經常會遇到下面的錯誤提示:
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out
因為官方腳本無法使用的原因是raw.githubusercontent.com訪問很不穩定,這里我們可以采用寫入hosts的方式,可以一定程度解決GitHub資源無法訪問的問題,
3.1、Mac
這里我們可以打開/etc/hosts檔案,在其中添加相關配置即可,具體操作可參考:
主站:https://github.com/ineo6/hosts
鏡像地址:https://gitee.com/ineo6/hosts
最后我們需要重繪快取,如下
sudo killall -HUP mDNSResponder
3.2、Windows
另外如果是Windows環境,同理我們將上述文章中內容追加到C:/windows/system32/drivers/etc/hosts檔案中,重繪快取即可,
ipconfig /flushdns
PS:如果覺得需要自己定時去上述hosts檔案中的內容太過于麻煩的話,這里推薦可使用通過SwitchHosts來自動更新,操作很簡單,支持跨平臺,添加一條hosts規則并啟用,然后就可以按設定的時間間隔自動與最新的hosts保持同步了,
四、鏡像腳本安裝
如果通過官方提供的方式無法成功安裝,這里推薦使用Homebrew的鏡像腳本來進行安裝,這里比較推薦兩個,首先是如下腳本:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
若出現Error: Checksum mismatch. 錯誤,報錯代碼如下
curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
Error: Checksum mismatch.
Expected: b065e5e3783954f3e65d8d3a6377ca51649bfcfa21b356b0dd70490f74c6bd86
Actual: e8a348fe5d5c2b966bab84052062f0317944122dea5fdfdc84ac6d0bd513c137
Archive: /Users/rocky/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檔案引起的安裝中斷,只需要到上面對應的路徑里,刪掉這個檔案,重新執行安裝命令即可,
如果上述命令執行卡在如下資訊處
==> Tapping homebrew/core
Cloning into ‘/opt/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
成功執行之后繼續執行安裝命令,最后看到如何命令就說明安裝成功了
==> Installation successful!
最后使用下面的命令進行更新即可
brew update
如果遇到解決不了的問題,先卸載Homebrew再重新安裝,官方提供的卸載腳本如下:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
當然使用官方腳本同樣會遇到uninstall地址無法訪問問題,那么就可以使用下面腳本:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"
這里洗掉不僅可以通過腳本進行卸載洗掉,還可以在檔案夾中進行洗掉,如下
# 目錄替換為真實的brew位置
sudo rm -rf 目錄
另外在Mac電腦的終端中,還可以執行open /opt/homebrew/打開檔案夾界面進行洗掉,
PS:各個環境下Homebrew的安裝路徑:
- Intel安裝目錄: /usr/local/Homebrew
- m1 arm安裝目錄: /opt/homebrew
- linux安裝目錄:/home/linuxbrew
第二種方法就更加的簡便了,我們只需要把下面的腳本內容復制到終端執行即可
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
然后根據終端中出現的提示進行選擇即可,如果后續需要卸載也是比較方便的,如下:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"
五、更換鏡像源
上述介紹到,Homebrew通常用來下載安裝軟體的,但它在下載軟體時非常慢,為了提升下載速度,需要更改Homebrew的安裝源,將其替換成國內鏡像,
阿里云
# brew
git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
# homebrew-core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
# homebrew-cask
# 缺少
brew update
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zprofile
source ~/.zprofile
# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
六、恢復默認源
如果我們想要恢復Homebrew的默認鏡像源也是非常的簡單,其中brew、homebrew-core、homebrew-cask可通過執行如下腳本命令進行恢復
# brew
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
# homebrew-core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git
# homebrew-cask
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git
brew update
其中homebrew-bottles配置只能手動洗掉,將~/.zprofile檔案中的HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com內容洗掉,并執行source ~/.zprofile即可
七、Homebrew常用命令
查看版本
brew -v
更新Homebrew
brew update
查看安裝串列
brew list
搜索、安裝、更新、卸載軟體,以git為例
# 搜索軟體
brew search git
?
# 安裝軟體
brew install git
?
# 更新軟體
brew upgrade git
?
# 卸載軟體
brew uninstall git
查看需要更新的軟體
brew outdated
清理舊版本
# 查看可清理的舊版本包,不執行實際操作
brew cleanup -n
?
# 清理所有包的舊版本
brew cleanup
?
# 清理指定包的舊版本
brew cleanup git
本文轉自:https://blog.csdn.net/rockvine/article/details/121895416
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460738.html
標籤:Linux
下一篇:Vagrant詳細教程
