mac使用homebrew无法安装nginx解决方案

场景

使用 homebrew 安装 nginx

brew install nginx

报错:

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/openssl%401.1-1.1.1k.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/pcre-8.44.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/nginx-1.19.9.mojave.bottle.tar.gz

curl: (22) The requested URL returned error: 404 
Trying a mirror...
==> Downloading https://ghcr.io/v2/homebrew/core/bottles/nginx-1.19.9.mojave.bottle.tar.gz
==> Downloading from https://github.com/-/v2/packages/container/package/homebrew%2Fcore%2Fbottles%2Fnginx-1.19.9.mojave.bottle.tar.gz
Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.
Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.
Warning: Transient problem: timeout Will retry in 4 seconds. 1 retries left.

curl: (28) Connection timed out after 15002 milliseconds
Error: Failed to download resource "nginx"
Download failed: https://ghcr.io/v2/homebrew/core/bottles/nginx-1.19.9.mojave.bottle.tar.gz

原因分析

是因为网络的原因导致的,即homebrew的下载源是外网的,因此下载速度很慢或者会导致下载失败。

解决办法

可能所有的mac用户都遇到过下面这种操蛋情况,在网络环境不太好的时候,你满怀期望的敲下 brew install 应用名称,静静的等待安装结果时,Homebrew 在 Updating 的地方卡死了,真是令人沮丧,当然了,这不是Homebrew的错,Homebrew 是一款 Mac OS 平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。

Homebrew 主要分两部分:git repo(位于 GitHub)和二进制 bottles,这两个网站在国内访问起来都慢的可以。接下来操作一下如何使用阿里云来加速这两部分的下载。

我们可以考虑使用 阿里云 的 Homebrew 镜像源进行加速。
假设你没有更换过镜像源,执行 brew 命令安装应用的时候,下面3 个仓库地址将影响你的下载速度:
brew.git
homebrew-core.git
homebrew-bottles

首先,更换 brew.git

cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

然后更换 homebrew-core.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

运行brew config 来检查一下地址是否更新成功

brew config

最后一步:更换 homebrew-bottles

首先查看一下你的系统当前shell版本
echo $SHELL
根据版本不同,会输出 2 种结果,/bin/zsh 或 /bin/bash,根据不同类型运行不同的命令

/bin/zsh

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

/bin/bash

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

如果阿里云镜像挂了,可以执行下面的命令进行下载源的恢复

# 重置brew.git:
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git

# 重置homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git

随后可以删掉 HOMEBREW_BOTTLE_DOMAIN 环境变量,将你终端文件 ~/.bash_profile 或者 ~/.zshrc 中的HOMEBREW_BOTTLE_DOMAIN这一行删掉,随后执行命令:

source ~/.bash_profile
source ~/.zshrc

猜你喜欢

转载自blog.csdn.net/weekdawn/article/details/123993819