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

相关推荐
飞雪20077 小时前
Alibaba Cloud Linux 3 在 Apple M 芯片 Mac 的 VMware Fusion 上部署的完整密码重置教程(二)
linux·macos·阿里云·vmware·虚拟机·aliyun·alibaba cloud
degree52016 小时前
全平台轻量浏览器推荐|支持Win/macOS/Linux,极速加载+隐私保护+扩展插件,告别广告与数据追踪!
windows·macos·电脑
冯浩(grow up)1 天前
macos 安装nodepad++ (教程+安装包+报错后的解决方法)
macos
2501_928094652 天前
Ps 2025 图像编辑 Photoshop(Mac中文)
图像处理·macos·photoshop·ps
Magnetic_h2 天前
【iOS】锁的原理
笔记·学习·macos·ios·objective-c·cocoa·xcode
Cosmoshhhyyy2 天前
mac环境下安装git并配置密钥等
git·macos
肥肥呀呀呀3 天前
mac 安卓模拟器 blueStacks
macos
csdn_aspnet3 天前
如何在 MacOS 上安装 SQL Server
macos·sqlserver
共享家95274 天前
linux-数据链路层
linux·网络·macos
CZIDC4 天前
MacOS字体看起来比在 Windows 上更好?
macos