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)

本文作者:很多无尾熊

本文链接:https://www.cnblogs.com/gnz48/p/16771502.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   很多无尾熊  阅读(1256)  评论(0编辑  收藏  举报
本站已运行[109743159 ]
欢迎光临本站,您是第1位访问者!
春去春来似有期,日高添睡是归时。
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑
点击右上角即可分享
微信分享提示
  1. 1 所念皆星河 房东的猫
  2. 2 所念皆星河 CMJ
  3. 3 热河 南京市民
  4. 4 起风了2018夏 卖辣椒也用券
  5. 5 纸短情长2018夏 烟把儿乐队
  6. 6 关于郑州的记忆 南京市民
  7. 7 定西 南京市民
  8. 8 化作樱花树 SNH48
  9. 9 化青春的约定 SNH48
  10. 10 BINGO! SNH48
  11. 11 恋爱捉迷藏 (2016 Bravery·挑战B50特殊联合公演现场) GNZ48
  12. 12 365天的纸飞机 AKB48 Team SH
  13. 13 《瞬间的永恒》夜色钢琴曲 赵海洋
  14. 14 卡农 我的野蛮女友
  15. 15 爱有天意ost 未知
  16. 16 野蛮女友ost 未知
  17. 17 野蛮女友ost 未知
  18. 18 野蛮女友ost 未知
  19. 19 野蛮女友ost 未知
  20. 20 野蛮女友ost 未知
  21. 21 我想念你...自撸管的悲伤 未知
  22. 22 在人间 未知
  23. 23 野蛮女友ost 未知
  24. 24 野蛮女友ost 未知
  25. 25 風になる つじあやの
  26. 26 潮鳴り 折戸伸治
  27. 27 青石巷 魏琮霏
  28. 28 坐在巷口的那对男女 自然卷
  29. 29 优美的小调(钢琴曲) 张宇桦
  30. 30 天之痕(钢琴版) 群星
  31. 31 花がとぶ飛ぶ 邱有句,李德奎
  32. 32 挺你 IDOL SCHOOL
  33. 33 Eternity 李墨染
  34. 34 北京东路的日子 汪源,刘千楚,徐逸昊,鲁天舒,姜玮珉,胡梦原,张鎏依,梁竞元,游彧涵,金书援,许一璇,张夙西
  35. 35 初恋サイダー Buono!
  36. 36 花朝可期——A-SOUL原创应援曲 林小暗
  37. 37 花之祭 SNH48
  38. 38 ハートサングラス 26時のマスカレイド
  39. 39 47の素敵な街へ(チーム8) AKB48
  40. 40 优美的小调(钢琴曲) 张宇桦
  41. 41 风のように S.E.N.S.
  42. 42 秋~華恋~ α·Pav
  43. 43 同窗 同窗
  44. 44 远方 同窗
  45. 45 流着泪微笑 (合唱版) 鞠婧祎,徐晨辰
  46. 46 初恋蝴蝶 中泰
  47. 47 初恋蝴蝶 jxl
纸短情长2018夏 - 烟把儿乐队
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.
西雅图
04:09发布
西雅图
04:09发布
7°
小雨
北风
3级
空气质量
相对湿度
92%
今天
中雨
6°/10°
周五
中雨
4°/10°
周六
雨夹雪
3°/10°