Homebrew 设置为阿里源

brew 源极慢,有必要把 Homebrew 源改为成国内源。

Homebrew 源主要有四个部分组成:

名称 说明
brew Homebrew 源代码仓库
homebrew-core Homebrew 核心软件仓库
homebrew-bottles Homebrew 预编译二进制软件包
homebrew-cask 提供 macOS 应用和大型二进制文件

安装 brew

如果还没安装 homebrew,则需要先安装。

用官网推荐安装方式:

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

如果安装很慢,可使用清华源安装

1
2
3
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

查看当前源

1
2
3
4
5
6
7
8
9
$ cd "$(brew --repo)" && git remote -v
origin https://github.com/Homebrew/brew.git (fetch)
origin https://github.com/Homebrew/brew.git (push)

# 查看 homebrew-core.git 当前源
$ cd "$(brew --repo homebrew/core)" && git remote -v
origin https://github.com/Homebrew/homebrew-core.git (fetch)
origin https://github.com/Homebrew/homebrew-core.git (push)

改为阿里源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 修改 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

# 如果 ~/.bash_profile 或 ~/.zshrc 不存在,可创建后在 `/etc/zshrc` 加入 `source ~/.bash_profile` 或 `source ~/.zshrc`
# bash 替换 brew 二进制包、API 镜像变量到 ~/.bash_profile,也可以加到 ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
echo 'export HOMEBREW_API_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles/api' >> ~/.bash_profile

source ~/.bash_profile # zsh 执行 source ~/.zshrc

# 更新源
$ brew update

# 清理旧包
$ brew cleanup

你也可以试用清华源

1
2
3
4
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
echo 'export HOMEBREW_API_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api' >> ~/.bash_profile

改回官方源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 重置 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


# bash 注释掉 HOMEBREW_BOTTLE_DOMAIN、HOMEBREW_API_DOMAIN 配置
$ vim ~/.bash_profile
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx
# export HOMEBREW_API_DOMAIN=xxxx

参考:《Mac 下 brew 切换为国内源》

作者

CoderPan

发布于

2023-02-20

更新于

2024-05-08

许可协议

评论