Homebrew 修改镜像源完全指南
2020年11月12日
预计阅读:4min
Homebrew 修改镜像源完全指南
为什么需要修改镜像源
Homebrew 是 macOS 上最流行的包管理工具,但使用默认源时,由于网络原因可能会导致下载缓慢或失败。修改为国内镜像源可以显著提升下载速度和稳定性。
镜像源分类
Homebrew 有几个主要的仓库需要考虑:
- brew - 主要的 Homebrew 程序
- homebrew-core - 核心软件包仓库
- homebrew-bottles - 预编译二进制软件包
- homebrew-cask - macOS 应用和大型二进制软件
修改镜像源步骤
1. 修改 brew 仓库源
# 进入brew安装目录
cd "$(brew --repo)"
# 查看当前源
git remote -v
# 替换为国内源
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git2. 修改 homebrew-core 仓库源
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git3. 修改 homebrew-cask 仓库源(如果使用)
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git4. 修改 homebrew-bottles 源
对于 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_profile5. 更新 Homebrew
# 更新brew
brew update可用的国内镜像源
中科大源(推荐)
- brew: https://mirrors.ustc.edu.cn/brew.git
- homebrew-core: https://mirrors.ustc.edu.cn/homebrew-core.git
- homebrew-cask: https://mirrors.ustc.edu.cn/homebrew-cask.git
- bottles: https://mirrors.ustc.edu.cn/homebrew-bottles
腾讯源
- brew: https://mirrors.cloud.tencent.com/homebrew/brew.git
- homebrew-core: https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git
- bottles: https://mirrors.cloud.tencent.com/homebrew-bottles
阿里源
- brew: https://mirrors.aliyun.com/homebrew/brew.git
- homebrew-core: https://mirrors.aliyun.com/homebrew/homebrew-core.git
- bottles: https://mirrors.aliyun.com/homebrew/homebrew-bottles
清华源
- brew: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
- homebrew-core: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
- bottles: https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
恢复官方源
如果需要恢复官方源,请执行以下命令:
# 重置brew源
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
# 重置homebrew-core源
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
# 重置homebrew-cask源(如果有)
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://github.com/Homebrew/homebrew-cask.git
# 移除HOMEBREW_BOTTLE_DOMAIN环境变量
# zsh用户
sed -i '' '/HOMEBREW_BOTTLE_DOMAIN/d' ~/.zshrc
source ~/.zshrc
# bash用户
sed -i '' '/HOMEBREW_BOTTLE_DOMAIN/d' ~/.bash_profile
source ~/.bash_profile常用 Homebrew 命令
# 搜索软件
brew search [软件名]
# 安装软件
brew install [软件名]
# 卸载软件
brew uninstall [软件名]
# 列出已安装的软件
brew list
# 更新所有软件
brew upgrade
# 清理旧版本和缓存
brew cleanup
# 诊断问题
brew doctor常见问题解决
连接超时问题
如果遇到连接超时,可以尝试更换其他镜像源或使用代理:
# 使用代理
export ALL_PROXY=socks5://127.0.0.1:1080更新失败问题
如果 brew update 失败,可以尝试:
# 重置更新
cd "$(brew --repo)"
git fetch --all
git reset --hard origin/master
brew update权限问题
如果遇到权限问题,可以检查 Homebrew 目录的所有权:
sudo chown -R $(whoami) $(brew --prefix)完成以上设置后,Homebrew 的下载和安装速度将会显著提升。定期执行 brew update 和 brew upgrade 保持软件包更新。