Lazy loaded image
mac 包管理 Homebrew 入门
Words 973Read Time 3 min
2025-9-29
2025-11-10
date
related_level
slug
brew_intro
type
Post
relate_date
summary
Homebrew 核心概念、国内镜像安装配置、常用指令、服务管理
status
Published
tags
mac
环境管理
category
运维管理
last_updated
Nov 10, 2025 10:02 PM
是否已更新
orginal_page
是否推荐
 
参考资料
  • 一些常用核心概念
    • formula (e)
      安装包的描述文件,formulae 为复数
      cellar
      安装好后所在的目录
      keg
      具体某个包所在的目录,keg 是 cellar 的子目录
      bottle
      预先编译好的包,不需要现场下载编译源码,速度会快很多;官方库中的包大多都是通过 bottle 方式安装
      tap
      下载源,可以类比于 Linux 下的包管理器 repository
      cask
      安装 macOS native 应用的扩展,你也可以理解为有图形化界面的应用。
      bundle
      描述 Homebrew 依赖的扩展
 
homebrew 安装脚本(清华源)
  • 官方一键安装脚本
    • /bin/bash -c "$(curl -fsSL https://github.com/Homebrew/install/raw/master/install.sh)"
  • macOS 用户需要安装 Command Line Tools (CLT) for Xcode
    • xcode-select --install
设置环境变量
  • 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"
    • 部分用户无需再克隆 homebrew/core 仓库,故无需设置 HOMEBREW_CORE_GIT_REMOTE 环境变量
    • 但若需要运行 brew 的开发命令或者 brew 安装在非官方支持的默认 prefix 位置,则仍需设置 HOMEBREW_CORE_GIT_REMOTE 环境变量
  • export HOMEBREW_INSTALL_FROM_API=1
    • 自 brew 4.0 起,HOMEBREW_INSTALL_FROM_API 会成为默认行为,无需设置
运行安装脚本
  • git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
    • 下载安装脚本
  • /bin/bash brew-install/install.sh
  • rm -rf brew-install
默认安装路径 cellar
  • /opt/homebrew
    • Apple Silicon
  • /usr/local
    • for macOS Intel
  • /home/linuxbrew/.linuxbrew
    • for Linux
切换国内镜像源
  • 假设使用 zsh 终端
替换 brew 程序本身的源
  • export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
  • brew update
设置 Homebrew Bottles 环境变量
  • 写入 profile 持久化
    • echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zprofile
    • echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile
  • export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
  • export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
针对 macOS 系统上的 Homebrew 更新 brew 源(core, cask, other)
vim switch-brew-source.sh
  • core 和 cask 按需执行
  • chmod +x switch-brew-source.sh
  • bash switch-brew-source.sh
或者通过以下指令执行更新
  • HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
  • for tapin core cask command-not-found;do brew tap --custom-remote --force-auto-update "homebrew/${tap}" "https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-${tap}.git" done
  • brew update
复原仓库上游
  • 重置回默认远程后,用户应该删除 shell 的 profile 设置中的环境变量 HOMEBREW_BREW_GIT_REMOTE 和 HOMEBREW_CORE_GIT_REMOTE 以免运行 brew update 时远程再次被更换
  • 针对 macOS 系统上的 Homebrew
管理 Homebrew
  • brew update
    • 更新 Homebrew 本体
  • brew doctor
    • 检查 Homebrew 环境
  • brew tap [user/repo]
    • 添加源
    • 一些特殊源
      • homebrew-cask-fonts
        • 提供代码字体
        • 安装 source code pro 字体
          • brew tap homebrew/cask-fonts
          • brew cask install font-source-code-pro
Homebrew 常用指令
  • brew install <formula>
  • brew list
    • 查看已安装的所有软件
  • brew list <formula>
    • 查看已安装软件文件路径
  • brew outdated
    • 查看有可用更新的软件
  • brew upgrade <formula>
    • 更新特定软件
  • brew uninstall <formula>
  • brew cleanup <formula>
    • 相当于 autoremove
Homebrew 管理后台软件
  • 软件日志位置
    • /opt/homebrew/var/log
  • brew services [list]
    • 查看
  • brew services info <formula>
  • brew services start <formula>
    • 运行并添加到开机时启动
      • 相当于 enable --now
  • brew services run <formula>
    • 仅运行
  • brew services restart <formula>
  • brew services stop <formula>
    • 停止运行并移除开机时启动
      • 相当于 disable --now
      • 通过添加 --keep 避免移除开机时启动
使用 Brewfile 完成环境迁移
  • brew bundle dump --global --force
    • 完成当前环境的导出到  ~/.Brewfile 文件
    • --describe 可以添加每个包的描述
  • brew bundle install
    • 新电脑执行安装
 
上一篇
JavaScript 学习笔记
下一篇
自建 git server 同步配置文件

Comments
Loading...