国内网络访问 GitHub 不稳定导致的 homebrew 安装失败

发布时间:2026/8/21 10:41:05
国内网络访问 GitHub 不稳定导致的 homebrew 安装失败 % /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)” Checking forsudoaccess (which may request your password)…Password: This script will install:/opt/homebrew/bin/brew/opt/homebrew/share/doc/homebrew/opt/homebrew/share/man/man1/brew.1/opt/homebrew/share/zsh/site-functions/_brew/opt/homebrew/etc/bash_completion.d/brew/opt/homebrew/etc/paths.d/homebrewPress RETURN/ENTER to continue or any other key to abort: /usr/bin/sudo /usr/sbin/chown -R edy:admin /opt/homebrew Downloading and installing Homebrew…fatal: unable to access ‘https://github.com/Homebrew/brew/’: Failed to connect to github.com port 443 after 78175 ms: Couldn’t connect to serverWarning: Trying again in 2 seconds: /usr/bin/git fetch --quiet --progress --force originfatal: unable to access ‘https://github.com/Homebrew/brew/’: SSL connection timeoutWarning: Trying again in 4 seconds: /usr/bin/git fetch --quiet --progress --force originfatal: unable to access ‘https://github.com/Homebrew/brew/’: Could not resolve host: github.comWarning: Trying again in 8 seconds: /usr/bin/git fetch --quiet --progress --force originfatal: unable to access ‘https://github.com/Homebrew/brew/’: Could not resolve host: github.comWarning: Trying again in 16 seconds: /usr/bin/git fetch --quiet --progress --force originremote: Enumerating objects: 355727, done.remote: Counting objects: 100% (721/721), done.remote: Compressing objects: 100% (246/246), done.error: RPC failed; curl 56 Recv failure: Operation timed outerror: 30 bytes of body are still expectedfetch-pack: unexpected disconnect while reading sideband packetfatal: early EOFfatal: fetch-pack: invalid index-pack outputFailed 5 times doing: /usr/bin/git fetch --quiet --progress --force origin这是典型的国内网络访问 GitHub 不稳定导致的安装失败。报错中既有 DNS 解析失败也有 SSL 超时和 RPC 断连说明直连 GitHub 完全不可靠。推荐方案使用清华镜像源安装在终端依次执行以下命令先设置镜像环境变量再运行安装脚本# 1. 设置 Homebrew 各组件的国内镜像exportHOMEBREW_API_DOMAINhttps://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/apiexportHOMEBREW_BOTTLE_DOMAINhttps://mirrors.tuna.tsinghua.edu.cn/homebrew-bottlesexportHOMEBREW_BREW_GIT_REMOTEhttps://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.gitexportHOMEBREW_CORE_GIT_REMOTEhttps://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git# 2. 调大 git 缓冲区避免 RPC 断连gitconfig--globalhttp.postBuffer524288000# 3. 重新运行安装脚本/bin/bash-c$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)如果连raw.githubusercontent.com都打不开下载安装脚本本身就失败改用中科大镜像的安装脚本/bin/bash-c$(curl-fsSLhttps://gitee.com/ineo6/homebrew-install/raw/master/install.sh)这个脚本会自动配置国内镜像源。安装完成后把镜像配置写入 shell 配置文件避免以后每次都要手动 export# 如果用 zshmacOS 默认echoexport HOMEBREW_API_DOMAINhttps://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api~/.zshrcechoexport HOMEBREW_BOTTLE_DOMAINhttps://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles~/.zshrcechoexport HOMEBREW_BREW_GIT_REMOTEhttps://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git~/.zshrcechoexport HOMEBREW_CORE_GIT_REMOTEhttps://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git~/.zshrcsource~/.zshrc核心思路安装过程中真正耗时的git fetch从 GitHub 拉取换成清华/中科大镜像后速度和稳定性都会正常。git postBuffer调大则解决大包传输时的 RPC 断连问题。