解决国内Homebrew安装太慢和失败问题

知识库 Mar 01, 2020

Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。

本文主要解决问题:Homebrew常规安装太慢;以及通过brew install安装软件太慢,还有时不时的自动updating巨耗时的问题。

背景

首先官网:https://brew.sh/index_zh-cn

官网安装命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
官网卸载命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
国内官网安装基本很慢,速度不忍直视,5KB/s......;这速度怎么对得起科学上网?

一开始大概是这个样子:

==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
Press RETURN to continue or any other key to abort
==> Downloading and installing Homebrew...

大部分情况是安装了一会就报错,然后是这个样子:

curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
抓狂😫……

那我们就不能愉快地安装HomeBrew了吗?(试了一下,卸载倒是挺快的……WTF???)

简单五步解决方案,搞定HomeBrew安装

第一步:创建HomeBrew文件夹

首先确保/usr/local/Homebrew文件夹不存在,存在的话删除,然后执行:

sudo mkdir /usr/local/Homebrew

第二步:git克隆

sudo git clone https://mirrors.ustc.edu.cn/brew.git /usr/local/Homebrew
或者
sudo git clone https://mirrors.aliyun.com/homebrew/brew.git /usr/local/Homebrew
或者
sudo git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git /usr/local/Homebrew
回车后,会提示Receiving objects: xx% 等待下载完成。

Cloning into '/usr/local/Homebrew'...
remote: Counting objects: 132526, done.
remote: Total 132526 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (132526/132526), 32.16 MiB | 1.09 MiB/s, done.
Resolving deltas: 100% (97548/97548), done.

第三步:创建一个快捷方式到/usr/local/bin目录

sudo ln -s /usr/local/Homebrew/bin/brew /usr/local/bin/brew
如果提示File exists表示/usr/local/bin文件夹里面已经有brew,删除后再运行第三步。

第四步:创建core文件夹 并 再次git克隆

sudo mkdir -p /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
以及

sudo git clone https://mirrors.ustc.edu.cn/homebrew-core.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
或者
sudo git clone https://mirrors.aliyun.com/homebrew/homebrew-core.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
或者
sudo git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
完成后有如下信息输出:

Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 688626, done.
remote: Total 688626 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (688626/688626), 223.64 MiB | 6.83 MiB/s, done.
Resolving deltas: 100% (455339/455339), done.

第五步:获取权限 并 运行更新

sudo chown -R $(whoami) /usr/local/Homebrew
以及

brew update
稍等一会儿~大功告成!

最后设置:

设置环境变量,再运行下面两句后,重启终端:(命令中的链接地址可以替换为第二步或者第四步中对应的链接地址)

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile


如果有问题自检试试:

brew有一个自检程序,

brew doctor
查看全部安装路径

brew list
查看指定软件安装路径

brew list 软件名
另外如果已经用官网的命令成功安装好了Homebrew的童鞋(好吧果然你们有耐心。。。),可以通过替换镜像源来解决安装软件慢以及更新慢的问题:

当然如果是通过本文介绍的安装方法是不用替换的

替换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

如果想要重置回默认的源:

重置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安装主要靠git仓库,切换源实际上就是切换相关git repo的remote url.

具体做法:

  • 找到brew.githomebrew-core.git的本地repo
  • 切换两个repo的remote url

切换USTC源:

https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git

# 更换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

brew update

另外,还需要更换Homebrew Bottle源,这个只要在shell配置文件里加上(或更改)一个变量即可。
如zsh就是编辑~/.zshrc文件, bash就是~/.bash_profile文件:

export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles

切换清华源:

https://mirror.tuna.tsinghua.edu.cn/help/homebrew/

cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-science"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-science.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-python"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-python.git

brew update

重置官方源:

# 重置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