Homebrew 替换国内镜像源

如果你用的是 zsh,下面命令搞定:

echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc

source ~/.zshrc
brew update

bash 类似~


在国内使用 Homebrew 的速度真心不敢恭维,尤其是在 brew update 的时候经常卡住不动。

换用 brew update --verbose 你会发现是 git fetch 的速度超级慢导致的,所以还是要看 GitHub 的连接速度。

这段时间被迫宅在家,brew 用得越多越不爽,只能试试换成国内镜像源的方法,用后才知道这确实可行。

下面整理几个可用的开源镜像,主要针对的是 macOS 上的 Homebrew,用得着的时候再来看看。

我最先找到的就是清华大学开源软件镜像,这个启发了我挨个去试哪个好用。

首次安装

如果系统上还没有安装 Homebrew,可以不用官方的脚本安装 (托管于 GitHub 🤣),先尝试使用来自清华镜像的安装脚本来安装 Homebrew。

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

接着,继续在命令行中输入一下几行命令设置环境变量:

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"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

最后,在命令行中运行以下命令安装 Homebrew:

# 从清华镜像下载安装脚本并安装 Homebrew
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

这样在首次安装的时候就全交给清华镜像了。

对于运行于 Apple Sillicon (M1) CPU 上的 macOS (uname -m 输出为 arm64),安装成功后需要将 brew 程序的相关路径加入到环境变量中:

test -r ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
test -r ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile

俺没有 M1,不是很懂~ 🤔

如果是 Intel CPU 的同学,如果想继续使用清华镜像,只需要将上面的环境变量写入 ~/.zshrc 就行了,文章后面部分的内容也就不用看了 🤪:

echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc

source ~/.zshrc

:如果用户设置了环境变量 HOMEBREW_BREW_GIT_REMOTE 和 HOMEBREW_CORE_GIT_REMOTE,则每次执行 brew update 时,brew 程序本身和 Core Tap (homebrew-core) 的远程将被自动设置。

来自 brew.sh 的安装脚本,以供参考:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

替换镜像源

如果是已经装好了 Homebrew,想要替换官方源,那就看下面吧。

清华镜像

使用 brew tap 替换 Homebrew 的源仓库地址就行1

export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
brew tap --custom-remote --force-auto-update homebrew/core https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
brew tap --custom-remote --force-auto-update homebrew/cask https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
brew tap --custom-remote --force-auto-update homebrew/cask-fonts https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
brew tap --custom-remote --force-auto-update homebrew/cask-drivers https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-drivers.git
brew tap --custom-remote --force-auto-update homebrew/cask-versions https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-versions.git
brew tap --custom-remote --force-auto-update homebrew/command-not-found https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-command-not-found.git

# 更换后测试工作是否正常
brew update

清华镜像也支持 Linuxbrew,如有需要请移步👉https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/

如果网络支持 IPv6,也可以使用 https://mirrors6.tuna.tsinghua.edu.cn (仅支持 IPv6),或许会更快些。

最近北外开源软件镜像站也启用了,和清华 TUNA 如出一辙,有兴趣的可以试试,只需替换上面的相关地址为:https://mirrors.bfsu.edu.cn。

中科大镜像

中科大镜像也挺有名的,以前我还使用过他们家反代的 Google Fonts。

export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.ustc.edu.cn/homebrew-core.git"
brew tap --custom-remote --force-auto-update homebrew/core https://mirrors.ustc.edu.cn/homebrew-core.git
brew tap --custom-remote --force-auto-update homebrew/cask https://mirrors.ustc.edu.cn/homebrew-cask.git
brew tap --custom-remote --force-auto-update homebrew/cask-versions https://mirrors.ustc.edu.cn/homebrew-cask-versions.git

# 更换后测试工作是否正常
brew update

支持的 tap 比清华的要少些,不过大家最常用的还是 core 和 cask,我在广东的连接速度还是挺快的,也是值得一用。

IPv6 的地址是 ipv6.mirrors.ustc.edu.cn,有需要的可以试试。

腾讯软件源

export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.cloud.tencent.com/homebrew/brew.git"
brew tap --custom-remote --force-auto-update homebrew/core https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git
brew tap --custom-remote --force-auto-update homebrew/cask https://mirrors.cloud.tencent.com/homebrew/homebrew-cask.git
brew tap --custom-remote --force-auto-update homebrew/cask-fonts https://mirrors.cloud.tencent.com/homebrew/homebrew-cask-fonts.git
brew tap --custom-remote --force-auto-update homebrew/cask-drivers https://mirrors.cloud.tencent.com/homebrew/homebrew-cask-drivers.git

# 更换后测试工作是否正常
brew update

腾讯倒是很全呢,良心出品,👍。

 

Homebrew Bottles 源

Homebrew 安装的软件都在 /usr/local/Cellar 目录中,其中 Cellar 意为酒窖,而 Hombrew 官方与预编译好的软件 (二进制软件包) 叫做 Bottle。Homebrew 把安装一个软件到文件夹叫作把一个酒瓶子 (Bottle) 倒入(pour) 酒窖 (Cellar),Bottles 默认是存放在 https://bintray.com/homebrew/bottles 网站中,国内连接速度也不理想。当所要安装的软件不在 bintray 中或从 bintray 下载失败时,Homebrew 会尝试从软件原始地址下载源文件再编译安装,这有时候一般会比安装 Homebrew 预编译好的 Bottle 慢。

上面提到的几个镜像站都提供 bottles 的镜像,所以不妨换上他们的源提高下载二进制软件包的速度。

替换的方法都一样,只是提供的源地址不同罢了,这里就以腾讯的软件源作例子吧。

临时替换

如果只是这一次安装软件需要使用别的镜像源,可以尝试2

export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles

brew update
brew install xxxxx

长期替换

也就是将 HOMEBREW_BOTTLE_DOMAIN 写入系统环境变量。

bash 用户替换方法

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

source ~/.bash_profile

zsh 用户替换方法

目前 macOS 已经将 zsh 设为默认的了。

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

source ~/.zshrc

源地址

  1. https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
  2. https://mirrors.ustc.edu.cn/homebrew-bottles
  3. https://mirrors.cloud.tencent.com/homebrew-bottles

选一个喜欢的替换上面的 HOMEBREW_BOTTLE_DOMAIN 就行。

我目前用的腾讯的 Bottles 源,速度还可以。

换回 Homebrew 官方源

# brew 程序本身
unset HOMEBREW_BREW_GIT_REMOTE
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew

BREW_TAPS="$(BREW_TAPS="$(brew tap 2>/dev/null)"; echo -n "${BREW_TAPS//$'\n'/:}")"
for tap in core cask{,-fonts,-drivers,-versions} command-not-found; do
    if [[ ":${BREW_TAPS}:" == *":homebrew/${tap}:"* ]]; then  # 只复原已安装的 Tap
        brew tap --custom-remote "homebrew/${tap}" "https://github.com/Homebrew/homebrew-${tap}"
    fi
done

# 更换后测试工作是否正常
brew update

Homebrew Bottles 还原的话,只需删除 .bash_profile 或 .zshrc 中相应的 HOMEBREW_BOTTLE_DOMAIN 那行就可以了。

 

 

转载:Homebrew 替换国内镜像源 | inDev. Journal (frankindev.com)

posted @ 2022-10-09 11:18  很多无尾熊  阅读(1312)  评论(0)    收藏  举报
本站已运行[ ]
正在加载今日诗词....