Homebrew的基本用法

文章更新

  1. 20160810-初次成文
  2. 20170727-丰富内容

为什么会有这篇文章

之前因为MBP出了点问题,拿去苹果售后维修,本本回来了硬盘里的内容全部丢失,顺便记录一下Homebrew的用法。

Homebrew介绍

Homebrew是Mac OSX上的软件包管理工具,类似ubuntu系统下的apt-get的功能,或者RHEL/HEL下的yum。

安装Homebrew

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

这条命令可以在homebrew项目的官方网站的首页看到。

安装过程没啥可说的,唯一耗时比较长的就是下载和安装xcode command line tools的地方。

==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following new directories will be created:
/usr/local/Cellar
/usr/local/Homebrew
/usr/local/Frameworks
/usr/local/bin
/usr/local/etc
/usr/local/include
/usr/local/lib
/usr/local/opt
/usr/local/sbin
/usr/local/share
/usr/local/share/zsh
/usr/local/share/zsh/site-functions
/usr/local/var

==> /usr/bin/sudo /bin/mkdir -p /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var
Password: 输入一次密码

==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var
==> /usr/bin/sudo /bin/chmod 755 /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /usr/sbin/chown wangyi /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var
==> /usr/bin/sudo /bin/mkdir -p /Users/wangyi/Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Users/wangyi/Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown wangyi /Users/wangyi/Library/Caches/Homebrew
==> /usr/bin/sudo /bin/mkdir -p /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown wangyi /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 6633, done.
remote: Compressing objects: 100% (4011/4011), done.
remote: Total 6633 (delta 3892), reused 4345 (delta 2414), pack-reused 0
Receiving objects: 100% (6633/6633), 3.57 MiB | 59.00 KiB/s, done.
Resolving deltas: 100% (3892/3892), done.
From https://github.com/Homebrew/brew
 * [new branch]      master     -> origin/master
 * [new tag]         0.1        -> 0.1
 * [new tag]         0.2        -> 0.2
 * [new tag]         0.3        -> 0.3
 * [new tag]         0.4        -> 0.4
 * [new tag]         0.5        -> 0.5
 * [new tag]         0.6        -> 0.6
 * [new tag]         0.7        -> 0.7
 * [new tag]         0.7.1      -> 0.7.1
 * [new tag]         0.8        -> 0.8
 * [new tag]         0.8.1      -> 0.8.1
 * [new tag]         0.9        -> 0.9
 * [new tag]         0.9.1      -> 0.9.1
 * [new tag]         0.9.2      -> 0.9.2
 * [new tag]         0.9.3      -> 0.9.3
 * [new tag]         0.9.4      -> 0.9.4
 * [new tag]         0.9.5      -> 0.9.5
 * [new tag]         0.9.8      -> 0.9.8
 * [new tag]         0.9.9      -> 0.9.9
 * [new tag]         1.0.0      -> 1.0.0
 * [new tag]         1.0.1      -> 1.0.1
 * [new tag]         1.0.2      -> 1.0.2
 * [new tag]         1.0.3      -> 1.0.3
 * [new tag]         1.0.4      -> 1.0.4
 * [new tag]         1.0.5      -> 1.0.5
 * [new tag]         1.0.6      -> 1.0.6
 * [new tag]         1.0.7      -> 1.0.7
 * [new tag]         1.0.8      -> 1.0.8
 * [new tag]         1.0.9      -> 1.0.9
 * [new tag]         1.1.0      -> 1.1.0
 * [new tag]         1.1.1      -> 1.1.1
 * [new tag]         1.1.10     -> 1.1.10
 * [new tag]         1.1.11     -> 1.1.11
 * [new tag]         1.1.12     -> 1.1.12
 * [new tag]         1.1.13     -> 1.1.13
 * [new tag]         1.1.2      -> 1.1.2
 * [new tag]         1.1.3      -> 1.1.3
 * [new tag]         1.1.4      -> 1.1.4
 * [new tag]         1.1.5      -> 1.1.5
 * [new tag]         1.1.6      -> 1.1.6
 * [new tag]         1.1.7      -> 1.1.7
 * [new tag]         1.1.8      -> 1.1.8
 * [new tag]         1.1.9      -> 1.1.9
 * [new tag]         1.2.0      -> 1.2.0
 * [new tag]         1.2.1      -> 1.2.1
 * [new tag]         1.2.2      -> 1.2.2
 * [new tag]         1.2.3      -> 1.2.3
 * [new tag]         1.2.4      -> 1.2.4
HEAD is now at 24f4797 Merge pull request #2929 from woodruffw/master
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 4489, done.
remote: Compressing objects: 100% (4280/4280), done.
remote: Total 4489 (delta 36), reused 468 (delta 14), pack-reused 0
Receiving objects: 100% (4489/4489), 3.56 MiB | 153.00 KiB/s, done.
Resolving deltas: 100% (36/36), done.
Tapped 4279 formulae (4,534 files, 11.1MB)
==> Cleaning up /Library/Caches/Homebrew...
==> Migrating /Library/Caches/Homebrew to /Users/wangyi/Library/Caches/Homebrew...
==> Deleting /Library/Caches/Homebrew...
Already up-to-date.
Warning: /usr/local/bin is not in your PATH.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
  http://docs.brew.sh/Analytics.html

==> Next steps:
- Run `brew help` to get started
- Further documentation:
    http://docs.brew.sh

这个过程大概用了5-6分钟吧,提前安装xcode可以缩短上面的过程。

检查是否安装正确

为了系统安全升级,先要让Homebrew检查系统是否可以安装或者更新软件:

brew doctor

添加homebrew的路径到系统PAHT中。如果使用 echo $PATH 发现这个路径已经在系统路径中,则无需添加。

