只要掌握正确的方法,在 Mac 上安装 Homebrew 并非难事。
Homebrew 是什么
Homebrew 作为 macOS 系统的包管理器,允许用户通过简单的命令行指令来下载和安装所需的软件包,从而免去手动下载、解压及拖拽安装的繁琐过程。例如,若要安装 Nginx 服务器,只需在终端运行特定命令即可完成部署。
brew install nginx
Homebrew 官方文档地址为 https://brew.sh/
Homebrew 怎么安装
方法一:使用 brew 官网提供的安装脚本
执行以下命令进行安装:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
在执行过程中,用户可能会遇到两种情况:一是下载速度极慢,二是直接出现连接失败的提示。
错误提示通常为:curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
产生该问题的原因是 http://raw.githubusercontent.com 的访问状态不稳定。
此时建议采用另一种更高效科学的安装方式,即下文介绍的方法二。
方法二:使用 brew 镜像安装脚本(亲测最快速有效)
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
该脚本利用中科大镜像服务加速访问,仅修改了仓库地址,不会带来任何安全隐患。关于中科大提供的 Homebrew 镜像服务详情,可参考 https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git
注意观察安装时的下载速度,几百 KiB/s 或几 MB/s 属于正常范围。如果速度仅为几 KiB/s,通常意味着该方法无效,往往在加载到百分之几十时就会报错中断。

Homebrew 安装时如何避坑
1. 若出现 Error: Checksum mismatch.
具体的报错代码如下:
curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54 Error: Checksum mismatch. Expected: b065e5e3783954f3e65d8d3a6377ca51649bfcfa21b356b0dd70490f74c6bd86 Actual: e8a348fe5d5c2b966bab84052062f0317944122dea5fdfdc84ac6d0bd513c137 Archive: /Users/joyce/Library/Caches/Homebrew/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz To retry an incomplete download, remove the file above. Error: Failed to install Homebrew Portable Ruby (and your system version is too old)! Failed during: /usr/local/bin/brew update --force
此错误由 Homebrew 目录下的 portable-ruby-2.6.3_2.yosemite.bottle.tar.gz 文件引起,导致安装中断。解决方法是进入上述路径删除该文件,然后重新执行安装命令即可。
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
2. 若卡在了 Cloning into...

根据此处极慢的下载速度可以判断进程已卡住,应立即使用 Control + C 中断脚本,随后执行以下命令:
cd "$(brew --repo)/Library/Taps/"
mkdir homebrew && cd homebrew
git clone git://mirrors.ustc.edu.cn/homebrew-core.git
执行后可看到:

此时速度会显著提升,能够迅速完成安装。
注:当最后出现 Installation successful! 或者 Checking out files: 100% (5392/5392), done. 时,说明安装已成功。
Homebrew 安装完为何需要配置
前文提到,Homebrew 主要用于下载软件,但其默认安装速度较慢。为了提升效率,需要更改 Homebrew 的安装源,将其替换为国内镜像。
这里使用的是由中科大负责托管维护的 Homebrew 镜像。其中,前两项为必须配置的项目,后两项可根据实际需求选择是否配置。
1. 必备设置:替换 brew.git:
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
替换 homebrew-core.git:
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
2. 按需设置:替换 homebrew-cask.git:
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
替换 homebrew-bottles:
首先需要区分你的 Mac 使用的是哪种终端工具。如果是 bash,则执行:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
若是 zsh,则执行:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
注:Homebrew 主要由四个部分组成:brew、homebrew-core、homebrew-cask、homebrew-bottles,它们对应的功能如下:
| 组成 | 功能 |
|---|---|
| Homebrew | 源代码仓库 |
| homebrew-core | Homebrew 核心源 |
| homebrew-cask | 提供macos应用和大型二进制文件的安装 |
| homebrew-bottles | 预编译二进制软件包 |
Homebrew 基本用法有哪些
// 查询:
brew search 软件名
// 安装:
brew install 软件名
// 卸载:
brew uninstall 软件名
// 更新 Homebrew:
brew update
// 查看 Homebrew 配置信息:
brew config
// 查看 Homebrew 安装软件的具体路径:
brew list 软件名
#例如:
brew list ant
/usr/local/Cellar/ant/1.10.9/bin/ant
/usr/local/Cellar/ant/1.10.9/bin/antRun
/usr/local/Cellar/ant/1.10.9/bin/antRun.pl
/usr/local/Cellar/ant/1.10.9/bin/complete-ant-cmd.pl
/usr/local/Cellar/ant/1.10.9/bin/runant.pl
/usr/local/Cellar/ant/1.10.9/bin/runant.py
/usr/local/Cellar/ant/1.10.9/libexec/bin/ (6 files)
/usr/local/Cellar/ant/1.10.9/libexec/etc/ (16 files)
/usr/local/Cellar/ant/1.10.9/libexec/lib/ (55 files)
/usr/local/Cellar/ant/1.10.9/libexec/manual/ (1572 files)
/usr/local/Cellar/ant/1.10.9/libexec/ (8 files)
#说明:
#1、通过brew install安装应用最先是放在/usr/local/Cellar/目录下。
#2、有些应用会自动创建软链接放在/usr/bin或者/usr/sbin,同时也会将整个文件夹放在/usr/local
#3、可以使用 brew list 软件名 (比如 brew list ant)确定安装位置
注:使用官方脚本同样会遇到 uninstall 地址无法访问的问题,可以替换为下面脚本:
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"
写在最后
在此之前尝试过多种安装方法,例如下载 brew_install.rb 文件并执行 ruby brew_install.rb,起初可行但中途常报错,且出错原因各异,尝试多种解决方法均告失败。最终只有使用镜像安装才能成功,且下载速度非常快。
到此这篇关于 Mac 安装 Homebrew 的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持本站。