Homebrew
是一款包管理工具,目前支持macOS
和linux
系统。主要有四个部分组成: brew
、homebrew-core
、homebrew-cask
、homebrew-bottles
。
本文主要介绍Homebrew
安装方式以及如何加速访问,顺便普及一些必要的知识。
如果你是在M1
芯片的Mac
上安装Homebrew
,可以先阅读下这篇文章:
如果安装过程中遇到问题,请遵循下面的沟通方式:
- GitHub Issue(推荐)
- 评论留言
- 私信
你可以点此 homebrew-install 创建issue
,这也是最及时、最标准的反馈方式。
本教程拥有自己的主页啦,你可以获得更好的阅读体验,速度点击下面卡片查看:
快速安装 Homebrew1、写在前面
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out
使用官方脚本遇到上面的错误提示:请按照下面「安装步骤」步骤执行。
官方脚本无法使用的原因是ineo6:GitHub加速指南进阶版ineo6:解决GitHub网页githubusercontent地址无法访问问题raw.githubusercontent.com
访问很不稳定,也可以采用写入hosts
的方式,可以一定程度解决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!
就说明安装成功了。
最最后更新下:
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
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/bottles' >> ~/.zprofile
source ~/.zprofile
如果是macOS Mojave
及更低版本,并且没有自己配置过zsh
,对应文件则是.bash_profile
:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile
注意:上述区别仅仅是.zprofile
和.bash_profile
不同,文章如有再次提及编辑.zprofile
,均按此方法替换。
如果想使用清华源:
把
https://mirrors.ustc.edu.cn/homebrew-bottles/bottles
替换为
https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/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 反馈问题
- GitHub Issue(推荐)
- 评论留言
- 私信
你可以点此 homebrew-install 创建issue
,这也是最及时、最标准的反馈方式。
5、总结
在前面的过程中我们把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"
参考文章
广告插播
GitMaster
可以树形展示git
项目代码。
- 代码树支持
GitHub
、GitLab
、Gitee
- ️ 支持私有部署页面,一键标记
- ️ 文件提交历史可视化
git history
-
GitHub
通知提醒功能 - ⬇️ 支持
GitHub
文件、目录下载

商店安装
FAQ
大部分问题都是资源不稳定导致的,如果可以的话尽量通过代理加速。
代理使用可以参考以下文章:
ineo6:终端使用代理加速的正确方式最新版问题排查内容请访问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>...]'
解决方案:
- 根据文章解决GitHub网页githubusercontent地址无法访问问题,给
github.com
域名加速 - 通过代理访问
设置镜像无效,安装还是很慢
这里说的安装很慢主要是指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)"
SHA256 mismatch
Error: An exception occurred within a child process:
ChecksumMismatchError: SHA256 mismatch
Expected: 01059532335fefc5e0e7a23cc79eeb1dc6fea477606981b89f259aa0e0f9abc1
Actual: 641ba394c891cee16dfa45873906edab12edfc9befaa121649bb18e7b7d574bf
Archive: /Users/XXX/Library/Caches/Homebrew/downloads/1124b500fc261f6d9be6193eca8c120f4dfb7ecd3470ac7d8edc1d04ecc5b345--jdk13u-jdk-13.0.2+8.tar.bz2
To retry an incomplete download, remove the file above.
遇到该问题时请尝试该方案:https://blog.csdn.net/lemostic/
6、安装python3
首先我们先总结一下homebrew中常用的命令:
1.查询所有包
brew services list
2. 启动包(以activemq为例子)
brew services start activemq
3.停止包
brew services stop activemq
4.卸载包
brew uninstall activemq
5.安装包
brew install activemq
6.查看包具体信息
brew info activemq
7. 查看更新 brew
brew update
8.更新指定包
brew upgrade activemq
9.更新所有包
brew upgarde
10.清理所有包的旧版本
brew cleanup
11.清理指定包的旧版本
brew cleanup activemq
12.查看可清理的旧版本包,不执行实际操作
brew cleanup -n
13.锁定不想更新的包
brew pin $FORMULA
14.取消锁定
brew unpin $FORMULA
15、mac brew安装的文件放在哪里
brew list redis
Mac使用homebrew安装python3
安装命令 : brew install python3
安装完成之后需要修改环境变量
python不同安装方式的默认安装路径如下:
控制台输入:vi ~/.bash_profile 并输入如下代码:
首先需要在第一行插入如下代码(用于覆盖本地的python2):
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
然后插入:
export PATH=$PATH:/usr/local/Cellar/python@3.9/3.9.4
:wq(退出)
刷新环境变量
source ~/.bash_profile
到此输入python -version就可以查看Python的版本内容了
注意python3安装各种包的时候需要使用pip3命令,例如
pip3 install beautifulsoup4
使用homebrew安装python时安装的包的默认位置在
/usr/local/lib/python3.9/site-packages
注意:如果安装了新的python,macos默认的python2将会被覆盖掉。
全部评论