Mac 高手必备神器:Homebrew 保姆级指南
Homebrew 是 macOS 上最流行的包管理器,让你用几条命令就能安装、管理、更新绝大多数开源软件。
1. 什么是 Homebrew?
-
包管理器(Package Manager),类似 Linux 下的
apt、yum -
目标:让 macOS 安装、升级、卸载命令行工具和 GUI 应用都变得“和 macOS 原生”一样简单
-
维护在 GitHub 上的“配方”(Formula / Cask),社区活跃、更新迅速
2. 安装与配置
2.1 安装 Homebrew
在终端(Terminal)执行:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
执行完成后,请根据提示把 Homebrew 加入 PATH,例如在 ~/.zshrc 中添加:
eval "$(/opt/homebrew/bin/brew shellenv)"
2.2 国内镜像(可选,加速)
使用清华镜像:
# 替换 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
# 替换 Bottles 源
echo '
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
' >> ~/.zprofile
source ~/.zprofile
3. 基本命令速查
| 命令 | 功能 |
| -------------------- | ------------------------------------ |
| brew help | 显示帮助 |
| brew update | 更新 Homebrew 本体及配方(Formula) |
| brew upgrade | 升级已安装的软件 |
| brew install <pkg> | 安装 <pkg> |
| brew uninstall <pkg> | 卸载 <pkg> |
| brew search <keyword> | 搜索包含关键字的软件 |
| brew info <pkg> | 查看 <pkg> 的详细信息 |
| brew list | 列出所有已安装软件 |
| brew doctor | 健康检查,诊断常见问题 |
| brew cleanup | 清理旧版本缓存 |
示例:
brew update
brew install git
brew info git
brew upgrade git
brew uninstall git
4. Cask:管理 GUI 应用
-
brew install --cask <app>:安装 macOS 原生应用,如 Chrome、Visual Studio Code -
常用 App 示例:
brew install --cask google-chrome brew install --cask visual-studio-code brew install --cask wechat -
列出已安装的 Cask:
brew list --cask
5. 进阶功能
5.1 管理后台服务(services)
-
启动/停止/开机自启:
brew services start nginx brew services stop nginx brew services restart mysql
5.2 Tap:第三方源
-
添加 Tap:
brew tap homebrew/cask-fonts # 字体源 brew tap mongodb/brew # MongoDB -
搜索 Tap 中的软件:
brew search mongodb
5.3 锁定版本(pin)
-
锁定:
brew pin git -
解锁:
brew unpin git
5.4 Brew Bundle:一键管理依赖
在项目根目录创建 Brewfile:
tap "homebrew/core"
brew "git"
brew "node"
cask "google-chrome"
执行:
brew bundle # 安装 Brewfile 中列出的所有包
brew bundle dump # 生成当前环境的 Brewfile
6. 常用配方推荐
-
Shell & CLI:
zsh,oh-my-zsh,fzf,htop,tmux -
版本控制:
git,tig -
编程语言:
python,node,go,rust,rbenv -
数据库:
mysql,postgresql,redis -
网络工具:
wget,curl,httpie -
虚拟化 & 容器:
docker,minikube -
GUI 应用 (cask):
visual-studio-code,iterm2,slack,notion
7. 故障排查与优化
-
brew doctor:首选健康检查 -
查看日志:
tail -n 50 /usr/local/var/homebrew/logs/<pkg>/*.log -
强制重装:
brew reinstall <pkg> -
清理缓存与旧版本:
brew cleanup -s -
网络问题:检查镜像源、代理设置
8. 高级玩法
-
自定义 Tap & formula:在 GitHub 上创建自己的 Tap,编写 Ruby 脚本定义 formula
-
CI/CD 集成:在 GitHub Actions、GitLab CI 中预装 Brewfile,保证环境可复现
-
离线安装:用
brew fetch下载 Bottle,再拷贝到无网络机器执行brew install --cache -
安全权限:避免使用
sudo brew …;确保/usr/local(Intel)或/opt/homebrew(M1/M2)归你所有
9. 常见问答
Q1. 为什么 brew install git 后,终端还是用系统自带版本?
A1. 检查 PATH 顺序:确保 /usr/local/bin(Intel)或 /opt/homebrew/bin 在 /usr/bin 之前。
Q2. Homebrew 占空间太大怎么办?
A2. brew cleanup、brew prune 清理无用文件;定期检查大文件:du -sh /usr/local/Cellar/*
Q3. 如何回退到旧版本?
A3. 使用 brew extract --version=<ver> <pkg> your/tap 将旧版本抽取到自定义 tap,再安装。
10. 参考文档
-
官方文档:https://brew.sh
-
GitHub 仓库:https://github.com/Homebrew/brew
至此,你已掌握 Homebrew 的安装、管理、进阶与故障排查。快去 brew install 一把,让你的 macOS 终端如虎添翼吧! 🚀