mac下镜像飞速安装Homebrew教程
Homebrew
是一款包管理工具,目前支持macOS
和linux
系统。主要有四个部分组成: brew
、homebrew-core
、homebrew-cask
、homebrew-bottles
。
名称 | 说明 |
---|---|
brew | Homebrew 源代码仓库 |
homebrew-core | Homebrew 核心源 |
homebrew-cask | 提供 macOS 应用和大型二进制文件的安装 |
homebrew-bottles | 预编译二进制软件包 |
本文主要介绍Homebrew
安装方式以及如何加速访问,顺便普及一些必要的知识。
快速导航:
1. 脚本说明
2020年12月14日更新:升级到shell脚本安装方式。
Homebrew
默认安装脚本:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
如果你等待一段时间之后遇到下面提示,就说明无法访问官方脚本地址:
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out
请按照下面 安装说明 步骤执行。
安装使用到的脚本源码在此 homebrew-install 。
官方脚本无法使用的原因是
raw.githubusercontent.com
访问很不稳定,我上面提供的方案里是采用了jsdelivr CDN
加速访问。另外也可以采用写入
hosts
的方式,可以一定程度解决GitHub
资源无法访问的问题,我也写了一篇操作文章,有需要可以阅读下。
解决GitHub网页githubusercontent地址无法访问问题
2. 安装说明
提前设置homebrew-core
镜像源并执行:
# 中科大
HOMEBREW_CORE_GIT_REMOTE=https://mirrors.ustc.edu.cn/homebrew-core.git
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
如果命令执行中卡在下面信息(如提示有差异,请反馈给我):
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
请Control + C
中断脚本执行如下命令:
cd "$(brew --repo)/Library/Taps/"
mkdir homebrew && cd homebrew
git clone git://mirrors.ustc.edu.cn/homebrew-core.git
cask
同样也有安装失败或者卡住的问题,解决方法也是一样:
cd "$(brew --repo)/Library/Taps/"
cd homebrew
git clone https://mirrors.ustc.edu.cn/homebrew-cask.git
成功执行之后继续执行前文的安装命令:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
最后看到==> Installation successful!
就说明安装成功了。
最最后更新下:
brew update
3. 设置镜像
brew
、homebrew/core
是必备项目,homebrew/cask
、homebrew/bottles
按需设置。
通过 brew config
命令可以查看相关配置信息。
3.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/bottles/' >> ~/.bash_profile
source ~/.bash_profile
注意bottles
可以临时设置,在终端执行下面命令:
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/
3.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/bottles/' >> ~/.bash_profile
source ~/.bash_profile
3.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. 其他相关
5.1 cask
目前cask
是从GitHub
上读取软件源,而GitHub Api
对访问有限制,如果使用比较频繁的话,可以申请Api Token
,然后在环境变量中配置到HOMEBREW_GITHUB_API_TOKEN
。
在.bash_profile
中追加:
export HOMEBREW_GITHUB_API_TOKEN=yourtoken
注意:因为cask
是从GitHub
下载软件,所以目前是无法加速的。
5.2 如何卸载Homebrew
使用官方脚本同样会遇到uninstall
地址无法访问问题,可以使用下面脚本:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"
5.3 反馈问题
6. 总结
在前面的过程中我们把brew
和homebrew-core
的地址都指向到中科大镜像。
原理是通过修改install.sh
脚本,在里面预设镜像地址。
STAT="stat --printf"
CHOWN="/bin/chown"
CHGRP="/bin/chgrp"
GROUP="$(id -gn)"
TOUCH="/bin/touch"
fi
# 这里替换了BREW_REPO
BREW_REPO="https://mirrors.ustc.edu.cn/brew.git"
最后不完美的地方是我们只能预设brew
镜像,没找到比较好的办法预设homebrew-core
、homebrew-cask
的git
地址。
更新后的方案使用HOMEBREW_CORE_GIT_REMOTE
来预设homebrew-core
源,不过这个设置方法很可能无效,欢迎小伙伴留言反馈你的效果。
如果对您有帮助
点赞、分享、收藏,能让更多人能发现文章,这也是对我的认可和鼓励,谢谢。
参考文章
📣 广告插播
GitMaster
可以树形展示git
项目代码。
- 🚀 代码树支持
GitHub
、GitLab
、Gitee
- 🖊️ 支持私有部署页面,一键标记
- 🗂️ 文件提交历史可视化
git history
- 🔔
GitHub
通知提醒功能 - ⬇️ 支持
GitHub
文件、目录下载
安装
商店安装
本文首发博客: mac下镜像飞速安装Homebrew教程
欢迎扫描二维码关注,回复 hb 即可加入Homebrew
沟通群。
湖中剑
NVM 快速安装教程
ineo6阅读 803
工具篇:iTerm与Zsh
super白赞 4阅读 4.7k
mac下git命令自动补全
zorro阅读 5k
前端怎么配置刚到手的mac和常用的快捷键
HappyCodingTop阅读 2k
macOS Monterey 12.2 (21D49) 正式版 ISO、IPSW、PKG 下载
sysin阅读 1.6k
macOS Monterey 12.2 (21D49) Boot ISO 原版可引导镜像
sysin阅读 1.3k
macOS Monterey 12.2.1 (21D62) 正式版 ISO、IPSW、PKG 下载
sysin阅读 965
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。