🚀 目录
- 什么是 Homebrew?
- 安装前准备
- 安装 Homebrew(Intel & Apple Silicon)
- 配置国内镜像加速
- 常用命令快速上手
- 安装 GUI 应用:Cask 使用
- 高级优化:自动更新、清理、数据分析
- 卸载与恢复默认
- 总结
1. 什么是 Homebrew?
Homebrew 是 macOS(及 Linux)上流行的开源包管理工具,适合开发者通过命令快捷安装、卸载、更新各种工具与库。它使用 Ruby 脚本 “formulae” 管理依赖,并提供 “bottles” 二进制包加快安装速度。
2. 安装前准备
- macOS 版本:建议 Catalina(10.15)或更高
- Xcode Command Line Tools:在终端运行:
xcode-select --install
3. 安装 Homebrew(Intel & Apple Silicon)
在终端粘贴官方一键脚本:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安装脚本会根据硬件自动选择安装路径:
- Apple Silicon:
/opt/homebrew
- Intel:
/usr/local
根据提示,将 brew 添加进 Shell 路径,如 ~/.zshrc
:
eval "$(/opt/homebrew/bin/brew shellenv)"
4. 配置国内镜像加速(清华 / 中科大 / 阿里云)
国内网络访问 GitHub 较慢,建议配置镜像加速,提高 Homebrew 安装速度。
示例:清华镜像
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
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
brew update
也可替换为中科大、阿里云等镜像源。更多访问源
5. 常用命令快速上手
操作 | 命令 |
---|---|
搜索软件 | brew search <工具名> |
安装软件 | brew install <formula> |
升级软件 | brew upgrade [<formula>] |
卸载软件 | brew uninstall <formula> |
更新 Homebrew 本身 | brew update |
检查系统状态 | brew doctor |
清理旧版本 | brew cleanup |
查看安装列表 | brew list |
查看软件详情 | brew info <formula> |
查看依赖关系 | brew deps <formula> |
关闭自动更新和安装后清理,加速脚本运行:
export HOMEBREW_NO_AUTO_UPDATE=1
export HOMEBREW_NO_INSTALL_CLEANUP=1
6. 安装 GUI 应用:Cask 使用
Cask 可安装 macOS 图形应用、插件、字体等。例如:
brew install --cask google-chrome
brew install --cask visual-studio-code
若频繁使用 Cask,建议设置 GitHub API 令牌 HOMEBREW_GITHUB_API_TOKEN
,避免触发速率限制。
7. 高级优化建议
- 数据采集:Homebrew 默认收集匿名统计,可运行
brew analytics off
关闭。 - Tap 扩展:支持通过
brew tap
添加第三方仓库,例如homebrew/php
。 - 定期维护:建议定期运行:
brew update && brew upgrade && brew cleanup
- 服务管理:可使用
brew services start/stop/restart <formula>
管理后台服务(如 MySQL、Redis)。
8. 卸载与恢复默认
完全卸载 Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
恢复官方源:
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
删除 ~/.zshrc
或 ~/.bash_profile
中的镜像环境变量即可。
9. 总结
- Homebrew 是 macOS 上不可或缺的包管理工具,极大提高开发效率。
- 借助镜像加速,访问速度更快更稳定。
- Cask 支持可一键安装常用 GUI 应用。
- 配合常用命令与定期维护,助你构建高效、可控的开发环境。