brew install 很慢的解决方法

🍺 Homebrew 加速指南:解决 brew install 缓慢问题

在国内使用 Homebrew,brew install 慢通常是因为默认连接境外服务器。最有效的解决方法是更换为国内镜像源。

🚀 方案一:一键自动换源(新手推荐)

无需手动配置,运行以下命令,根据提示选择镜像源(中科大/清华等),脚本自动完成所有设置。

/bin/zsh -c “$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)”
运行后输入数字选择镜像(例如 1 → 中科大源,2 → 清华源),脚本会自动完成后续配置。

🛠️ 方案二:手动配置镜像源 🇨🇳 中科大源(稳定快速,推荐) bash

1. 替换核心仓库地址

git -C “$(brew –repo)” remote set-url origin https://mirrors.ustc.edu.cn/brew.git git -C “$(brew –repo homebrew/core)” remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git git -C “$(brew –repo homebrew/cask)” remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

2. 配置环境变量(Bottle & API)

echo ‘export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles’ >> ~/.zshrc echo ‘export HOMEBREW_API_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/api’ >> ~/.zshrc

3. 生效并更新

source ~/.zshrc brew update 🏫 清华大学 TUNA 源(教育网优选) bash git -C “$(brew –repo)” remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git git -C “$(brew –repo homebrew/core)” remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git git -C “$(brew –repo homebrew/cask)” remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git echo ‘export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles’ >> ~/.zshrc source ~/.zshrc brew update ☁️ 阿里云源(依赖阿里云服务时可选用) bash git -C “$(brew –repo)” remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git git -C “$(brew –repo homebrew/core)” remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git git -C “$(brew –repo homebrew/cask)” remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-cask.git echo ‘export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew-bottles’ >> ~/.zshrc source ~/.zshrc brew update 注意:以上命令默认适用于 macOS 的 zsh shell。若使用 bash,请将 ~/.zshrc 替换为 ~/.bash_profile 或 ~/.bashrc。

🧹 方案三:清理修复与临时加速技巧 清理缓存与自检 bash brew cleanup # 清理旧版本缓存与下载包 brew doctor # 诊断潜在问题并给出修复建议 🔌 临时使用代理(如果你有 HTTP/Socks5 代理,例如 ClashX / Surge 端口 7890) bash export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890 之后执行 brew install <package> 即可走代理。

⏩ 临时跳过 brew update 直接安装(适合急需安装软件时) bash export HOMEBREW_NO_AUTO_UPDATE=1 brew install <软件名> # 例如 brew install git 也可以将此变量永久加入 ~/.zshrc 来全局禁用自动更新(不推荐长期使用)。

🔄 恢复官方源(切回全球源) 如果需要换回 Homebrew 官方源,执行以下命令:

bash

恢复三个核心仓库地址为官方源

git -C “$(brew –repo)” remote set-url origin https://github.com/Homebrew/brew.git git -C “$(brew –repo homebrew/core)” remote set-url origin https://github.com/Homebrew/homebrew-core.git git -C “$(brew –repo homebrew/cask)” remote set-url origin https://github.com/Homebrew/homebrew-cask.git 然后手动编辑 ~/.zshrc,删除或注释以下两行(若存在):

bash

export HOMEBREW_BOTTLE_DOMAIN=…

export HOMEBREW_API_DOMAIN=…

最后重新加载配置并更新:

bash source ~/.zshrc brew update 💡 常见小贴士 ✅ 更换镜像后第一次运行 brew update 可能稍慢,因为需要同步元数据,之后日常使用会很快。

✅ 如果遇到 homebrew-cask 仓库错误,可暂时忽略 cask 镜像,核心仓库(core)已经够用。

✅ Apple Silicon(M1/M2/M3)芯片的 Mac 同样适用以上配置。

✅ 建议定期执行 brew update && brew upgrade 保持软件源健康。

✅ 部分网络环境(公司防火墙)可能导致 git 协议受阻,可尝试将 remote url 中的 https:// 改为 http://,但更推荐使用一键脚本处理。

终极提速组合:中科大源 + 临时代理(可选)+ 关闭自动更新(安装大软件时)。绝大多数慢的问题都能解决。

评论

0 条已显示

还没有通过审核的评论。