一、簡(jiǎn)介
Homebrew
是一款包管理工具,目前支持macOS
和Linux
系統(tǒng)。主要有四個(gè)部分組成:brew、homebrew-core 、homebrew-cask、homebrew-bottles
名稱 | 說明 |
---|---|
brew | Homebrew源代碼倉(cāng)庫 |
homebrew-core | Homebrew 核心源 |
homebrew-cask | 提供 macOS 應(yīng)用和大型二進(jìn)制文件的安裝 |
homebrew-bottles | 預(yù)編譯二進(jìn)制軟件包 |
二、官方腳本下載安裝及配置環(huán)境變量
首先我們可以進(jìn)入官網(wǎng)地址獲取下載鏈接,如下:
在安裝完成后如果遇到command not found: brew
的錯(cuò)誤,一般是環(huán)境變量無效,這里則需要使用之前介紹使用過的echo $SHELL
來確認(rèn)終端類型,如下
以前每個(gè)新用戶的默認(rèn)shell是/bin/bash
,那么這里我們需要配置文件為~/.bash_profile
,從macOS Catalina(10.15.x) 版開始,Mac使用/bin/zsh
作為默認(rèn)Shell,其對(duì)應(yīng)的配置文件為~/.zshrc
另外我們還需確定下我們電腦CPU的型號(hào),這里可以通過uname -m
來查看,如下
M1芯片ARM版Homebrew最終會(huì)被安裝在/opt/homebrew
路徑下,而之前Intel芯片的Mac則會(huì)被安裝到/usr/local/Homebrew
路徑下。
所以綜上所述,我們?cè)谂渲肏omebrew環(huán)境變量的時(shí)候,需要結(jié)合不同的情況來進(jìn)行相應(yīng)的配置,如下:
M1芯片
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加速
在使用官方腳本進(jìn)行下載安裝時(shí),經(jīng)常會(huì)遇到下面的錯(cuò)誤提示:
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out
因?yàn)楣俜侥_本無法使用的原因是raw.githubusercontent.com
訪問很不穩(wěn)定,這里我們可以采用寫入hosts
的方式,可以一定程度解決GitHub資源無法訪問的問題。
3.1、Mac
這里我們可以打開/etc/hosts
文件,在其中添加相關(guān)配置即可,具體操作可參考:
- 主站:https://github.com/ineo6/hosts
- 鏡像地址:https://gitee.com/ineo6/hosts
最后我們需要刷新緩存,如下
sudo killall -HUP mDNSResponder
3.2、Windows
另外如果是Windows環(huán)境,同理我們將上述文章中內(nèi)容追加到C:/windows/system32/drivers/etc/hosts
文件中,刷新緩存即可。
ipconfig /flushdns
PS:如果覺得需要自己定時(shí)去上述hosts文件中的內(nèi)容太過于麻煩的話,這里推薦可使用通過SwitchHosts
來自動(dòng)更新,操作很簡(jiǎn)單,支持跨平臺(tái),添加一條hosts規(guī)則并啟用,然后就可以按設(shè)置的時(shí)間間隔自動(dòng)與最新的hosts保持同步了。
四、鏡像腳本安裝
如果通過官方提供的方式無法成功安裝,這里推薦使用Homebrew
的鏡像腳本來進(jìn)行安裝,這里比較推薦兩個(gè),首先是如下腳本:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
該腳本用了中科大鏡像加速訪問,僅修改倉(cāng)庫地址部分,不會(huì)產(chǎn)生安全隱患。
關(guān)于中科大所提供的Homebrew鏡像服務(wù)
- https://mirrors.ustc.edu.cn/help/brew.git.html?highlight=homebrew
- https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git/
關(guān)于清華所提供的Homebrew鏡像服務(wù)
- https://mirror.tuna.tsinghua.edu.cn/help/homebrew/
若出現(xiàn)Error: Checksum mismatch.
錯(cuò)誤,報(bào)錯(cuò)代碼如下
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
文件引起的安裝中斷,只需要到上面對(duì)應(yīng)的路徑里,刪掉這個(gè)文件,重新執(zhí)行安裝命令即可。
如果上述命令執(zhí)行卡在如下信息處
==> Tapping homebrew/core
Cloning into ‘/opt/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í)行安裝命令,最后看到如何命令就說明安裝成功了
==> Installation successful!
最后使用下面的命令進(jìn)行更新即可
brew update
如果遇到解決不了的問題,先卸載Homebrew再重新安裝,官方提供的卸載腳本如下:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
當(dāng)然使用官方腳本同樣會(huì)遇到uninstall地址無法訪問問題,那么就可以使用下面腳本:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"
這里刪除不僅可以通過腳本進(jìn)行卸載刪除,還可以在文件夾中進(jìn)行刪除,如下
# 目錄替換為真實(shí)的brew位置
sudo rm -rf 目錄
另外在Mac電腦的終端中,還可以執(zhí)行open /opt/homebrew/
打開文件夾界面進(jìn)行刪除。
PS:各個(gè)環(huán)境下Homebrew的安裝路徑:
- Intel安裝目錄:
/usr/local/Homebrew
- m1 arm安裝目錄:
/opt/homebrew
- linux安裝目錄:
/home/linuxbrew
第二種方法就更加的簡(jiǎn)便了,我們只需要把下面的腳本內(nèi)容復(fù)制到終端執(zhí)行即可
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
然后根據(jù)終端中出現(xiàn)的提示進(jìn)行選擇即可,如果后續(xù)需要卸載也是比較方便的,如下:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"
五、更換鏡像源
上述介紹到,Homebrew通常用來下載安裝軟件的,但它在下載軟件時(shí)非常慢。為了提升下載速度,需要更改Homebrew的安裝源,將其替換成國(guó)內(nèi)鏡像。
5.1、中科大
這里用由中科大負(fù)責(zé)托管維護(hù)的Homebrew鏡像為例。其中brew、homebrew-core
是必備項(xiàng)目,homebrew-cask、homebrew-bottles
按需設(shè)置??赏ㄟ^ brew config 命令可以查看相關(guān)配置信息。
# brew
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# homebrew-core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# homebrew-cask
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
brew update
另外如需設(shè)置bottles
鏡像,還需要配置環(huán)境變量,這里配置文件的判斷上面已經(jīng)介紹過來,這里就不再贅述了,按照不同的情況選擇下面的其中一種配置即可。
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile
# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile
除了中科大的鏡像源外,我們還可以使用其他的鏡像源進(jìn)行配置
5.2、清華
# brew
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# homebrew-core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
# homebrew-cask
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
brew update
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile
# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile
5.3、騰訊
# brew
git -C "$(brew --repo)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/brew.git
# homebrew-core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git
# homebrew-cask
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/homebrew-cask.git
brew update
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile
# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile
5.4、北京外國(guó)語
# brew
git -C "$(brew --repo)" remote set-url origin https://mirrors.bfsu.edu.cn/git/homebrew/brew.git
# homebrew-core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-core.git
# homebrew-cask
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask.git
brew update
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile
# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile
5.5、阿里
# 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
六、恢復(fù)默認(rèn)源
如果我們想要恢復(fù)Homebrew的默認(rèn)鏡像源也是非常的簡(jiǎn)單,其中brew、homebrew-core、homebrew-cask
可通過執(zhí)行如下腳本命令進(jìn)行恢復(fù)文章來源:http://www.zghlxwxcb.cn/news/detail-859745.html
# 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
配置只能手動(dòng)刪除,將~/.zprofile
文件中的HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com
內(nèi)容刪除,并執(zhí)行source ~/.zprofile
即可文章來源地址http://www.zghlxwxcb.cn/news/detail-859745.html
七、Homebrew常用命令
- 查看版本
brew -v
- 更新Homebrew
brew update
- 查看安裝列表
brew list
- 搜索、安裝、更新、卸載軟件,以git為例
# 搜索軟件
brew search git
?
# 安裝軟件
brew install git
?
# 更新軟件
brew upgrade git
?
# 卸載軟件
brew uninstall git
- 查看需要更新的軟件
brew outdated
- 清理舊版本
# 查看可清理的舊版本包,不執(zhí)行實(shí)際操作
brew cleanup -n
?
# 清理所有包的舊版本
brew cleanup
?
# 清理指定包的舊版本
brew cleanup git
到了這里,關(guān)于Mac如何安裝Homebrew的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!