跳转到内容

Homebrew 软件管理 | 安装、更新、卸载命令完全指南

Homebrew Software Management

Homebrew 最核心的功能就是软件包管理。掌握这些命令,你可以轻松地安装、更新、卸载各种开发工具和应用程序。本文将详细介绍 Homebrew 的软件管理功能。

安装软件

例如我们要安装 Nginx,在下面终端输入以下命令即可。

sh
brew install nginx

安装命令详解

bash
# 基本安装
brew install package_name

# 安装特定版本
brew install nginx@1.24

# 从指定 tap 安装
brew install homebrew/core/nginx

# 仅下载不安装
brew fetch nginx

# 显示详细安装信息
brew install -v nginx

# 强制重新安装
brew reinstall nginx

# 忽略依赖安装
brew install --ignore-dependencies nginx

常用软件安装示例

开发工具

bash
# 版本控制
brew install git
brew install svn

# 编程语言
brew install node
brew install python
brew install ruby
brew install go
brew install rust

# 数据库
brew install mysql
brew install postgresql
brew install redis
brew install mongodb-community

# Web 服务器
brew install nginx
brew install apache2

实用工具

bash
# 文件处理
brew install wget
brew install curl
brew install tree
brew install htop

# 文本编辑
brew install vim
brew install neovim
brew install emacs

# 压缩工具
brew install zip
brew install unzip
brew install 7zip

批量安装软件

bash
# 一次性安装多个软件
brew install git node nginx mysql redis

# 从文件读取列表安装
cat packages.txt | xargs brew install

# packages.txt 内容:
# git
# node
# nginx

搜索软件

1. 使用命令搜索

sh
 brew search [关键词]

搜索示例:

bash
# 搜索包含 "python" 的软件
brew search python

# 搜索以 "node" 开头的软件
brew search /^node/

# 搜索所有 Python 版本
brew search python@

# 输出示例:
# ==> Formulae
# python@3.9    python@3.10   python@3.11   python@3.12
# 
# ==> Casks
# python-launcher

2. 使用网页搜索 https://formulae.brew.sh

在线搜索的优势:

  • 查看软件的详细信息
  • 查看可用版本
  • 查看依赖关系
  • 查看安装统计

查看软件信息

sh
brew info [关键词]

详细信息示例:

bash
brew info nginx

# 输出包含:
# nginx: stable 1.25.3 (bottled), HEAD
# HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
# https://nginx.org/
# Conflicts with:
#   nginx-full (because both install the same binaries)
# /usr/local/Cellar/nginx/1.25.3 (26 files, 2.3MB) *
#   Poured from bottle using the formulae.brew.sh API on 2024-01-15 at 10:30:00
# From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/n/nginx.rb
# License: BSD-2-Clause
# ==> Dependencies
# Build: pkg-config ✔
# Required: openssl@3 ✔, pcre2 ✔
# ==> Options
# --HEAD
#     Install HEAD version
# ==> Caveats
# Docroot is: /usr/local/var/www
# ...

关键信息解读:

  • 版本信息:当前稳定版本和可用选项
  • 安装路径:软件的实际安装位置
  • 依赖关系:安装此软件需要的其他软件
  • 冲突软件:不能同时安装的软件
  • 配置说明:重要的配置信息和使用提示

查看已安装的软件

sh
brew list

更多查看选项:

bash
# 列出所有已安装的公式
brew list

# 列出特定软件的文件
brew list nginx

# 以树形结构显示
brew list --tree

# 显示版本信息
brew list --versions

# 只列出 cask 软件
brew list --cask

# 只列出 formula 软件
brew list --formula

# 统计安装数量
brew list | wc -l

更新已安装的软件

sh
 brew outdated # 查看所有有更新版本的软件
 brew upgrade # 更新所有的软件
 brew upgrade [软件名] #更新单个软件

更新策略详解

检查可更新的软件

bash
# 查看所有过时的软件
brew outdated

# 输出示例:
# git 2.43.0 -> 2.43.1
# node 20.10.0 -> 20.11.0
# nginx 1.25.2 -> 1.25.3

