Homebrew切换为国内镜像源

1、brew.git 镜像

中科大镜像:https://mirrors.ustc.edu.cn/brew.git
 
阿里镜像:  https://mirrors.aliyun.com/homebrew/brew.git
 
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
 
GitHub镜像:https://github.com/Homebrew/brew.git

2、homebrew-core.git 镜像

中科大镜像:https://mirrors.ustc.edu.cn/homebrew-core.git
 
阿里镜像:  https://mirrors.aliyun.com/homebrew/homebrew-core.git
 
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
 
GitHub镜像:https://github.com/Homebrew/homebrew-core

3、homebrew-cask.git 镜像

# 中科大镜像
https://mirrors.ustc.edu.cn/homebrew-cask.git
# 阿里暂无该镜像
 
# github镜像
https://github.com/Homebrew/homebrew-cask.git

一、切换 Homebrew 镜像源为中科大镜像源

1、替换源

以中科大镜像为例

# 替换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' >> ~/.zshrc
source ~/.zshrc

2. 查看镜像源信息

brew config

二、切换Homebrew 镜像源为阿里镜像源

1. 更换为阿里源

    # 替换brew.git:
    cd "$(brew --repo)"
    git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
    # 替换homebrew-core.git:
    cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
    git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
    # 应用生效
    brew update
    # 替换homebrew-bottles:
    echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc

2. 查看镜像源信息

brew config

三、重置 Homebrew 镜像源

1. 切回官方原始镜像(GitHub)

# 替换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
 
# 删除.zshrc变量
vim  ~/.zshrc
# 删除如下变量
# export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
 
# 执行更新
source ~/.zshrc

2.  查看镜像源信息

brew config

四、非常规操作及常见问题解决
一.换源安装homebrew
方法一:
1.卸载
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
2.安装
方法一:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
方法二:
把官网给的脚本拿下来 
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install
更改脚本中的资源链接,替换成中国科学技术大学的镜像:
BREW_REPO = “https://github.com/Homebrew/brew“.freeze 
CORE_TAP_REPO = “https://github.com/Homebrew/homebrew-core“.freeze 
更改为这两句 
BREW_REPO = “https://mirrors.ustc.edu.cn/brew.git “.freeze 
CORE_TAP_REPO = “https://mirrors.ustc.edu.cn/homebrew-core.git“.freeze 
执行脚本
/usr/bin/ruby brew_install


二. 安装常见问题, ping https://github.com试试。


1.替换成国内源,参考如上步骤,建议修改为阿里的源

2. 踩坑
2.1
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
fatal: unable to access 'https://github.com/Homebrew/homebrew-core/': transfer closed with outstanding read data remaining
Error: Failure while executing; `git clone https://github.com/Homebrew/homebrew-core /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1` exited with 128.
Error: Failure while executing; `/usr/local/bin/brew tap homebrew/core` exited with 1.
Failed during: /usr/local/bin/brew update --force
分析:
网络问题,手工下载。
创建目录:
cd "$(brew --repo)"
cd Library/Taps/
mkdir homebrew
cd homebrew/
mkdir homebrew-core
cd homebrew-core/
开始clone
git clone git://mirrors.ustc.edu.cn/homebrew-core.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core


2.2
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
-bash: cd: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask: No such file or directory
$ brew cask install atom
==> Tapping homebrew/cask
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'…
fatal: unable to access 'https://github.com/Homebrew/homebrew-cask/': transfer closed with outstanding read data remaining
Error: Failure while executing; `git clone https://github.com/Homebrew/homebrew-cask /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask —depth=1` exited with 128.
分析:
故障原因和上面一样,处理方式也一样就可以了。
创建目录:
mkdir -p /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask
cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask
开始clone
git clone https://mirrors.ustc.edu.cn/homebrew-cask.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask

三. 常用命令
brew search <package_name> # 搜索
brew install <package_name> # 安装一个软件
brew uninstall <package_name> # 卸载一个软件
brew update # 从服务器上拉取,并更新本地 brew 的包目录
brew upgrade <package_name> # 更新一个软件
brew upgrade # 更新全部已安装软件
brew outdated # 查看你的软件中哪些有新版本可用
brew cleanup # 清理老版本。使用 `-n` 参数,不会真正执行,只是打印出真正运行时会做什么。
brew list --versions # 查看你安装过的包列表(包括版本号)
brew link <package_name> # 将软件的当前最新版本软链到`/usr/local`目录下
brew unlink <package_name> # 将软件在`/usr/local`目录下的软链接删除。
brew info # 显示软件的信息 
brew deps # 显示包依赖
brew doctor # 检查brew软件的安装是否正常
brew prune # 移除链接失效的文件
brew --cache # 打印下载软件的缓存路径
brew --config # 打印brew的配置信息
brew --env # 打印brew的环境信息
brew --prefix # 打印prefix的路径(通常是/usr/local/)
brew --prefix [formula] # 指出formula安装位置

原文地址:https://www.cnblogs.com/anly95/p/14728311.html