Mac Homebrew安装与源替换

安装

1.官网地址安装

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

2.使用国内源安装

1
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" #推荐使用
1
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"

Homebrew 命令

  • brew install [包名]
  • brew update
  • brew update -verbose
  • brew cleanup:清理旧有软件包.
  • brew doctor:诊断Homebrew的问题.
  • brew update-reset

操作步骤

以阿里云源为例,查看当前源地址

1
cd "$(brew --repo)" && git remote -v

更换 homebrew-bottles

这与你当前 macOS 系统使用的 shell 版本有关系,执行以下命令查看 Shell版本:Mac OS在10.15系统开始,默认的shell都换成了zsh

1
2
atx:Homebrew atx$ echo $SHELL
/bin/bash

/bin/bash 结果

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

/bin/zsh 结果

1
2
3
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc

source ~/.zshrc

中科大源

1
2
3
4
5
# 对于 bash 用户
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"' >> ~/.bash_profile

# 对于 zsh 用户
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"' >> ~/.zshrc

替换homebrew-core

1
2
3
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

中科大

1
2
3
4
5
# 对于 bash 用户
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.ustc.edu.cn/homebrew-core.git"' >> ~/.bash_profile

# 对于 zsh 用户
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.ustc.edu.cn/homebrew-core.git"' >> ~/.zshrc

替换homebrew-cask源

1
2
$ cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
$ git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-cask.git

中科大使用如下命令

1
2
brew tap --custom-remote --force-auto-update homebrew/cask https://mirrors.ustc.edu.cn/homebrew-cask.git

如果进入该目录提示不存在该目录,则直接手动创建即可

1
2
3
4
cd: no such file or directory: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask

mkdir -p /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask

4.更新brew

1
brew update 

重置为官方

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

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core

cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
git remote set-url origin https://github.com/Homebrew/homebrew-cask

卸载

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

踩坑

1.brew update 报错 “fatal: Could not resolve HEAD to a revision”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mgtv@baiwei-MacBook-Pro homebrew-cask % brew update -verbose
Checking if we need to fetch /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
error: Not a valid ref: refs/remotes/origin/master
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
Fetching /usr/local/Homebrew...
Updating /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
fatal: Could not resolve HEAD to a revision

Usage: brew update-report [--preinstall] [--force]

The Ruby implementation of brew update. Never called manually.

--preinstall Run in 'auto-update' mode (faster, less
output).
-f, --force Treat installed and updated formulae as if
they are from the same taps and migrate
them anyway.
-d, --debug Display any debugging information.
-q, --quiet Make some output more quiet.
-v, --verbose Make some output more verbose.
-h, --help Show this message.
Error: invalid option: -erbose

解决方案

  1. 进入报错路径
1
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

执行如下命令:

1
2
3
git fetch --prune origin
git pull --rebase origin master
brew update

brew update卡住

1
2
brew update-reset
brew update
1
fatal: unable to access 'https://github.com/Homebrew/homebrew-core.git/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 

解决方案

准备好vpn配置如下

1
2
git config --global http.proxy socks5://127.0.0.1:1080
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080

网络代理socket设置127.0.0.1:1080

参考资料