MAC 安装 Homebrew(使用国内镜像源)

注意,如果过程中发生错误,可以查看后面的错误解决方式

自动安装 Homebrew

首先可以尝试自动安装方法,直接一行命令就行:

bash 复制代码
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

如果不行,再尝试手动安装方法

手动安装 Homebrew

首先,需要确保系统中安装了 bash、git 和 curl (对于 macOS 用户,系统自带 bash、git 和 curl),对于 macOS 用户需额外要求安装 Command Line Tools (CLT) for Xcode。在命令行输入 xcode-select --install 安装 CLT for Xcode 即可。

接着,在终端输入以下几行命令设置环境变量:

ini 复制代码
export HOMEBREW_INSTALL_FROM_API=1
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"

注:自 brew 4.0 起,HOMEBREW_INSTALL_FROM_API 会成为默认行为,无需设置; 大部分用户无需再克隆 homebrew/core 仓库,故无需设置 HOMEBREW_CORE_GIT_REMOTE 环境变量。但若需要运行 brew 的开发命令或者 brew 安装在非官方支持的默认 prefix 位置,则仍需设置 HOMEBREW_CORE_GIT_REMOTE 环境变量; 如果不想通过 API 安装,可以设置 HOMEBREW_NO_INSTALL_FROM_API=1。 不过设置了也不影响

最后,在终端运行以下命令以安装 Homebrew / Linuxbrew(从清华镜像源安装):

bash 复制代码
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install

/bin/bash brew-install/install.sh

rm -rf brew-install

这样在首次安装的时候也可以使用镜像。

安装成功后需将 brew 程序的相关路径加入到环境变量中: 以下针对基于 Apple Silicon CPU 设备上的 macOS 系统(命令行运行 uname -m 应输出 arm64)上的Homebrew:

bash 复制代码
if test -r ~/.bash_profile;then echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile;else touch ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile;fi

if test -r ~/.zprofile;then echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile;else touch ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile;fi

一些错误的解决方式

Error downloading Command Line Tools for Xcode: The network connection was lost. 很明显网络问题,建议手动安装 Command Line Tools for Xcode: 访问(需要注册登陆苹果账号) developer.apple.com/download/al... 然后下载最新的 dmg 安装包安装就行。

error: Rpc failed; curl 92 HTTP/2 stream 5 was not closed cleanly: CANCEL (err 8) error: Rpc failed; curl 18 transfer closed with outstanding read data remaining 这两个是网络问题,建议使用国内源进行安装,也就是使用上面的教程,不过建议你也把 git 的缓冲区调大。 加大缓冲区:编辑 ~/.gitconfig 文件,如下:

执行 brew -v 查看版本没反应 执行上面说的 "安装成功后需将 brew 程序的相关路径加入到环境变量中" 里的命令。然后确认一下 eval "$(/opt/homebrew/bin/brew shellenv)" 这句话是不是在这两个文件中:~/.bash_profile~/.zprofile

相关推荐
Magnum Lehar6 分钟前
macos的信息采集器appledataHarvester-2
macos·网络安全·系统安全
echo-niuben1 小时前
macOS 端已如何正常安装并配置XcodeBuildMCP ?
macos
刘某某.2 小时前
Mac上缺失宋体字体,只有宋体-简
macos
Tom·Ge3 小时前
在macOS上安装OpenClaw并实现Chrome网站自动化测试
chrome·macos·策略模式
Lo-Y-eH4 小时前
Mac 安装 Ollama 部署 DeepSeek 模型
macos·ollama·deepseek·cherry studio
仙剑魔尊重楼20 小时前
iMazing 3.1.3官方中文版新功能介绍
macos·objective-c·cocoa
Genie cloud1 天前
在 Mac 上使用 Docker 安装宝塔并部署 LNMP 环境
macos·docker·容器·云计算
清蒸鳜鱼1 天前
【Mobile Agent——Droidrun】MacOS+Android配置、使用指南
android·macos·mobileagent
linweidong1 天前
屏幕尺寸的万花筒:如何在 iOS 碎片化生态中以不变应万变?
macos·ios·移动开发·objective-c·cocoa·ios面试·ios面经
TESmart碲视1 天前
如何用一套键盘鼠标切换多个输入设备?TESmart KVM热键工作原理深度解析.一份涵盖设计原理、使用方法与兼容性的完整技术指南
游戏·macos·计算机外设·kvm切换器·双屏kvm切换器