前言

使用brew下载软件包、代码库等资源时总是会因为网络问题下载失败。因此更换brew镜像源为国内源会显著提升下载速度

国内源是指在国内建立的软件源或镜像站点,用于提供软件包、代码库等资源的下载和更新服务。国内源的建立旨在提高软件下载速度、降低网络延迟,并且可以更好地适应国内用户的需求。

在国内,有很多知名的软件源和镜像站点,例如:

  1. 清华大学开源软件镜像站(mirrors.tuna.tsinghua.edu)提供了包括Docker、Maven、RubyGems等众多开源软件的镜像下载服务,同时还提供了一些国内常用的Linux发行版的镜像下载。
  2. 阿里云开源镜像站(mirrors.aliyun.com):提供了包括Docker、Maven、RubyGems等众多开源软件的镜像下载服务,同时还提供了一些国内常用的Linux发行版的镜像下载。
  3. 中科大开源镜像站(mirrors.ustc.edu.cn):提供了包括Debian、Arch Linux、Rust等众多开源软件的镜像下载服务,同时还提供了一些国内常用的Linux发行版的镜像下载。

这些国内源都提供了稳定可靠的下载服务,并且在更新速度和网络延迟方面都有一定的优势。使用国内源可以加快软件的下载速度,提高开发效率。

⚠️:此片文章基于Mac操作系统



在终端中更改brew镜像源

1、环境准备

使用brew命令需要在Mac终端上安装好Homebrew软件包管理系统;安装命令如下

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

如果安装过程中较慢,可以使用下面这条命令,选择国内中科院镜像源下载

1
➜  ~ /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

2、查看当前源

1
2
3
4
# 查看 brew.git 当前源
➜ ~ cd "$(brew --repo)" && git remote -v
origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git (fetch)
origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git (push)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 查看 homebrew-core.git 当前源
➜ ~ cd "$(brew --repo homebrew/core)" && git remote -v
origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git (fetch)
origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git (push)

# 该命令执行如出现以下错误提示
cd: no such file or directory: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core

#解决方法如下
# 1、进入Taps文件夹
➜ ~ cd /usr/local/Homebrew/Library/Taps

# 2、创建homebrew文件夹,如果已有该文件夹忽略这一步
➜ ~ mkdir homebrew

# 3、进入homebrew
➜ ~ cd homebrew

# 4、查看homebrew中是否有homebrew-core
➜ ~ ls

# 5、如果有则删除
➜ ~ rm -rf homebrew-core

# 6、重新下载homebrew-core
➜ ~ git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

➜ ~ git clone https://github.com/Homebrew/homebrew-core.git
# 或重载homebrew-cask
➜ ~ git clone https://github.com/Homebrew/homebrew-cask.git

# 7、更新
➜ ~ brew update

或者

1
2
# ORIGIN指当前brew使用的源
➜ ~ brew config

3、更改brew镜像源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 替换 brew.git 为阿里源
➜ ~ git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

# 替换 homebrew-core.git 为阿里源
➜ ~ git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 替换homebrew-cask.git
➜ ~ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-cask.git

# zsh用户替换 brew bintray 镜像
➜ ~ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
➜ ~ source ~/.zshrc

# bash用户替换 brew bintray 镜像
➜ ~ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
➜ ~ source ~/.bash_profile

# 刷新源
➜ ~ brew update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 替换 brew.git 为清华源
➜ ~ git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 替换 homebrew-core.git 为清华源
➜ ~ git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 替换 homebrew-cask.git 为清华源
➜ ~ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

# zsh用户替换 brew bintray 镜像
➜ ~ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
➜ ~ source ~/.zshrc

# bash用户替换 brew bintray 镜像
➜ ~ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
➜ ~ source ~/.bash_profile

# 刷新源
➜ ~ brew update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 替换 brew.git 为中科大源
➜ ~ git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替换 homebrew-core.git 为中科大源
➜ ~ git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 替换 homebrew-cask.git 为中科大源
➜ ~ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# zsh用户替换 brew bintray 镜像
➜ ~ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
➜ ~ source ~/.zshrc

# bash用户替换 brew bintray 镜像
➜ ~ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
➜ ~ source ~/.bash_profile

# 刷新源
➜ ~ brew update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 重置 brew.git 为官方源
➜ ~ git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

# 重置 homebrew-core.git 为官方源
➜ ~ git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

# 重置 homebrew-cask.git 为官方源
➜ ~ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask

# zsh用户注释掉 HOMEBREW_BOTTLE_DOMAIN 配置
➜ ~ vi ~/.zshrc
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx

# bash用户注释掉 HOMEBREW_BOTTLE_DOMAIN 配置
➜ ~ vi ~/.bash_profile
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx

# 刷新源
➜ ~ brew update

4、更新错误

如果执行 brew update 出现以下错误提示

1
2
3
4
fatal: unable to access 'https://mirrors.aliyun.com/homebrew/brew.git/': Failed to connect to 127.0.0.1 port 31181 after 1 ms: Couldn't connect to server
fatal: unable to access 'https://mirrors.aliyun.com/homebrew/homebrew-cask.git/': Failed to connect to 127.0.0.1 port 31181 after 0 ms: Couldn't connect to server
fatal: unable to access 'https://mirrors.aliyun.com/homebrew/homebrew-core.git/': Failed to connect to 127.0.0.1 port 31181 after 0 ms: Couldn't connect to server
fatal: unable to access 'https://gitee.com/cunkai/homebrew-services.git/': Failed to connect to 127.0.0.1 port 31181 after 0 ms: Couldn't connect to server

则取消git代理,取消gitconfig中的http.proxy=127.0.0.1:1080、https.proxy=127.0.0.1:1080

1
2
➜  ~ git config --global --unset http.proxy  
➜ ~ git config --global --unset https.proxy

然后再执行 brew update 即可

1
2
3
4
5
6
➜  homebrew-cask git:(master) brew update
Installing from the API is now the default behaviour!
You can save space and time by running:
brew untap homebrew/core
brew untap homebrew/cask
Already up-to-date.

不过我执行 brew update 命令后系统建议我删除cask跟core
很奇怪,但我还是执行了系统建议
执行了系统建议之后,只需要替换 brew.git 为其它源即可,不需要再更换homebrew-core.git homebrew-cask.git

5、执行更新

替换源后通常会出现如下提示,提醒你更新当前软件包

1
2
3
You have 8 outdated formulae installed.
You can upgrade them with brew upgrade
or list them with brew outdated.

只需要执行以下命令即可,也可以不执行,但最好还是执行更新

1
2
# 更新所有软件包
➜ ~ brew upgrade





借鉴链接

Mac 的brew 换源
macOS中 更换Homebrew镜像源
HomeBrew和HomeBrew Cask的安装和使用
更多关于brew的知识持续更新中!!!



声明

借鉴部分均注明了原文出处,可在文章的`借鉴链接`处获取原文出处
文中若内容有涉及原版权,请邮件联系elvin-chen@qq.com,涉及的相关文章或内容将会及时更改或取消发布