跳转到内容

Homebrew 安装教程 | macOS/Linux 包管理器快速上手指南

Homebrew Installation Guide

Homebrew 是 macOS 和 Linux 上最受欢迎的包管理器之一。它简化了软件安装、更新和卸载的流程,让开发者能够专注于代码而不是环境配置。本文将带你从零开始,完成 Homebrew 的安装和配置。

简介

Homebrew 由开发者 Max Howell 开发,并基于 BSD 开源,是一个非常方便的包管理器工具。在早期, Homebrew 仅有 macOS 的版本,后续随着用户的增多,Homebrew 还提供了 Linux 的版本,帮助开发者在 Linux 同样使用 Homebrew 来配置环境。

Homebrew 的核心特点

  • 简单易用:一条命令即可完成软件安装
  • 自动化依赖管理:自动处理软件依赖关系
  • 开源免费:完全开源,社区活跃
  • 跨平台支持:同时支持 macOS 和 Linux
  • 丰富的软件库:数万个软件包可供选择

Homebrew vs 其他包管理器

特性HomebrewMacPortsFink
安装方式源码编译 + 预编译源码编译源码编译
依赖管理自动自动自动
系统整合一般一般
软件数量非常多较少
更新频率中等

安装前的准备工作

系统要求

macOS:

  • macOS 10.15 (Catalina) 或更高版本
  • 命令行工具 (Command Line Tools)
  • 网络连接(建议配置镜像源)

Linux:

  • glibc 2.13 或更高版本
  • 64 位 x86_64 架构
  • GCC 4.4.7 或更高版本
  • Git

安装 Xcode Command Line Tools

在 macOS 上安装 Homebrew 之前,需要先安装命令行工具:

bash
# 方法一:使用 xcode-select
xcode-select --install

# 方法二:从 Apple Developer 网站下载
# 访问 https://developer.apple.com/download/

验证安装:

bash
gcc --version
git --version

安装 Homebrew

官方安装方法(推荐)

sh
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装过程说明:

  1. 下载安装脚本

    bash
    # 脚本会从 GitHub 下载最新的安装程序
    curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install
  2. 执行安装

    bash
    # 安装程序会:
    # - 创建 /usr/local/Homebrew 目录
    # - 克隆 homebrew-core 仓库
    # - 设置环境变量
    # - 配置权限
  3. 验证安装

    bash
    brew --version
    # 输出示例:Homebrew 4.x.x

国内镜像安装(加速)

如果你在中国大陆,建议使用镜像源加速安装:

使用清华大学镜像

bash
# 第一步:设置环境变量
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

# 第二步:执行安装
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 第三步:配置持久化
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc
source ~/.zshrc

使用中科大镜像

bash
# 设置环境变量
export HOMEBREW_BREW_GIT_REMOTE="git://mirrors.ustc.edu.cn/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="git://mirrors.ustc.edu.cn/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"

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

Linux 安装

在 Linux 上安装 Homebrew(也称为 Linuxbrew):

bash
# 安装依赖
sudo apt-get install build-essential procps curl file git  # Debian/Ubuntu
sudo yum groupinstall 'Development Tools'                   # CentOS/RHEL

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

# 配置环境变量
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

检查 Hombrew 环境

如果你的 Hombrew 没有办法正常的工作,你可以执行 brew doctor 来开启 Homebrew 自带的检查,从而确认有哪些问题,并进行修复。

常见问题及解决方案

bash
# 运行诊断
brew doctor

# 常见警告及解决方法:

# 1. 过时的 Command Line Tools
# 解决:更新 Xcode Command Line Tools
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

# 2. 未设置的 PATH
# 解决:添加 Homebrew 到 PATH
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

