MacOS (Apple芯片) 安装 Homebrew 教程

wxh Lv1

MacOS (Apple芯片) 安装 Homebrew 教程

Homebrew 是 MacOS 上最受欢迎的包管理器,本文将指导您在搭载 Apple 芯片的 Mac 上安装 Homebrew。

准备工作

  1. 打开”终端”应用程序
  2. 安装过程中请保持终端窗口打开
  3. 避免同时打开多个终端窗口,以防止环境变量混淆

安装步骤

1. 安装基础命令工具

首先需要安装 git、curl 等基础命令工具。在终端中执行:

1
xcode-select --install

2. 配置 Homebrew 镜像源

为了加快下载速度,我们使用清华大学的镜像源。在终端中执行:

1
2
3
export HOMEBREW_BREW_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_INSTALL_FROM_API=1

3. 安装 Homebrew

执行以下命令安装 Homebrew:

1
2
3
git clone --depth=1 http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install

4. 配置环境变量

根据您使用的 shell 类型,执行相应的命令:

对于 bash 用户:

1
test -r ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile

对于 zsh 用户:

1
test -r ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile

5. 配置国内镜像源

执行以下命令配置 Homebrew 使用国内镜像:

1
2
3
4
5
6
7
export HOMEBREW_BREW_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
brew update

export HOMEBREW_CORE_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
for tap in core cask command-not-found; do
brew tap --custom-remote --force-auto-update "homebrew/${tap}" "http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-${tap}.git"
done

6. 永久配置镜像源

为了确保重启后镜像源配置仍然生效,需要将配置添加到配置文件中:

对于 bash 用户:

1
2
3
4
test -r ~/.bash_profile && echo 'export HOMEBREW_BREW_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.bash_profile
test -r ~/.bash_profile && echo 'export HOMEBREW_CORE_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.bash_profile
test -r ~/.profile && echo 'export HOMEBREW_BREW_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.profile
test -r ~/.profile && echo 'export HOMEBREW_CORE_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.profile

对于 zsh 用户:

1
2
test -r ~/.zprofile && echo 'export HOMEBREW_BREW_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zprofile
test -r ~/.zprofile && echo 'export HOMEBREW_CORE_GIT_REMOTE="http://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zprofile

验证安装

安装完成后,您可以通过以下命令验证 Homebrew 是否安装成功:

1
brew --version

如果显示版本号,则说明安装成功。

注意事项

  1. 安装过程中请保持网络连接稳定
  2. 如果遇到权限问题,可能需要使用 sudo 执行某些命令
  3. 安装完成后建议重启终端,以确保环境变量生效

现在您已经成功安装了 Homebrew,可以开始使用它来安装各种软件包了!

  • 标题: MacOS (Apple芯片) 安装 Homebrew 教程
  • 作者: wxh
  • 创建于 : 2025-03-21 00:00:00
  • 更新于 : 2025-04-13 20:30:09
  • 链接: https://blog.private-line.org/2025/03/21/MacOS安装Brew/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论