2026年11月15日 星期日

macOS 學習筆記 : 安裝套件管理器 Homebrew

Homebrew 是 macOS 上最受歡迎的開源套件管理器, 常被稱為 Mac 上的 App Store 命令列版, 功能類似 Windows 上的 Scoop 或 Linux 的 apt (Ubuntu) 或 apt-get (Debian). 簡言之, Homebrew 是 macOS 的軟體安裝管理中心, 它能讓我們不必手動下載安裝檔, 尋找安裝路徑, 設定環境變數, 就能透過 Terminal 使用指令安裝, 更新, 以及移除開發工具. 

Homebrew 的四大功能 :
  • 安裝軟體 : 快速安裝開發工具及應用程式
  • 更新軟體 : 更新 Homebrew 與已安裝套件
  • 移除軟體 : 完整管理透過 Homebrew 安裝的套件
  • 管理相依套件 : 自動處理軟體所需的相依性
例如安裝 Python 不必自己去 Python 官網下載安裝檔, 只要用 brew install python 指令就可以完成安裝. 注意, 安裝 Homebrew 前必須先安裝好 Xcode 指令列工具, 作法參考前一篇測試 :


本系列全部測試文章索引參考 :



1. 安裝 Homebrew :  

開啟終端機, 輸入下列指令 : 

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

過程中需要輸入 Mac 開機密碼 (輸入時螢幕不會顯示任何字元, 輸完直接按 Enter 即可). 

tony1966@Mac ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password)...
Password: (輸入開機密碼後按 Enter)
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew
/etc/paths.d/homebrew
==> The following new directories will be created:
/opt/homebrew/bin
/opt/homebrew/etc
/opt/homebrew/include
/opt/homebrew/lib
/opt/homebrew/sbin
/opt/homebrew/share
/opt/homebrew/var
/opt/homebrew/opt
/opt/homebrew/share/zsh
/opt/homebrew/share/zsh/site-functions
/opt/homebrew/var/homebrew
/opt/homebrew/var/homebrew/linked
/opt/homebrew/Cellar
/opt/homebrew/Caskroom
/opt/homebrew/Frameworks

Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/bin/install -d -o tony1966 -g admin -m 0755 /opt/homebrew
==> Found Git: /Library/Developer/CommandLineTools/usr/bin/git
==> Downloading and installing Homebrew...
remote: Enumerating objects: 373833, done.
remote: Counting objects: 100% (645/645), done.
remote: Compressing objects: 100% (404/404), done.
remote: Total 373833 (delta 274), reused 427 (delta 206), pack-reused 373188 (from 2)
remote: Enumerating objects: 55, done.
remote: Counting objects: 100% (33/33), done.
remote: Total 55 (delta 33), reused 33 (delta 33), pack-reused 22 (from 1)
==> /usr/bin/sudo /usr/bin/install -o root -g wheel -m 0644 /var/folders/73/5k5h8cbj4vlg1w6jssdkp08w0000gn/T/tmp.P2rElHHfrs /etc/paths.d/homebrew
HOMEBREW_BREW_GIT_REMOTE set: using https://github.com/Homebrew/brew as the Homebrew/brew Git remote.
From https://github.com/Homebrew/brew
 * [new branch]            master     -> origin/master
==> Updating Homebrew...
==> Downloading https://ghcr.io/v2/homebrew/core/portable-ruby/blobs/sha256:43ff285a82f48c84a06d0676a3800815a19d290d32e44182ddd7a6e95478e18e
############################################################################################# 100.0%
==> Pouring portable-ruby-4.0.9.arm64_big_sur.bottle.tar.gz
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run these commands in your terminal to add Homebrew to your PATH:
    echo >> /Users/tony1966/.zprofile
    echo 'eval "$(/opt/homebrew/bin/brew shellenv zsh)"' >> /Users/tony1966/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv zsh)"
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

tony1966@Mac ~ % 

這樣就安裝完成了. 


2. 設定 Homebrew 環境變數路徑 :  

Homebrew 安裝完成後必須設定 Shell 環境, 讓 macOS 能夠在任何目錄直接執行 brew 指令. 我的 Macbook air (M2) 與 Mac mini (M6) 都 Apple Silicon 晶片, 安裝路徑與環境變數設定指令完全相同, 設定指令有三個 : 

echo >> ~/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

第一個指令 : 在 .zprofile 增加換行
第二個指令 : 將 Homebrew 環境設定永久寫入 .zprofile
第三個指令 : 立即載入環境設定

完成環境變數路徑設定後, 用下列指令確認 :

brew --version
brew --prefix
which brew

如果出現下列輸出表示設定成功 :

Homebrew 版本號
/opt/homebrew
/opt/homebrew/bin/brew

tony1966@Mac ~ % echo >> ~/.zprofile
tony1966@Mac ~ % echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
tony1966@Mac ~ % eval "$(/opt/homebrew/bin/brew shellenv)"
tony1966@Mac ~ % brew --version
Homebrew 7.0.13
tony1966@Mac ~ % brew --prefix
/opt/homebrew
tony1966@Mac ~ % which brew
/opt/homebrew/bin/brew

這樣以後每次開啟 Terminal 都會自動載入 Homebrew 環境了.


3. 常用的 Homebrew 指令 :  

常用的 Homebrew 指令如下表 : 


指令 說明
brew --version 查看 Homebrew 版本
brew update 更新 Homebrew 套件資料庫
brew upgrade 更新所有已安裝的套件
brew install 套件名稱 安裝指定套件
brew uninstall 套件名稱 移除指定套件
brew list 列出已安裝的套件
brew search 關鍵字 搜尋可安裝的套件
brew info 套件名稱 查看指定套件的詳細資訊
brew doctor 檢查 Homebrew 環境及設定是否正常
brew cleanup 清除舊版本套件及不需要的快取檔案


Homebrew 將軟體分為兩大類 : 
  • Formula (命令列工具) :
    安裝 CLI, 開發工具, 函式庫等, 通常透過 Terminal 操作, 例如 Python, Git, Node.js, wget, 與htop 等. 
  • Cask (macOS 圖形應用程式) :
    安裝一般 macOS 桌面應用程式, 例如 Google Chrome, Visual Studio Code, Firefox 等. 
例如安裝 VS Code 的 brew 指令 :
 
 brew install --cask visual-studio-code

沒有留言 :