【Mac安裝Git教學】20250817(日)使用Homebrew安裝Git官方更新版本-安裝Homebrew

【Mac安裝Git教學】20250817(日)使用Homebrew安裝Git官方更新版本-安裝Homebrew

使用 Homebrew 在 macOS 上安裝 Git

使用 Homebrew 在 macOS 上安裝 Git

Homebrew 是 macOS 上的套件管理工具,類似 Linux 的 apt(Ubuntu/Debian)或 yum(CentOS)。有了 Homebrew,你可以方便地安裝、更新、移除各種開發工具或應用程式,無需手動從官網下載 dmg/pkg 檔案進行安裝。

檢查 Git 是否已安裝

你可以先檢查系統是否已安裝 Git:

git --version

輸出結果如下:

Arcquantum@Linkou ~ % git --version
git version 2.39.5 (Apple Git-154)

這表示系統已內建 Git(版本 2.39.5,Apple 提供的版本)。但如果你想安裝最新版本的 Git,可以使用 Homebrew。

嘗試使用 Homebrew 安裝 Git

執行以下命令以使用 Homebrew 安裝 Git:

brew install git

如果出現以下錯誤:

Arcquantum@Linkou ~ % brew install git
zsh: command not found: brew

這表示 Homebrew 尚未安裝。

安裝 Homebrew

要安裝 Homebrew,請執行以下命令:

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

這行命令會啟動 Homebrew 的官方安裝程式,執行以下步驟:

  1. 下載並安裝 Homebrew:將 Homebrew 安裝到 /usr/local(Intel Mac)或 /opt/homebrew(Apple Silicon M1/M2/M3 Mac)。
  2. 建立套件管理目錄:在上述路徑下創建必要的資料夾。
  3. 設定環境變數:有時需要將 Homebrew 的路徑加入 ~/.zprofile~/.zshrc

1. 檢查 sudo 權限

Homebrew 安裝需要管理員權限,因為它會修改系統目錄。安裝程式會提示輸入你的 macOS 帳號密碼(輸入時不會顯示字元,這是正常現象):

Arcquantum@Linkou ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password)...
Password:
Sorry, try again.
Password:

2. 安裝檔案與資料夾

安裝程式會列出將要創建的檔案與資料夾(以 Apple Silicon 的 /opt/homebrew 路徑為例,Intel Mac 則為 /usr/local):

==> 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

這些檔案包括:

  • /opt/homebrew/bin/brew:Homebrew 的主要執行程式。
  • 其他檔案:說明文件(man page)、zsh/bash 自動補全檔案等。

3. 設定權限

安裝程式會使用 sudo 修改資料夾的擁有者與讀寫權限,確保你的帳號(例如 Arcquantum)有權管理 Homebrew 套件:

==> The following existing directories will be made group writable:
/opt/homebrew/etc/bash_completion.d
/opt/homebrew/share/doc
/opt/homebrew/share/man
/opt/homebrew/share/man/man1
/opt/homebrew/bin/brew
==> The following existing directories will have their owner set to Arcquantum:
/opt/homebrew/etc/bash_completion.d
/opt/homebrew/share/doc
/opt/homebrew/share/man
/opt/homebrew/share/man/man1
/opt/homebrew/bin/brew

接著執行權限設定:

==> /usr/bin/sudo /bin/chmod u+rwx /opt/homebrew/etc/bash_completion.d /opt/homebrew/share/doc /opt/homebrew/share/man /opt/homebrew/share/man/man1 /opt/homebrew/bin/brew
==> /usr/bin/sudo /bin/chmod g+rwx /opt/homebrew/etc/bash_completion.d /opt/homebrew/share/doc /opt/homebrew/share/man /opt/homebrew/share/man/man1 /opt/homebrew/bin/brew
==> /usr/bin/sudo /usr/sbin/chown Arcquantum /opt/homebrew/etc/bash_completion.d /opt/homebrew/share/doc /opt/homebrew/share/man /opt/homebrew/share/man/man1 /opt/homebrew/bin/brew
==> /usr/bin/sudo /usr/sbin/chown -R Arcquantum:admin /opt/homebrew
==> /usr/bin/sudo /bin/mkdir -p /Users/Arcquantum/Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Users/Arcquantum/Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown -R Arcquantum /Users/Arcquantum/Library/Caches/Homebrew

4. 下載 Homebrew 原始碼

安裝程式會從 GitHub 下載 Homebrew 的原始碼:

==> Downloading and installing Homebrew...
remote: Enumerating objects: 19072, done.
remote: Counting objects: 100% (6321/6321), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 19072 (delta 6289), reused 6278 (delta 6270), pack-reused 12751 (from 2)

5. 設定系統路徑

Homebrew 會建立 /etc/paths.d/homebrew 檔案,讓 macOS 將 /opt/homebrew/bin 加入系統可執行檔路徑,這樣你就可以直接使用 brew 命令:

==> /usr/bin/sudo /bin/mkdir -p /etc/paths.d
==> /usr/bin/sudo tee /etc/paths.d/homebrew
/opt/homebrew/bin
==> /usr/bin/sudo /usr/sbin/chown root:wheel /etc/paths.d/homebrew
==> /usr/bin/sudo /bin/chmod a+r /etc/paths.d/homebrew

6. 更新 Homebrew

安裝完成後,Homebrew 會自動更新到最新版本:

==> Updating Homebrew...
==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:20fa657858e44a4b39171d6e4111f8a9716eb62a78ebbd1491d94f90bb7b830a
######################################################################### 100.0%

7. 安裝便攜版 Ruby

Homebrew 內建便攜版 Ruby(portable-ruby),因為 Homebrew 是用 Ruby 撰寫的,這確保它能在所有系統上運行:

==> Pouring portable-ruby-3.4.5.arm64_big_sur.bottle.tar.gz

8. 安裝完成

安裝成功後,你會看到以下提示:

==> 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

9. 下一步:設定環境變數

為了讓系統識別 brew 命令,你需要將 Homebrew 加入環境變數 PATH。執行以下命令:

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

這些命令會:

  1. ~/.zprofile 中添加一行空白行(避免檔案末尾無換行)。
  2. 添加 Homebrew 的環境變數設定。
  3. 立即應用環境變數到當前終端機會話。

10. 驗證 Homebrew 安裝

執行以下命令檢查 Homebrew 是否成功安裝:

brew --version

預期輸出:

Homebrew 4.6.3

這表示 Homebrew 已成功安裝!

執行完之後,你就可以馬上用 brew 指令了。

測試一下:

brew --version

如果跳出版本號,表示一切正常。

張貼留言

0 留言