# 3. 权限问题
# 解决:修复权限
sudo chown -R $(whoami) /usr/local/*
sudo chmod -R u+w /usr/local/*

# 4. 未链接的 keg-only 软件
# 解决:按需链接
brew link --force software_name

查看详细配置信息

bash
# 查看 Homebrew 配置
brew config

# 输出包含:
# - macOS 版本
# - Homebrew 版本
# - Ruby 版本
# - Git 版本
# - 系统架构等信息

更新 Homebrew

sh
brew update

更新策略

bash
# 1. 更新 Homebrew 本身
brew update

# 2. 查看所有可更新的软件
brew outdated

# 3. 更新所有软件
brew upgrade

# 4. 更新特定软件
brew upgrade nginx

# 5. 清理旧版本
brew cleanup

# 6. 一键更新并清理(推荐)
brew update && brew upgrade && brew cleanup

自动更新配置

bash
# 禁用自动更新(加快 brew 命令速度)
export HOMEBREW_NO_AUTO_UPDATE=1

# 添加到 shell 配置文件
echo 'export HOMEBREW_NO_AUTO_UPDATE=1' >> ~/.zshrc

使用 Brewfile 完成环境迁移

设备用久了,我们的电脑中会有大量的软件,如果你需要迁移环境,重新安装会是一个大麻烦,好在 Homebrew 本身为我们提供了一个非常好用的环境迁移的工具 —— Homebrew Bundle

安装 Homebrew Bundle

bash
# Homebrew Bundle 通常已内置
# 如果没有,可以手动安装
brew tap Homebrew/bundle

导出当前环境

你首先需要在之前的电脑中执行 brew bundle dump 来完成当前环境的导出,导出完成后,你会得到一个 Brewfile。

bash
# 导出所有已安装的软件
brew bundle dump

# 导出到指定文件
brew bundle dump --file=~/Backup/Brewfile

# 包含 cask 软件(图形界面应用)
brew bundle dump --describe --force

Brewfile 示例:

ruby
# Brewfile

# Taps
tap "homebrew/bundle"
tap "homebrew/cask"
tap "homebrew/cask-fonts"
tap "homebrew/core"

# Formulae (命令行工具)
brew "git"
brew "node"
brew "nginx"
brew "mysql"
brew "redis"
brew "wget"
brew "tree"
brew "htop"

# Casks (图形界面应用)
cask "visual-studio-code"
cask "google-chrome"
cask "iterm2"
cask "docker"
cask "notion"

# Fonts
cask "font-fira-code"
cask "font-jetbrains-mono"
cask "font-source-code-pro"

恢复环境

然后将 Brewfile 复制到新的电脑中,并执行 brew bundle 来开始安装的过程。

bash
# 从当前目录的 Brewfile 安装
brew bundle

# 从指定文件安装
brew bundle --file=~/Backup/Brewfile

# 跳过已安装的软件(加快速度)
brew bundle --no-upgrade

# 仅安装,不更新已有软件
brew bundle install

Brewfile 最佳实践

1. 分类管理软件

ruby
# Brewfile.dev

# 核心工具
brew "git"
brew "node"
brew "python"

# 数据库
brew "mysql"
brew "postgresql"
brew "redis"
brew "mongodb-community"

# Web 服务器
brew "nginx"
brew "apache2"

# 开发工具
brew "vim"
brew "neovim"
brew "tmux"
brew "zsh"

# 监控工具
brew "htop"
brew "iftop"
brew "nmap"

2. 团队协作

bash
# 将 Brewfile 提交到版本控制
git add Brewfile
git commit -m "chore: add Brewfile for development environment"

# 团队成员同步环境
git pull
brew bundle

3. CI/CD 集成

yaml
# .github/workflows/setup.yml
name: Setup Environment
on: [push]
jobs:
  setup:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install Homebrew packages
        run: brew bundle --file=Brewfile

Homebrew 目录结构

了解 Homebrew 的目录结构有助于更好地管理和排查问题:

bash
# 查看 Homebrew 安装位置
brew --prefix
# 输出:/usr/local (Intel Mac) 或 /opt/homebrew (Apple Silicon)

# 主要目录说明
/usr/local/
├── Cellar/          # 软件包的实际安装位置
├── opt/             # 指向 Cellar 中最新版本的符号链接
├── bin/             # 可执行文件
├── include/         # 头文件
├── lib/             # 库文件
├── share/           # 共享资源
└── etc/             # 配置文件

常见问题排查

问题 1:安装速度慢或失败

bash
# 解决方案:使用镜像源
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

问题 2:权限错误

bash
# 修复权限
sudo chown -R $(whoami):admin /usr/local/*
sudo chmod -R g+w /usr/local/*

# 或者重置整个 Homebrew
sudo rm -rf /usr/local/Homebrew
# 重新安装

问题 3:找不到 brew 命令

bash
# 检查 PATH
echo $PATH

# 添加 Homebrew 到 PATH
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

# Apple Silicon Mac
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc

问题 4:依赖冲突

bash
# 查看依赖树
brew deps --tree nginx

# 强制重新安装
brew reinstall nginx

# 清理并重新链接
brew unlink nginx && brew link nginx

卸载 Homebrew

如果需要完全卸载 Homebrew:

bash
# 下载卸载脚本
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

# 或使用本地脚本
brew uninstall --force $(brew list)
rm -rf /usr/local/Homebrew
rm -rf ~/.cache/Homebrew

下一步学习

恭喜你完成了 Homebrew 的安装!接下来可以学习:

总结

本文详细介绍了 Homebrew 的安装和配置:

  1. 安装准备:Xcode Command Line Tools
  2. 安装方法:官方安装和镜像加速
  3. 环境检查:brew doctor 诊断
  4. 更新策略:保持软件最新
  5. 环境迁移:Brewfile 的使用
  6. 问题排查:常见问题解决方案

关键要点:

  • ✅ 中国大陆用户建议使用镜像源
  • ✅ 定期运行 brew doctor 检查环境
  • ✅ 使用 Brewfile 管理开发环境
  • ✅ 保持 Homebrew 和软件包更新

现在你已经准备好使用 Homebrew 来管理你的开发环境了!🎉