Homebrew 更换为国内镜像(阿里巴巴开源镜像、中科大镜像、清华镜像)

1、定义

Homebrew 是一个很方便的包管理器,主要用来管理 mac 操作系统的包,现在也支持 linux 操作系统的包管理。Homebrew 通过 tap 方式定义源,官方默认提供了 brew、homebrew-core 、homebrew-cask 、homebrew-bottles 四个常用的源(仓库),用户也可以通过 tap 定义自己的源。

在使用 brew install 安装软件的过程中,经常会遇到安装缓慢、卡死的情况,这大多是由于 Homebrew 的默认安装源位于国外,国内访问速度慢造成的,解决这个问题的方法是将安装源替换为国内镜像,常用的国内镜像包括:阿里巴巴开源镜像、中科大镜像、清华镜像等。

本文的编写目的是简单的介绍四个常用的源(源),以及如何更换为国内镜像实现快速安装软件。

2、brew

说明:Homebrew 源代码仓库。
默认地址:https://github.com/Homebrew/brew.git

3、homebrew-core

说明:Homebrew 核心源(仓库),它是 brew install 的默认安装源(仓库)。
默认地址:https://github.com/Homebrew/homebrew-core.git

4、homebrew-cask

说明:homebrew-cask 源(仓库),提供 macOS 应用和大型二进制文件的安装。通常我们在 mac 操作系统上安装图形用户界面软件,系统都会提示“若要安装,请拖动此图标…”。homebrew-cask 扩展了Homebrew,为安装和管理 Atom 和 Google Chrome 之类的图形用户界面应用程序带来了优雅、简单和速度。

默认地址:https://github.com/Homebrew/homebrew-cask.git

5、homebrew-bottles

说明:Homebrew 预编译二进制软件包。
默认地址:https://bintray.com/homebrew/bottles

6、替换为国内镜像源

我的习惯是创建 change_brew.sh 文件,然后在文件中写入替换源需要的命令。以替换为中科大镜像为例,在 change_brew.sh 文件中写入如下内容,并执行该脚本,就可以成功切换镜像源。

#!/bin/bash

# 替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 替换homebrew-cask.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# 应用生效
brew update
# 替换homebrew-bottles:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

通过 brew config 命令查看配置信息:
在这里插入图片描述

7、还原默认镜像源

创建 restore_brew.sh 文件,写入如下内容,执行该文件。

#!/bin/bash

# 替换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-cask.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://github.com/Homebrew/homebrew-cask.git

# 应用生效
brew update

执行完 restore_brew.sh 文件后,将 ~/.bash_profile 文件中的 HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' 内容删除,并执行 source ~/.bash_profile,就恢复了默认的镜像。

8、常见的国内镜像

阿里巴巴开源镜像
清华镜像
中科大镜像

9、参考资料

homebrew github
brew github
homebrew-core github
homebrew-cask github
homebrew-bottles
Homebrew 的关键词定义

文章内容仅代表个人观点,如有不正之处,欢迎批评指正,谢谢大家。

发布了179 篇原创文章 · 获赞 296 · 访问量 164万+

猜你喜欢

转载自blog.csdn.net/claram/article/details/101577547