mac下镜像飞速安装Homebrew教程

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

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

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

如果你需要在M1芯片的Mac上安装Homebrew,可以先阅读下这篇文章:

M1芯片Mac上Homebrew安装教程

如果安装过程中遇到问题,可以通过下面方式获得联系:

本教程拥有自己的主页啦,你可以获得更好的阅读体验,赶紧点此访问吧。

1. 写在前面

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

使用官方脚本遇到上面的错误提示:请按照下面「安装步骤」步骤执行。

官方脚本无法使用的原因是raw.githubusercontent.com访问很不稳定,可以采用写入hosts的方式,可以一定程度解决GitHub资源无法访问的问题,我也写了一篇操作文章,有需要可以阅读下。

GitHub加速指南进阶版

2. 安装步骤

默认使用中科大源,如果需要更换,请使用 镜像助手 获取执行脚本。

安装脚本:

/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!就说明安装成功了。

需要注意,如果是m1则需要手动设置环境变量,请注意看终端输出的中文提示信息,会以下面文字开头。

"切记执行环境变量设置!,如已执行过请忽略。"

最后更新下:

brew update

🎉 恭喜Homebrew安装完成了!

3. 如何设置镜像

必读!!!

这个章节主要针对已经安装过Homebrew,但是需要设置镜像的情况。

如果是全新安装,只需要设置3.3的bottles镜像。

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

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

更多可选源请访问镜像助手

3.3 设置bottles镜像

设置环境变量需要注意终端Shell的类型,请看下面说明:

镜像以中科大源为例。

macOS Catalina(10.15.x) 版开始,Mac使用zsh作为默认Shell,对应文件是.zprofile,所以命令为:

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

如果是macOS Mojave 及更低版本,并且没有自己配置过zsh,对应文件则是.bash_profile

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

注意:上述区别仅仅是.zprofile.bash_profile不同,文章如有再次提及编辑.zprofile,均按此方法替换。

如果想使用清华源:

把
https://mirrors.ustc.edu.cn/homebrew-bottles

替换为
https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles

至此,安装和设置操作都完成了。

3.4 如何恢复默认源

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配置只能手动删除,将 ~/.zprofile 文件中的 HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com内容删除,并执行 source ~/.zprofile

4. 其他相关

4.1 cask

目前cask是从GitHub上读取软件源,而GitHub Api对访问有限制,如果使用比较频繁的话,可以申请Api Token,然后在环境变量中配置到HOMEBREW_GITHUB_API_TOKEN

.zprofile中追加,注意替换yourtoken:

echo 'export HOMEBREW_GITHUB_API_TOKEN=yourtoken' >> ~/.zprofile
source ~/.zprofile

注意:因为cask是从GitHub下载软件,所以目前是无法加速的。

4.2 如何卸载Homebrew

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

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"

4.3 反馈问题

5. 总结

在前面的过程中我们把brewhomebrew-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"

如果对您有帮助

点赞、分享、收藏,能让更多人能发现文章,这也是对我的认可和鼓励,谢谢。

参考文章

📣 广告插播

GitMaster可以树形展示git项目代码。

  • 🚀 代码树支持GitHubGitLabGiteeGiteaGogs
  • 🖊️ 支持私有部署页面,一键标记
  • 🗂️ 文件提交历史可视化 git history
  • 🔔 GitHub通知提醒功能
  • ⬇️ 支持GitHub文件、目录下载

GitMaster

安装

商店安装

FAQ

大部分问题都是资源不稳定导致的,如果可以的话尽量通过代理加速。

代理使用可以参考以下文章:

完整版问题排查内容请访问FAQ,以便获得强有力的支持。

No.1 的小秘籍

遇到解决不了的问题,先删除homebrew目录再重新运行脚本安装。

  • x86 上安装目录: /usr/local/Homebrew/
  • arm 上安装目录: /opt/homebrew

fatal: unable to access xxx

HEAD is now at 028e733e7 Merge pull request #10260 from reitermarkus/audit-livecheck

fatal: unable to access 'https://github.com/Homebrew/homebrew-core/': transfer closed with outstanding read data remaining
fatal: ambiguous argument 'refs/remotes/origin/master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

解决方案:

设置镜像无效,安装还是很慢

这里说的安装很慢主要是指brew本身安装速度慢(镜像地址没有设置成功)。

可以执行命令echo $SHELL,先查看终端类型。

  • bash: /bin/bash
  • zsh: /bin/zsh

然后根据终端类型设置镜像,参考"设置镜像"。

command not found: brew

一般是环境变量无效,请使用echo $SHELL确认终端类型,参考上节。

m1芯片Mac电脑需要手动设置环境变量:

zsh

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

bash

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"

m1的话,可以尝试手动加入环境变量:

zsh

echo 'eval "$(/usr/local/Homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/usr/local/Homebrew/bin/brew shellenv)"

bash

echo 'eval "$(/usr/local/Homebrew/bin/brew shellenv)"' >> ~/.bash_profile
eval "$(/usr/local/Homebrew/bin/brew shellenv)"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值