官方
Homebrew是Mac上非常優秀的軟件包管理工具。
前提
Mac安裝Homebrew的前提條件:
- 64bit Intel CPU或Apple Silicon CPU(M1)
- macOS Mojave(10.14)或更高版本
- 安裝Xcode命令行工具(Command Line Tools for Xcode)
- 可以通過命令行xcode-select --install安裝
- shell(比如bash或zsh)
安裝
打開終端,輸入以下命令:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
其他安裝方法
由於國內的網絡環境等問題,使用官方的安裝方法可能無法安裝成功。可以嘗試使用其他更適合國內網絡環境的安裝方法,參考地址:https://brew.idayer.com/。
安裝homebrew-core
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
安裝homebrew-cask
cd "$(brew --repo)/Library/Taps/homebrew/"
git clone https://mirrors.ustc.edu.cn/homebrew-cask.git
源
為了加速以后使用Homebrew安裝其他軟件的過程,建議設置軟件源為國內源。
查看源
cd "$(brew --repo)" && git remote -v
cd "$(brew --repo homebrew/core)" && git remote -v
cd "$(brew --repo homebrew/cask)" && git remote -v
設置源
設置為中科大的源
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
設置bottles鏡像
從macOS Catalina(10.15.x) 版開始,Mac使用zsh作為默認shell,使用的配置文件:.zprofile
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zprofile
source ~/.zprofile
如果是以前的macOS版本,Mac使用bash作為默認shell,使用的配置文件:.bash_profile
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
重置為官方源
可以通過以下命令還原回官方源。
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
# zsh 注釋掉 HOMEBREW_BOTTLE_DOMAIN 配置
vi ~/.zprofile
# export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
source ~/.zprofile
# bash 注釋掉 HOMEBREW_BOTTLE_DOMAIN 配置
vi ~/.bash_profile
# export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
source ~/.bash_profile
brew update
卸載
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"