于飞
发布于 2026-03-17 / 20 阅读
0
0

Mac下Homebrew国内源配置指南

前言

Homebrew是macOS和Linux系统上不可或缺的包管理工具,让我们能够通过简单的命令安装、卸载、更新各种软件包。然而,由于网络原因,国内用户在安装和使用Homebrew时常常面临速度慢、连接失败的问题。本文将详细介绍如何将Homebrew切换为国内镜像源,让你的包管理体验更加流畅。

Homebrew简介

Homebrew主要由四个部分组成:

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

国内安装Homebrew

官方安装方法(可能较慢)

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

推荐:国内快速安装脚本

感谢网友分享的便捷安装脚本,一键完成安装:

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

执行脚本后,按提示输入密码授权,并选择镜像源(推荐选择中科大源,输入1即可)。

切换为国内源

中科大源

# 替换各个源
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# 配置bottles镜像(根据你的shell选择)
## Zsh用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

## Bash用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

# 刷新源
brew update

清华源

# 替换各个源
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
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

# 配置bottles镜像
## Zsh用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

## Bash用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

# 刷新源
brew update

阿里源

# 查看当前源
cd "$(brew --repo)" && git remote -v
cd "$(brew --repo homebrew/core)" && git remote -v

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

# 配置bottles镜像
## Zsh用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

## Bash用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

# 刷新源
brew update

重置为官方源

如果需要切换回官方源,执行以下操作:

# 重置各个源
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask

# 注释掉bottles镜像配置
## Zsh用户
vi ~/.zshrc
## 找到并注释掉 HOMEBREW_BOTTLE_DOMAIN 配置行
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx

## Bash用户
vi ~/.bash_profile
## 找到并注释掉 HOMEBREW_BOTTLE_DOMAIN 配置行
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx

# 刷新源
brew update

结语

配置国内源后,Homebrew的安装和更新速度将有明显提升。建议根据自己的网络情况选择合适的镜像源,中科大和清华源都是不错的选择。如果在配置过程中遇到问题,欢迎在评论区交流讨论。


评论