export PATH="/usr/local/bin:$PATH"

2017年7月27日更新

我的环境是MAC OS 10.11(EI CAPITAN)和XCODE,最近发现安装后通过 brew doctor 命令检查的时候,Homebrew 会显示下面这样一个警告错误

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!

Warning: You have Xcode 8 installed without the CLT;
this causes certain builds to fail on OS X El Capitan (10.11).
Please install the CLT via:
  sudo xcode-select --install

意思是缺少开发人员命令行工具,需要单独安装,网上答案说因为xcode 8已经默认不在低于10.12版本的mac os系统上安装这个工具了,需要手动安装,好在命令也不麻烦

sudo xcode-select --install

Homebrew 升级和更新

brew update && brew upgrade

PS:关于sudo的用户权限问题,如果你发现上面命令不行,而强制使用 sudo brew update 的话,会遇到一个错误:

Error: Cowardly refusing to 'sudo brew update'
You can use brew with sudo, but only if the brew executable is owned by root.
However, this is both not recommended and completely unsupported so do so at
your own risk.

意思就是Homebrew绝对不推荐使用 sudo brew update 命令对系统进行强制更新,虽然可以这样使用,但是也最好在确定需要执行的操作属于root用户的情况下才可以这样做,Homebrew不推荐也不支持用户使用sudo来进行homebrew的相关操作。

就算你使用了 sudo -s 切换到 root 用户下,重新执行也不行。

这是因为Homebrew在安装的时候不隶属于root用户,所以,需要执行下面的命令

sudo chown $(whoami) /usr/local/bin/brew

将Homebrew相关文件的所有权,重新划分到当前用户下,这样Homebrew的主人就变更为了当前用户,再次执行相关的homebrew操作的时候,不需要使用sudo命令。

通过执行 brew doctor,可以诊断系统Homebrew冲突,如果结果显示

Your system is ready to brew.

则表明Homebrew已经准备就绪,可以进行升级操作了。

说个题外话,在很早的时候我使用 brew doctor 诊断系统冲突,发现有很多Node目录下的文件,这是因为node.js在当时,我并不是使用homebrew进行的安装。因此有很多文件,没有受到Homebrew的监控,而被安装到了不应该的位置上,这里开了个新帖子,专门讨论如何在mac下完整卸载Node.js,并重新使用Homebrew进行安装。

如果上面的命令结果中有warning,需要先分析下问题是什么,然后针对性的去解决之后,再进行下一步。

如何安装软件

brew install git #安装git
brew install wget #安装wget

如何卸载软件

brew uninstall wget

如何查询软件

有时候,你不知道你安装的软件的名字, 那么你需要先搜索下, 查到包的名字。

比如我要安装

brew search /wge*/

/wge*/是个正则表达式,需要包含在/

其他brew命令

  • brew list #列出已安装的软件
  • brew update #更新brew
  • brew home #用浏览器打开brew的官方网站
  • brew info #显示软件信息
  • brew deps #显示包依赖

Homebrew中提示的keg-only是什么意思

首先,brew 本身是酿造、酿酒的意思,会用这个字的原因是 Homebrew 的安装方式为下载源代码到本地回来做编译,由于是在自己电脑做本地编译,所以这个工具叫做 brew at Home,自家酿酒。

酿酒需要有配方(formula),当你需要安装套件时,流程就是使用brew命令去根据配方 formula ,酿造出一桶(keg)酒来。所以 keg 指的是整个编译完成的套件资料夹。

再来,放置套件的位置在 /usr/local/Cellar,Cellar就是地窖,一桶一桶酿好的酒当然要存放在地窖里面了!所以编译完成的套件资料夹 keg 预设目录在 /usr/local/Cellar 。

最后回到「keg-only」这个词,字面上意思现在就很清楚,表示这个套件只会存放在桶里,不会跑出桶外面;实际上的行为是 brew 不会帮你做 symlink 到 /usr/local ,避免你的原生系统内还有一套名称相同的套件,从而引起冲突,所以如果遇到安装或者更新套件后提示某个套件是 keg-only ,那么你就需要自己去做symlink的事情了。

其实这就引出了Homebrew安装套件的两种不同方式了,继续往下看

Homebrew安装的两种方式

Homebrew安装软件分为两种情况:

  1. 系统没有自带的

    这个没什么好说的,因为如果系统没有带,我们安装完相应的软件之后就自动的将编译好的二进制文件软链接到PATH中,这样才会生效。

  2. 系统自带的

    如果系统自带的有这个软件,那么问题就不好办了,是直接覆盖呢,还是应该给用户一些选择?本着上面说过的原则,尽量少的影响原来的目录树,那么它在安装完二进制文件之后并没有做建软链的那一步操作,这就是所谓的keg-only的意思了。

那么,如果我要更新wget,而OS X发行时本就是带着wget的,应该怎么办呢?当然作者已经替我们想到了这一点,

brew link wget --force

这样,它就会把需要的二进制链接到PATH中,要注意这时这些路径中是存在相应的二进制文件的,正是homebrew不敢确定是不是要直接帮我们做这些操作,才给我们提供了这个命令。

那么如果你之前已经安装了不少软件了,发现有好几个没有建软链,难道要手工的一个一个执行?当然不用,可以参考下面的脚本

ls -1 /usr/local/Library/LinkedKegs | while read line; do
    echo $LINE
    brew unlink $LINE
    brew link $LINE --force
doneb

上面的脚本是利用了homebrew将LinkedKegs目录设置成了存放所有二进制文件的路径的特点,将其全部取出,删除全部软链,而后重新建立软链。

其实也可以将ls -1 /usr/local/Library/LinkedKegs替换成brew list -1也可。

参考文章

  1. Mac入门(三)使用brew安装软件