在MacBook上安装Homebrew的完整指南如下,覆盖Intel和Apple Silicon(M1/M2)芯片机型,并针对国内网络环境优化:
一、安装前准备
-
检查系统版本
确保macOS版本为10.14或更高(推荐升级至最新版)。 -
安装Xcode命令行工具
这是Homebrew的依赖项,在终端执行:xcode-select --install
若提示“无法安装”,访问苹果开发者官网手动下载对应版本的Command Line Tools安装包。
-
确认芯片架构
终端执行:arch
- Intel芯片:输出
x86_64
- Apple Silicon(M1/M2):输出
arm64
- Intel芯片:输出
二、核心安装步骤(适配所有芯片)
-
执行官方安装脚本
打开终端,粘贴以下命令并回车:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 安装过程中需输入系统密码,按提示操作即可。
- Apple Silicon机型:安装路径为
/opt/homebrew
;Intel机型:路径为/usr/local/Homebrew
。
-
配置环境变量
根据芯片类型,将Homebrew路径添加到环境变量:- Apple Silicon(M1/M2):
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile source ~/.zprofile
- Intel芯片:
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> ~/.zshrc source ~/.zshrc
若使用bash(非默认zsh),将
.zshrc
替换为.bash_profile
。 - Apple Silicon(M1/M2):
-
验证安装
终端执行:brew --version
若输出类似
Homebrew 4.2.1 (arm64)
,则安装成功。
三、国内网络优化(加速下载)
-
替换Homebrew源为清华镜像
执行以下命令替换核心仓库:# 替换brew仓库 git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git # 替换homebrew-core仓库 git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git # 替换homebrew-cask仓库(可选) git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git # 更新本地仓库 brew update
-
配置二进制包镜像(可选)
编辑环境变量配置文件(如.zprofile
),添加:export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles source ~/.zprofile
其他镜像源(如中科大、腾讯)可参考。
四、常见问题解决方案
-
权限错误
若提示Permission denied
,执行:sudo chown -R $(whoami) /opt/homebrew # Apple Silicon sudo chown -R $(whoami) /usr/local # Intel
-
网络连接失败
- 临时设置代理(如Clash):
export http_proxy=http://127.0.0.1:7890 export https_proxy=http://127.0.0.1:7890
- 改用国内镜像安装脚本:
/bin/bash -c "$(curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.sh)"
- 临时设置代理(如Clash):
-
安装后无法找到brew命令
- 检查路径是否正确:
ls -l /opt/homebrew/bin/brew # Apple Silicon ls -l /usr/local/bin/brew # Intel
- 重新执行环境变量配置命令(见步骤二.2)。
- 检查路径是否正确:
-
Command Line Tools安装失败
访问苹果开发者官网手动下载对应版本的Command Line Tools安装包,安装后重新运行Homebrew安装脚本。
五、基础使用示例
-
安装软件包
- 命令行工具(如wget):
brew install wget
- 图形化应用(如Chrome):
brew install --cask google-chrome
- 命令行工具(如wget):
-
更新Homebrew及软件包
brew update # 更新Homebrew自身 brew upgrade # 升级所有已安装软件 brew upgrade wget # 升级指定软件
-
清理旧版本
brew cleanup # 清理缓存和旧版本 brew autoremove # 卸载孤立依赖项
六、验证环境配置
命令 | 作用 |
---|---|
brew doctor | 检查系统环境是否正常 |
brew list | 列出已安装的软件包 |
brew info wget | 查看软件包详细信息 |
brew services list | 查看后台服务状态(如MySQL) |
通过以上步骤,您可以在MacBook上快速安装并高效使用Homebrew。对于开发场景,建议搭配brew install --cask
安装IntelliJ IDEA等工具,并定期执行brew update
保持环境最新。