0%

玩转Homebrew

搜索软件

brew search [TEXT|/REGEX/]用于搜索软件,支持使用正则表达式进行复杂的搜索。

例如,查询静态博客生成工具 hugo:

1
2
3
4
5
6
$ brew search hugo

==> Searching local taps...
hugo ✔
==> Searching taps on GitHub...
==> Searching blacklisted, migrated and deleted formulae...

查看安装的包

查看已经安装的包

1
brew list

查看包相关信息

brew info 可以查看包的相关信息,最有用的应该是包依赖和相应的命令。比如 Nginx 会提醒你怎么加 launchctl ,PostgreSQL 会告诉你如何迁移数据库。这些信息会在包安装完成后自动显示,如果忘了的话可以用这个命令很方便地查看。

1
2
3
4
5
brew info $FORMULA    # 显示某个包的信息
brew info # 显示安装了包数量,文件数量,和总占用空间
brew deps 可以显示包的依赖关系,我常用它来查看已安装的包的依赖,然后判断哪些包是可以安全删除的。

brew deps --installed --tree # 查看已安装的包的依赖,树形显示

更新 Homebrew

要获取最新的包的列表,首先得更新 Homebrew 自己。这可以用 brew update 办到。

brew update
完后会显示可以更新的包列表,其中打钩的是已经安装的包。

更新包 (formula)

更新之前,我会用 brew outdated 查看哪些包可以更新。

brew outdated
然后就可以用 brew upgrade 去更新了。Homebrew 会安装新版本的包,但旧版本仍然会保留

brew upgrade # 更新所有的包

brew upgrade $FORMULA # 更新指定的包

清理旧版本

新版本安装了,旧版本就不需要了。我会用 brew cleanup 清理旧版本和缓存文件。Homebrew 只会清除比当前安装的包更老的版本,所以不用担心有些包没更新但被删了。

brew cleanup # 清理所有包的旧版本

brew cleanup $FORMULA # 清理指定包的旧版本

brew cleanup -n # 查看可清理的旧版本包,不执行实际操作

现在该更新的都更新了,旧版本也被清理。

对于 Homebrew 来说,如果没有卸载掉软件包的所有版本,那么 Homebrew 会继续尝试安装这个软件包的最新版本。要想彻底卸载某个软件包,需要执行命令:

1
brew uninstall formula_name --force

锁定不想更新的包

如果经常更新的话,brew update 一次更新所有的包是非常方便的。但我们有时候会担心自动升级把一些不希望更新的包更新了。数据库就属于这一类,尤其是 PostgreSQL 跨 minor 版本升级都要迁移数据库的。我们更希望找个时间单独处理它。这时可用 brew pin 去锁定这个包,然后 brew update 就会略过它了。

1
2
brew pin $FORMULA      # 锁定某个包
brew unpin $FORMULA # 取消锁定

服务管理

brew services 用于方便的管理 brew 安装的软件软件,类似于 Linux 下的 service 命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
brew services command:
Integrates Homebrew formulae with macOS' launchctl manager.

[sudo] brew services list:
List all running services for the current user (or root).

[sudo] brew services run (formula|--all):
Run the service formula without registering to launch at login (or boot).

[sudo] brew services start (formula|--all):
Start the service formula immediately and register it to launch at login (or boot).

[sudo] brew services stop (formula|--all):
Stop the service formula immediately and unregister it from launching at login (or boot).

[sudo] brew services restart (formula|--all):
Stop (if necessary) and start the service formula immediately and register it to launch at login (or boot).

[sudo] brew services cleanup:
Remove all unused services.

If sudo is passed, operate on /Library/LaunchDaemons (started at boot).
Otherwise, operate on ~/Library/LaunchAgents (started at login).

查看配置信息

brew config 用于查看 brew 所在环境及相关的配置情况

诊断问题

brew doctor 诊断当前 brew 存在哪些问题,并给出解决方案

仓库管理

1
2
3
4
5
brew tap 已安装的仓库列表

brew tap [--full] user/repo [URL] 添加仓库

brew untap tap 移除仓库

替换 homebrew 源

替换现有上游

清华镜像源,下面有阿里巴巴的镜像源,国内镜像源有很多 USTC,清华,Coding.net,aliyun等。Google之。

1
2
3
4
5
6
7
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

brew update

复原

(感谢Snowonion Lee提供说明)

1
2
3
4
5
6
7
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git

brew update

执行 brew install 命令长时间卡在 Updating Homebrew 的解决方法

在国内的网络环境下使用 Homebrew 安装软件的过程中可能会长时间卡在 Updating Homebrew 这个步骤。

例:执行 brew install composer 命令

1
2
➜  ~ brew install composer
Updating Homebrew... # 如果碰到长时间卡在这里,参考以下 2 种处理方法
方法 1:按住 control + c 取消本次更新操作
1
2
3
➜  ~ brew install composer
Updating Homebrew...
^C

按住 control + c 之后命令行会显示 ^C,就代表已经取消了 Updating Homebrew 操作

大概不到 1 秒钟之后就会去执行我们真正需要的安装操作了

1
2
3
4
5
➜  ~ brew install composer
Updating Homebrew...
^C==> Satisfying dependencies
==> Downloading https://getcomposer.org/download/1.7.2/composer.phar
...

这个方法是临时的、一次性的

方法 2:使用 Alibaba 的 Homebrew 镜像源进行加速

平时我们执行 brew 命令安装软件的时候,跟以下 3 个仓库地址有关:

  1. brew.git
  2. homebrew-core.git
  3. homebrew-bottles

通过以下操作将这 3 个仓库地址全部替换为 Alibaba 提供的地址

1. 替换 / 还原 brew.git 仓库地址
1
2
3
4
5
6
7
8
9
# 替换成阿里巴巴的 brew.git 仓库地址:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

#=======================================================

# 还原为官方提供的 brew.git 仓库地址
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
2. 替换 / 还原 homebrew-core.git 仓库地址
1
2
3
4
5
6
7
8
9
# 替换成阿里巴巴的 homebrew-core.git 仓库地址:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

#=======================================================

# 还原为官方提供的 homebrew-core.git 仓库地址
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
3. 替换 / 还原 homebrew-bottles 访问地址

这个步骤跟你的 macOS 系统使用的 shell 版本有关系

所以,先来查看当前使用的 shell 版本

1
2
3
4
echo $SHELL

# 如果你的输出结果是 /bin/zsh,参考下方的 zsh 终端操作方式
# 如果你的输出结果是 /bin/bash,参考下方的 bash 终端操作方式

3.1 zsh 终端操作方式

1
2
3
4
5
6
7
8
9
10
# 替换成阿里巴巴的 homebrew-bottles 访问地址:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

#=======================================================

# 还原为官方提供的 homebrew-bottles 访问地址
vi ~/.zshrc
# 然后,删除 HOMEBREW_BOTTLE_DOMAIN 这一行配置
source ~/.zshrc

3.2 bash 终端操作方式

1
2
3
4
5
6
7
8
9
10
# 替换 homebrew-bottles 访问 URL:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

#=======================================================

# 还原为官方提供的 homebrew-bottles 访问地址
vi ~/.bash_profile
# 然后,删除 HOMEBREW_BOTTLE_DOMAIN 这一行配置
source ~/.bash_profile