HomeBrew配置阿里云镜像

你可能有过这样糟糕的经历,当敲下 brew install 应用名称,静静的等待安装结果时,Homebrew 在 Updating 的地方卡死了。

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 update

此时可以执行 brew config 命令,查看配置信息:

MacBook-Pro :: ~ » brew config
HOMEBREW_VERSION: 2.5.11
ORIGIN: https://mirrors.aliyun.com/homebrew/brew.git
HEAD: 07b3aa69f8dc5098437cf2562864558bae2d45aa
Last commit: 10 days ago
Core tap ORIGIN: https://mirrors.aliyun.com/homebrew/homebrew-core.git
Core tap HEAD: deda5a05551ab74d5313af112d3bb2054f9b6af8
Core tap last commit: 17 hours ago
Core tap branch: master
HOMEBREW_PREFIX: /usr/local
HOMEBREW_BOTTLE_DOMAIN: https://mirrors.aliyun.com/homebrew/homebrew-bottles
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 8
Homebrew Ruby: 2.6.3 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
CPU: octa-core 64-bit icelake
Clang: 12.0 build 1200
Git: 2.24.3 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Curl: 7.64.1 => /usr/bin/curl
macOS: 10.15.7-x86_64
CLT: 1103.0.32.62
Xcode: 12.2

更换 homebrew-bottles

接下来是二进制文件下载的设置。

这与你当前 macOS 系统使用的 shell 版本有关系,执行以下命令查看 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

到这里,更换 Homebrew 默认源的所有操作啦,尽情地去 brew install 吧!

恢复默认配置

出于某些场景,可能需要回退到默认配置,你可以通过下述方式回退到默认配置。

首先执行下述命令:

# 重置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

原文作者:Summer
转自链接:https://learnku.com/mac/wikis/39228
版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。

posted @ 2020-11-26 14:19  会飞的小黑鱼  阅读(1771)  评论(0编辑  收藏  举报