更新软件

bash
# 更新所有软件
brew upgrade

# 更新特定软件
brew upgrade nginx

# 更新多个软件
brew upgrade nginx node git

# 更新时跳过某些软件
brew upgrade --except=mysql,postgresql

# 仅更新,不清理旧版本
brew upgrade --cleanup=none

版本锁定

bash
# 锁定软件版本(防止自动更新)
brew pin nginx

# 解除锁定
brew unpin nginx

# 查看已锁定的软件
brew list --pinned

回退到旧版本

bash
# 查看软件的历史版本
brew log nginx

# 安装旧版本(需要安装 homebrew/versions tap)
brew install nginx@1.24

# 切换版本
brew unlink nginx
brew link nginx@1.24

卸载某个已经安装的包

sh
 brew uninstall [软件名]

卸载详解

bash
# 基本卸载
brew uninstall nginx

# 强制卸载(即使有其他软件依赖)
brew uninstall --force nginx

# 卸载并清理相关文件
brew uninstall --zap nginx

# 卸载多个软件
brew uninstall nginx mysql redis

# 卸载所有未使用的依赖
brew autoremove

完全清理

bash
# 卸载后清理缓存
brew cleanup nginx

# 清理所有旧版本
brew cleanup

# 查看可以清理的空间
brew cleanup -n

# 自动清理(添加到 cron 或定期执行)
brew cleanup -s

查看包信息

sh
brew info [软件名]

高级信息查询:

bash
# 查看软件的依赖树
brew deps --tree nginx

# 查看哪些软件依赖于此软件
brew uses nginx

# 查看软件的安装历史
brew log nginx

# 查看软件的测试状态
brew audit nginx

# 查看软件的分析信息
brew analytics

清理软件的旧版

sh
brew cleanup # 清理系统中所有软件的历史版本
brew cleanup [软件名] # 清理特定软件的旧版

自动化清理

bash
# 配置自动清理
echo 'export HOMEBREW_NO_INSTALL_CLEANUP=0' >> ~/.zshrc

# 设置清理频率(每 30 天)
echo 'export HOMEBREW_AUTO_UPDATE_SECS=2592000' >> ~/.zshrc

# 手动触发清理
brew cleanup

# 预览清理内容
brew cleanup -n

# 清理特定软件的旧版本
brew cleanup nginx

# 清理超过 30 天的版本
brew cleanup --prune=30

管理后台软件

诸如 Nginx``MySQL 等软件,都是有一些服务端软件在后台运行,如果你希望对这些软件进行管理,可以使用 brew services 命令来进行管理

Brew Services 完整指南

bash
# 查看所有服务状态
brew services list

# 输出示例:
# Name       Status  User     File
# mysql      started username ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
# nginx      stopped
# redis      started username ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

服务操作

  • brew services list: 查看所有服务
bash
# 详细列表
brew services list --all
  • brew services run [服务名]: 单次运行某个服务
bash
# 启动服务(不设置开机自启)
brew services run mysql

# 停止服务
brew services stop mysql

# 重启服务
brew services restart mysql
  • brew services start [服务名]: 运行某个服务,并设置开机自动运行。
bash
# 启动并设置开机自启
brew services start mysql

# 启动所有服务
brew services start --all
  • brew services stop [服务名]:停止某个服务
bash
# 停止服务
brew services stop mysql

# 停止所有服务
brew services stop --all
  • brew services restart:重启某个服务。
bash
# 重启服务
brew services restart mysql

# 重启所有服务
brew services restart --all

# 清理服务配置
brew services cleanup

服务配置文件位置

bash
# macOS
~/Library/LaunchAgents/homebrew.mxcl.[service].plist

# Linux
~/.config/systemd/user/homebrew.[service].service

自定义服务配置

bash
# 编辑 MySQL 配置
vim /usr/local/etc/my.cnf

# 编辑 Nginx 配置
vim /usr/local/etc/nginx/nginx.conf

# 编辑 Redis 配置
vim /usr/local/etc/redis.conf

