Homebrew 使用

安装

  1. 安装 brew:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. 安装 command-not-found

    brew tap homebrew/command-not-found
    

    将下面的命令加入 ~/.zshrc

    HB_CNF_HANDLER="$(brew --repository)/Library/Taps/homebrew/homebrew-command-not-found/handler.sh"
    if [ -f "$HB_CNF_HANDLER" ]; then
        source "$HB_CNF_HANDLER"
    fi
    

换源

使用镜像源

版本变更信息:

  • 4.3.0:弃用了 cask-fonts 源和 cask-versions 源,合并到 cask 源。
  • 4.0.0:软件包信息不再从 corecask 源获取,转而从 API (formulae.brew.sh) 获取。相应地,弃用了环境变量 HOMEBREW_CORE_GIT_REMOTE 而启用了 HOMEBREW_API_DOMAIN

更新 brew git 仓库索引:

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"  # 指定 brew 自身的 git 仓库地址
brew update  # 更新 brew

~/.zshrc 中添加以下内容:

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"
export HOMEBREW_PIP_INDEX_URL="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
  • HOMEBREW_API_DOMAIN:Homebrew 检索包元数据 ruby 文件的 API URL
  • HOMEBREW_BOTTLE_DOMAIN:Homebrew 下载预编译包(bottle)的 URL
  • HOMEBREW_PIP_INDEX_URL:Homebrew 使用 pip 安装包时使用的索引 URL
  • HOMEBREW_BREW_GIT_REMOTE:brew 的 git 仓库地址
  • HOMEBREW_CORE_GIT_REMOTE:core 源的 git 仓库地址。现在 core 源已经弃用,不过当 brew 安装在非标准位置时,会从 core 源获取包元数据 ruby 文件

指定 command-not-found 源的 git 仓库地址:

brew tap --custom-remote --force-auto-update "homebrew/command-not-found" "https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-command-not-found.git"

使用官方源

更新 brew git 仓库地址:

export HOMEBREW_BREW_GIT_REMOTE="https://github.com/Homebrew/brew.git"  # 指定 brew 自身的 git 仓库地址
brew update  # 更新索引

.zshrc 中删除以下内容:

-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"
-export HOMEBREW_PIP_INDEX_URL="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"

指定 command-not-found 源的 git 仓库地址:

brew tap --custom-remote "homebrew/command-not-found" "https://github.com/Homebrew/homebrew-command-not-found.git"

各镜像站参考文档:

Tap 管理

tap 是 Homebrew 的第三方软件源。

brew tap                  # 查看已添加的源
brew tap owner/repo       # 添加源 owner/homebrew-repo
brew untap owner/repo     # 删除源
brew tap-info owner/repo  # 查看源信息

也可以不添加源而直接安装源中的包:

brew install owner/repo/package

常用命令

brew install <package>  # 安装软件包
brew remove <package>   # 卸载软件包
brew list               # 显示已安装软件包列表
brew update             # 更新索引
brew upgrade            # 更新所有软件包
brew search <package>   # 搜索软件包
brew info <package>     # 显示软件包详细信息
brew help [command]     # 显示命令帮助
man brew                # 显示帮助手册

术语

  • Homebrew: 自酿酒
  • formula: 配方(CLI 包)
  • Cellar: 酒窖(存放 formula 包)
  • cask: 盛酒的木桶(GUI 包)
  • Caskroom:酒桶房(存放 cask 包)
  • tap:龙头(软件源)
  • purge: 清洗(彻底卸载软件包)
  • keg:小桶(不会自动 link 的软件包)

Formulae 和 Cask 的区别

  • 最明显的区别在于 formulae 一般是命令行程序,而 cask 一般是图形化程序。

  • Homebrew Cask 项目:原先是独立于 Homebrew 的一个扩展,提供对以二进制形式发布的 macOS 应用的管理,但现在与 Homebrew 密切合作。

  • Formulae 和 Cask:Homebrew 项目将自己的包定义文件称为 formulae,而 Homebrew Cask 项目将自己的包定义文件称为 cask。它们都是用基于 Ruby 的 DSL 编写的文件,描述如何安装软件。区别在于 formulae 往往提供命令行程序的包定义文件,而 cask 往往提供图形化程序的包定义文件。

  • 下载好的 formulae 包会存放在 Homebrew 的 Cellar 子目录下,并且会链接到 opt 子目录中。对于 cask 包,则会存放到 Caskroom 子目录下。

参见:

Troubleshooting

It seems the App source '/Applications/Spotify.app' is not there.

解决方法:

rm -rf /opt/homebrew/Caskroom/spotify
brew install spotify
posted @ 2024-05-22 13:33  Undefined443  阅读(209)  评论(0)    收藏  举报