国内无障碍安装 Homebrew
pshu 码农英语课堂
MacOS 下的包管理工具Homebrew的安装原本轻松的把一行 shell 拷贝到终端就能搞定的事情,因为特殊原因安装起来就特别的慢,除非挂上梯子。
Pshu喜欢用命令行,自然喜欢用命令行运行梯子骑墙。弄了一台新电脑,没梯子,想装 homebrew,装 homebrew 需要梯子,梯子需要用命令行工具,命令行工具又需要homebrew。死锁!怎么办?
于是觉得记录下如何完全无梯子的情况下如何安装,若干年后可以让后人感受下前人是如何上网。
获取 install.sh
看 curl 访问的 url 路径知道是 github 托管的脚本,Homebrew 组织下的 install 项目。
用 bing 搜索了下 github 镜像,找个镜像站点 http://github-mirror.bugkiller.org/Homebrew/install, 对应的脚本的路径 http://github-mirror.bugkiller.org/Homebrew/install/blob/master/install.sh, 因为源文件下载还是要走 githubusercontent.com 。所以只能自己手动复制内容到本地,保存成 install.sh
简单分析下脚本
BREW_REPO="https://github.com/Homebrew/brew"
cd "${HOMEBREW_REPOSITORY}" >/dev/null || return
# we do it in four steps to avoid merge errors when reinstalling
execute "git" "init" "-q"
# "git remote add" will fail if the remote is defined in the global config
execute "git" "config" "remote.origin.url" "${BREW_REPO}"
execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"
# ensure we don't munge line endings on checkout
execute "git" "config" "core.autocrlf" "false"
execute "git" "fetch" "origin" "--force"
execute "git" "fetch" "origin" "--tags" "--force"
安装的过程需要从 github fetch https://github.com/Homebrew/brew 到 HOMEBREW_REPOSITORY 目录, 那就找个 homebrew 国内的 git 仓库镜像。
Homebrew 的 git 镜像
bing 搜索 Homebrew 镜像,清华大学有镜像。https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/ , brew 的 git 镜像地址是https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git , 替换 BREW_REPO 的地址。执行一把看看。
bash install.sh
home-brew-core 卡住了
==> Homebrew is run entirely by unpaid volunteers. Please consider do
https://github.com/Homebrew/brew#donations
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
但是我们的brew 已经是安装好了,我们看看再有什么方法绕过去。通过看上面最后一行日志,可以推测是在克隆 homebrew/homebrew-core 这个项目的时候失败了。在已经安装好的 /usr/local/Homebrew 里面搜一把。
$git grep -i github.com/homebrew/homebrew-core | grep md -v
Library/Homebrew/brew.sh: HOMEBREW_DEFAULT_CORE_GIT_REMOTE="https://github.com/Homebrew/homebrew-core"
打开 brew.sh 看看
if [[ -n "$HOMEBREW_MACOS" ]] || [[ -n "$HOMEBREW_FORCE_HOMEBREW_ON_LINUX" ]]
then
HOMEBREW_DEFAULT_CORE_GIT_REMOTE="https://github.com/Homebrew/homebrew-core"
else
HOMEBREW_DEFAULT_CORE_GIT_REMOTE="https://github.com/Homebrew/linuxbrew-core"
fi
if [[ -z "$HOMEBREW_CORE_GIT_REMOTE" ]]
then
HOMEBREW_CORE_GIT_REMOTE="$HOMEBREW_DEFAULT_CORE_GIT_REMOTE"
fi
export HOMEBREW_CORE_GIT_REMOTE
homebrew 会根据系统设置默认 homebrew-core 的 github 地址。如果有 HOMEBREW_CORE_GIT_REMOTE 的定义,那么就用这个定义的变量。好办,通过环境变量来修改这个 core 的地址,从刚才的清华的镜像站可以找到 core 镜像地址 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
HOMEBREW_CORE_GIT_REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git bash install.sh
再执行一把, brew 和 核心的 tap 就都安装好了
==> Downloading and installing Homebrew...
HEAD is now at db121c7b1 Merge pull request #7167 from MikeMcQuaid/linelength
HOMEBREW_CORE_GIT_REMOTE set: using https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git for Homebrew/core Git remote URL.
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 694690, done.
remote: Compressing objects: 100% (255505/255505), done.
remote: Total 694690 (delta 453826), reused 672279 (delta 436123)
Receiving objects: 100% (694690/694690), 221.00 MiB | 2.57 MiB/s, done.
Resolving deltas: 100% (453826/453826), done.
Checking out files: 100% (5169/5169), done.
Tapped 2 commands and 4931 formulae (5,193 files, 248.1MB).
Already up-to-date.
==> Installation successful!
当然还要记得设置下 Homebrew-bottles 的镜像地址,参考下这里:https://mirrors.tuna.tsinghua.edu.cn/help/homebrew-bottles/,下载一些预编译的会快不少。以后如果还要添加其他的 Homebrew Tap,可以参照清华大学的镜像配置。
无障碍的安装 Homebrew 至此结束,希望后人看到我们这么使用网络不会嘲笑我们。
PS:为了让 homebrew 的 install.sh 方便替换 BREW_REPO 的地址,我提了个 PR:https://github.com/Homebrew/install/pull/287,有兴趣的可以关注下。
欢迎转发,关注,赞赏!