mac下镜像飞速安装Homebrew教程

5次阅读

共计 3186 个字符,预计需要花费 8 分钟才能阅读完成。

Homebrew是一款包管理工具,目前支持 macOSlinux系统。主要有四个部分组成: brewhomebrew-corehomebrew-caskhomebrew-bottles

名称 说明
brew Homebrew 源代码仓库
homebrew-core Homebrew 核心源
homebrew-cask 提供 macOS 应用和大型二进制文件的安装
homebrew-bottles 预编译二进制软件包

本文主要介绍 Homebrew 安装方式以及如何加速访问,顺便普及一些必要的知识。

1. 脚本说明

Homebrew默认安装脚本:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如果你等待一段时间之后遇到下面提示,就说明无法访问官方脚本地址:

curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out

请使用下面的脚本:

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

上面脚本中使用了中科大镜像来加速访问。

2. 执行命令

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

如果命令执行中卡在下面信息:

==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...

Command + C 中断脚本执行如下命令:

cd "$(brew --repo)/Library/Taps/"
mkdir homebrew && cd homebrew
git clone git://mirrors.ustc.edu.cn/homebrew-core.git

成功执行之后继续执行前文的安装命令。

最后看到 ==> Installation successful! 就说明安装成功了。

最最后执行:

brew update

卸载 Homebrew

使用官方脚本同样会遇到 uninstall 地址无法访问问题,可以替换为下面脚本:

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"

4. 设置镜像

brewhomebrew/core是必备项目,homebrew/caskhomebrew/bottles按需设置。

通过 brew config 命令查看配置信息。

4.1 中科大源

git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

git -C "$(brew --repo homebrew/cask)" 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' >> ~/.bash_profile
source ~/.bash_profile

注意 bottles 可以临时设置,在终端执行下面命令:

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

4.2 清华大学源

git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

brew update

# 长期替换 homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

4.3 恢复

git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

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

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

brew update

homebrew-bottles配置只能手动删除,将 ~/.bash_profile 文件中的 HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com内容删除,并执行 source ~/.bash_profile

5. 写在最后

在前面的过程中我们把 brewhomebrew-core的地址都指向到中科大镜像。

brew是在我提供的脚本中预设的。

#!/usr/bin/ruby
# This script installs to /usr/local only. To install elsewhere (which is
# unsupported) you can untar https://github.com/Homebrew/brew/tarball/master
# anywhere you like.
HOMEBREW_PREFIX = "/usr/local".freeze
HOMEBREW_REPOSITORY = "/usr/local/Homebrew".freeze
HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
# 这里替换了 BREW_REPO
BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git".freeze

homebrew-core是我们手动修改的,目前没找到比较好的办法预设 homebrew-corehomebrew-caskgit地址。

参考文章

  • 清华大学开源软件镜像站
  • 科大源

本文首发博客: mac 下镜像飞速安装 Homebrew 教程

正文完
 0