# 修改后重启服务
brew services restart mysql

常见服务管理场景

开发环境一键启动

bash
# 创建启动脚本 start-dev.sh
#!/bin/bash
brew services start mysql
brew services start redis
brew services start nginx
echo "Development environment started!"

# 创建停止脚本 stop-dev.sh
#!/bin/bash
brew services stop mysql
brew services stop redis
brew services stop nginx
echo "Development environment stopped!"

# 添加执行权限
chmod +x start-dev.sh stop-dev.sh

检查服务状态

bash
# 检查服务是否运行
brew services list | grep started

# 检查特定服务
brew services info mysql

# 查看服务日志
tail -f /usr/local/var/log/mysql.log
tail -f /usr/local/var/log/nginx/error.log

高级技巧

1. 软件别名

bash
# 为常用软件创建别名
alias bi='brew install'
alias bu='brew uninstall'
alias bs='brew search'
alias bl='brew list'
alias bo='brew outdated'

2. 批量操作脚本

bash
#!/bin/bash
# update-all.sh - 更新所有软件并清理

echo "Updating Homebrew..."
brew update

echo "Checking for outdated packages..."
brew outdated

echo "Upgrading packages..."
brew upgrade

echo "Cleaning up..."
brew cleanup

echo "Done!"

3. 导出已安装软件列表

bash
# 导出 formula 列表
brew list --formula > formula.txt

# 导出 cask 列表
brew list --cask > cask.txt

# 导出完整列表(可用于重装)
brew bundle dump --describe --force

4. 比较两个环境的差异

bash
# 在旧机器上
brew bundle dump --file=old-Brewfile

# 在新机器上
brew bundle dump --file=new-Brewfile

# 比较差异
diff old-Brewfile new-Brewfile

常见问题排查

问题 1:安装失败

bash
# 查看详细错误信息
brew install -v nginx

# 清理缓存重试
brew cleanup nginx
brew install nginx

# 从源码编译
brew install --build-from-source nginx

问题 2:依赖冲突

bash
# 查看依赖关系
brew deps --tree nginx

# 强制重新链接
brew unlink nginx && brew link nginx

# 修复 broken symlinks
brew doctor

问题 3:服务无法启动

bash
# 查看服务状态
brew services list

# 查看错误日志
tail -f /usr/local/var/log/[service].log

# 检查配置文件
brew config
brew doctor

# 重置服务
brew services stop [service]
brew services start [service]

问题 4:空间不足

bash
# 查看 Homebrew 占用的空间
du -sh /usr/local/Cellar
du -sh /usr/local/Caskroom

# 清理所有缓存
brew cleanup --prune=all

# 删除下载的缓存文件
rm -rf ~/Library/Caches/Homebrew/*

最佳实践

1. 定期维护

bash
# 每周执行一次
brew update
brew upgrade
brew cleanup
brew doctor

2. 备份配置

bash
# 备份 Brewfile
cp Brewfile ~/Backup/Brewfile-$(date +%Y%m%d)

# 备份配置文件
tar -czf homebrew-config.tar.gz /usr/local/etc/

3. 文档化

bash
# 在 Brewfile 中添加注释
cat > Brewfile << EOF
# Development Tools
brew "git"
brew "node"

# Database
brew "mysql"
brew "redis"

# Web Server
brew "nginx"
EOF

总结

掌握 Homebrew 软件管理可以显著提升工作效率:

  1. 安装软件:一条命令完成安装
  2. 搜索查找:快速找到需要的软件
  3. 更新管理:保持软件最新版本
  4. 卸载清理:干净地移除不需要的软件
  5. 服务管理:轻松管理后台服务

关键命令速查:

bash
brew install [package]     # 安装
brew search [keyword]      # 搜索
brew info [package]        # 查看信息
brew list                  # 列出已安装
brew upgrade               # 更新
brew uninstall [package]   # 卸载
brew services start [pkg]  # 启动服务
brew cleanup               # 清理

下一步学习:

记住:合理使用 Homebrew 可以让你的开发环境管理变得简单高效!🚀