# homebrew(macOS包管理工具)
# 简介
Homebrew是一款包管理工具,目前支持macOS和Linux系统。主要有四个部分组成: brew、homebrew-core 、homebrew-cask、homebrew-bottles。
| 名称 | 说明 |
|---|---|
| brew | Homebrew 源代码仓库 |
| homebrew-core | Homebrew 核心源 |
| homebrew-cask | 提供 macOS 应用和大型二进制文件的安装 |
| homebrew-bottles | 预编译二进制软件包 |
brew官网:https://brew.sh/ (opens new window)
# brew安装&卸载
推荐安装脚本
项目地址1:https://gitee.com/cunkai/HomebrewCN (opens new window)
项目地址2:https://brew.idayer.com/ (opens new window)
# 安装
苹果电脑标准安装脚本:(推荐 优点全面 缺点慢一点)
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
苹果电脑极速安装脚本:(优点安装速度快 缺点update功能需要命令修复 )
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" speed
Linux 标准安装脚本:
rm Homebrew.sh ; wget https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh ; bash Homebrew.sh
# 卸载
苹果电脑卸载脚本:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"
Linux卸载脚本:
rm HomebrewUninstall.sh ; wget https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh ; bash HomebrewUninstall.sh
# brew使用
# 手动设置镜像
# 中科大源
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
# 清华大学源
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
brew update
# 设置 bottles 镜像
设置环境变量需要注意终端的类型,可以先通过以下方式获取:
执行命令echo $SHELL,根据结果判断:
/bin/zsh=>zsh=>.zprofile/bin/bash=>bash=>.bash_profile
然后继续正式操作,以中科大源为例:
从macOS Catalina(10.15.x) 版开始,Mac使用zsh作为默认Shell,对应文件是.zprofile,所以命令为:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile
如果是macOS Mojave 及更低版本,并且没有自己配置过zsh,对应文件则是.bash_profile:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile
注意:上述区别仅仅是
.zprofile和.bash_profile不同,上下文如有再次提及编辑.zprofile,均按此方法判断具体操作的文件。
至此,安装和设置操作都完成了。
# 恢复默认源
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
brew update
homebrew-bottles配置只能手动删除,将 ~/.zprofile 文件中的 HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com内容删除,并执行 source ~/.zprofile。
# cask相关
目前cask是从GitHub上读取软件源,而GitHub Api对访问有限制,如果使用比较频繁的话,可以申请Api Token,然后在环境变量中配置到HOMEBREW_GITHUB_API_TOKEN。
在.zprofile中追加,注意替换yourtoken:
echo 'export HOMEBREW_GITHUB_API_TOKEN=yourtoken' >> ~/.zprofile
source ~/.zprofile
注意:因为cask是从GitHub下载软件,所以目前是无法加速的。