跳轉到內容

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 可以讓你的開發環境管理變得簡單高效!🚀

最後更新於: