如何安装 Homebrew — MacOS 入门指南

发布: (2026年1月31日 GMT+8 01:15)
6 min read
原文: Dev.to

Source: Dev.to

Little Coding Things Blog

最初发表于 Little Coding Things

如果你是 macOS 的新手,可能从未听说过 Homebrew。Homebrew 是一个 强大的包管理器,如果你之前使用过 npm,它的概念类似。它让你可以直接在终端中轻松地安装、更新和移除软件包,是开发者非常实用的工具。在本文中,你将看到在 macOS 上安装 Homebrew 的一步步指南。

为什么安装 Homebrew 很重要?

Homebrew 被广泛认为是 macOS 上最受欢迎的包管理器。它因简洁易用和活跃的社区支持,成为开发者和高级用户的首选。

Homebrew 让你轻松获取开发或日常任务所需的工具和库,从 Git、Node.js 等开发工具到简单的日常实用程序。它通过省去手动下载和配置的麻烦,节省时间。使用它既简单又快速,是定制你的 Mac 的理想选择。

如果你之前没有使用过类似 Homebrew 的工具,请不要忽视它——它可能会彻底改变你在 Mac 上管理软件的方式。

检查 Homebrew 是否已安装

为了确保您之前没有安装过 Homebrew,让我们检查一下。打开终端并输入:

brew help

如果已安装,它会显示类似如下内容:

Example usage:
  brew search TEXT|/REGEX/
  brew info [FORMULA|CASK...]
  brew install FORMULA|CASK...
  brew update
  brew upgrade [FORMULA|CASK...]

否则您会看到以下结果:

command not found: brew

Source:

如何安装 Homebrew

准备好安装 Homebrew 了吗?以下是你需要了解的全部内容。打开终端(⌘ + 空格键并输入 “Terminal”),然后运行:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

这条命令做了什么?

  • /bin/bash – 指定使用 Bash shell,确保即使默认 shell 不是 Bash(例如 Zsh),也能用 Bash 执行该命令。
  • -c – 告诉 Bash 执行后面引号中的命令。
  • $(curl -fsSL …) – 使用 curl(一种数据传输工具)从 GitHub 获取 Homebrew 安装脚本。

简而言之,这条命令会从 GitHub 下载 Homebrew 安装脚本,并立即使用 Bash 运行它,以在你的系统上设置 Homebrew。

系统会要求你输入超级用户密码:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Checking for `sudo` access (which may request your password)...
Password:

输入密码后,你会看到一个提示,要求按 ENTER 继续:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Checking for `sudo` access (which may request your password)...
Password:

==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew

/opt/homebrew

==> The following new directories will be created:
/opt/homebrew/bin
/opt/homebrew/etc
/opt/homebrew/include

/opt/homebrew/Frameworks

Press RETURN/ENTER to continue or any other key to abort:

等待安装过程完成——这可能需要几分钟。完成后,你会看到 “Installation successful!”(安装成功)的提示。要验证安装是否成功,运行:

brew help

如果一切顺利,你应该会看到与 “检查 Homebrew 是否已安装” 部分相同的输出。

Source:

Homebrew 安装后无法使用

如果你已经按照所有步骤操作,但仍然出现 “command not found” 错误(例如 brew help),很可能是 Homebrew 可执行文件没有加入到你的 PATH 中。

要解决此问题,请重新查看 Homebrew 安装脚本的日志。脚本结束时通常会出现类似以下的指示:

Warning: /opt/homebrew/bin is not in your PATH.
Instructions on how to configure your shell for Homebrew can be found in the 'Next steps' section below.
==> Installation successful!
...
==> Next steps:
- Run these commands in your terminal to add Homebrew to your PATH:
  echo >> /Users//.zprofile
  echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users//.zprofile
  eval "$(/opt/homebrew/bin/brew shellenv)"

运行上述命令(如果你使用的是其他 shell 配置文件,如 ~/.bash_profile~/.zshrc,请相应调整路径)。将 Homebrew 添加到 PATH 后,重新启动终端或 source 该配置文件,然后再次使用 brew help 验证。


就这样!现在你已经在 macOS 系统上成功安装 Homebrew,并可以用它来管理软件包。祝编码愉快!

复制 Homebrew 安装说明的最后部分——从以 echo 开头的命令开始——并粘贴到终端中。该命令通常会将 Homebrew 添加到系统的 PATH

此时,你的系统应该已经成功安装了 Homebrew。

恭喜! 这只是你掌握强大工具和工作流的激动人心旅程的第一小步。如果你接下来想探索自动化开发工作流的方式,可能会发现像 Husky 预提交钩子 这样的 Git 工具特别有帮助。

Back to Blog

相关文章

阅读更多 »

cURL 入门:向互联网发送消息

什么是 cURL?用非常简单的话来说,cURL 代表 Client URL。把它想象成一个没有按钮、图像或颜色的网页浏览器。它是一个命令行工具……