January 28, 2023
By: Kevin

homebrew的原理以及使用

  1. 安装
    1. 直接安装
    2. 使用清华的镜像安装
  2. 工作原理
  3. 术语
    1. formula(配方)
    2. cask(大酒桶)
    3. keg(小木桶), 是bottle(酒瓶)的上级目录
    4. rack(酒架), 是所有keg(小木桶)的上级容器
    5. cellar(酒窖), 所有Rank(酒架)和Caskroom(储藏室)的上级容器
    6. Caskroom(酒窖储藏室)
    7. external command(外部命令)
    8. tap(龙头)
    9. bottle(瓶装酒)
  4. 常用命令
    1. 查找应用
    2. 安装命令行程序
    3. 安装图形界面应用程序
    4. 移除程序
    5. 更新程序
    6. 查看所有已经安装的应用
    7. 增加新的源
    8. 查看程序状态
    9. 后台服务

Homebrew由开发者 Max Howell开发开源包管理工具,是当前(2022年)osx上最方便的包管理器工具。

Homebrew 早期仅有macOS版本,现在也可以进行Linux包管理.

可以一键安装开源免费的命令行以及图形界面应用. 使用体验接近apt, yum之类的Linux包管理工具.

对高级用户(程序员)非常友好.

安装

直接安装

如果可以科学上网, 直接安装最简单.

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

使用清华的镜像安装

因为github很不稳定, 可以切换为国内的镜像. https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/

工作原理

切换镜像就是使用国内的地址, 换掉 HOMEBREW_BREW_GIT_REMOTE,HOMEBREW_BOTTLE_DOMAIN

下面的图很好的解释了homebrew的工作过程

  1. 从指定的 HOMEBREW_BREW_GIT_REMOTE 获得最新的配方
  2. 配方中指定了创建某个可执行程序的的依赖
  3. 如果是现成的(有已经预编译的版本), 则直接从HOMEBREW_BOTTLE_DOMAIN下载获得, 如果没有, 则需要根据配方获取所有依赖, 然后在本地编译构建.

术语

formula(配方)

Homebrew package definition built from upstream sources 存在 HOMEBREW_BREW_GIT_REMOTE 的配置

cask(大酒桶)

Homebrew package definition that installs macOS native applications 放在 /opt/homebrew/Caskroom , 应用指向安装于/Applications/目录下的可执行程序, 一般是GUI程序, 比如iterm, emacs, wireshark等

➜  Caskroom git:(stable) tree /opt/homebrew/Caskroom/iterm2
/opt/homebrew/Caskroom/iterm2
└── 3.4.12
    └── iTerm.app -> /Applications/iTerm.app

keg(小木桶), 是bottle(酒瓶)的上级目录

installation destination directory of a given formula version. 以下目录就是酒窖里git的小木桶, 特定的版本的git /opt/homebrew/Cellar/git/2.38.0/

rack(酒架), 是所有keg(小木桶)的上级容器

directory containing one or more versioned kegs. 酒窖里的git的酒架, git的总目录 /opt/homebrew/Cellar/git/

cellar(酒窖), 所有Rank(酒架)和Caskroom(储藏室)的上级容器

directory containing one or more named racks. /opt/homebrew/Cellar/

Caskroom(酒窖储藏室)

directory containing one or more named casks. /opt/homebrew/Caskroom/

external command(外部命令)

brew subcommand defined outside of the Homebrew/brew GitHub repository

tap(龙头)

directory (and usually Git repository) of formulae, casks and/or

bottle(瓶装酒)

pre-built keg poured into the cellar/rack instead of building from upstream sources /opt/homebrew/Cellar/git/2.38.0/bin/git

常用命令

查找应用

brew search 比如说搜索git

➜  lisp git:(master) ✗ brew search git
==> Formulae
bagit                            git-filter-repo                  git-remote-codecommit            git-workspace                    legit
bash-git-prompt                  git-fixup                        git-remote-gcrypt                git-xargs                        libgit2
bit-git                          git-flow                         git-remote-hg                    gitbackup                        libgit2-glib
cgit                             git-flow-avh                     git-review                       gitbatch                         literate-git
digitemp                         git-fresh                        git-revise                       gitbucket                        modgit
easy-git                         git-ftp                          git-secret                       gitfs                            moz-git-tools
git ✔                            git-game                         git-secrets                      gitg                             multi-git-status
git-absorb                       git-gerrit                       git-series                       github-keygen                    pass-git-helper
git-annex                        git-gui                          git-sizer                        github-markdown-toc              pygitup
git-annex-remote-rclone          git-hooks-go                     git-ssh                          github-release                   sagittarius-scheme
git-appraise                     git-hound                        git-standup                      gitlab-ci-local                  stgit
git-archive-all                  git-if                           git-subrepo                      gitlab-gem                       topgit
git-branchless                   git-imerge                       git-svn                          gitlab-runner                    ugit
git-bug                          git-integration                  git-svn-abandon                  gitleaks                         ungit
git-cal                          git-interactive-rebase-tool      git-sync                         gitless                          willgit
git-cinnabar                     git-lfs                          git-test                         gitlint                          zsh-git-prompt
git-cliff                        git-machete                      git-tf                           gitmoji                          gist
git-codereview                   git-multipush                    git-tig                          gitql                            gmt
git-cola                         git-now                          git-town                         gitslave                         got
git-credential-libsecret         git-number                       git-tracker                      gitter-cli                       grt
git-credential-manager           git-octopus                      git-trim                         gitui                            gwt
git-crypt                        git-open                         git-url-sub                      gitup                            lit
git-delete-merged-branches       git-plus                         git-utils                        gitversion                       pit
git-delta                        git-quick-stats                  git-vendor                       gitwatch                         bit
git-extras                       git-recent                       git-when-merged                  lazygit                          vit

==> Casks
adobe-digital-editions      gitblade                    gitfox                      gitnote                     gitup                       smartgit
deepgit                     gitdock                     github                      gitpigeon                   gitx                        snagit
digital                     gitee                       githubpulse                 gitscout                    logitech-presentation       subgit
git-it                      gitfiend                    gitify                      gitter                      plotdigitizer               webplotdigitizer
gitahead                    gitfinder                   gitkraken                   gittyup                     rowanj-gitx                 xit

安装命令行程序

安装git到cell目录 brew install git

安装图形界面应用程序

比如安装wireshark或者emacs之类的带图形界面的应用, 会安装到Caskroom中

brew install --cask wireshark

移除程序

brew uninstall git

更新程序

  • brew update 更新git库
  • brew upgrade 更新应用

查看所有已经安装的应用

brew list

增加新的源

brew tap 增加一个新的源, 比如说为了安装emacs-plus, 需要增加新的源 brew tap d12frosted/emacs-plus

➜  ~ brew tap
# 这个是我们配置的
borkdude/brew           #babashka #kondo-clj
d12frosted/emacs-plus   #emacs
# 以下是默认
homebrew/cask
homebrew/core
homebrew/services

查看程序状态

  • 查看安装状态 brew info : 查看xxx安装和磁盘占用 brew info xxx 查看某个具体应用的安装
  • 查看所有安装 brew list

后台服务

brew servcie 借助launchctl 实现系统自动(或者定时执行), 在我的系统上, 数据库(mysql), emacs等都是通过服务来管理的.

# 启动某服务, 且注册为系统自启动
brew services start xxxx
# 停止某服务
brew services stop xxxx
# 重启某个服务
brew services restart xxxx
# 列出所有的brew service
brew services list
Tags: mac