homebrew的常见用法

工欲善其事,必先利其器.homebrew是mac上的包管理工具,类似于ubuntu的apt-get,centos的yum.
但是在日常使用的过程中常常搞不懂homebrew的一些命令用法,所以首先需要了解一下homebrew的设计理念.

homebrew安装目录为/usr/local/Cellar/,然后将命令软连接到/usr/local/bin/目录下,如笔者电脑上的wget命令

1
2
$ls -l /usr/local/bin/wget
lrwxr-xr-x  1 lu  admin  32 Oct 23  2017 /usr/local/bin/wget -> ../Cellar/wget/1.19.1_1/bin/wget

brew install

使用brew install wget这样的命令的背后到底是什么?
这涉及到formula的概念,formula是包定义,使用ruby语言书写,位置在/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/目录,所以安装wget实际上homebrew是到这个目录下解析这个文件,然后按照文件的描述去下载和安装包. 可以使用brew edit <formula>来编辑某个包的定义文件

brew cask

有时候常常会遇到brew cask install atom这样的命令,那么cask又是什么东东?

cask是对homebrew的扩展,cask用来从命令行安装应用程序,用户不需要使用图形化界面也可安装应用程序。 为确保描述准确,加上引文来源,来自man brew

Cask is a tool for installing precompiled macOS binaries (such as Applications) from the command line. The user is never required to use the graphical user interface.

brew tap

brew tap 用来添加更多的formula仓库,tap默认仓库来源为GitHub,但是具体地址也可以通过URL指定。

brew tap adds more repositories to the list of formulae that brew tracks, updates, and installs from. By default, tap assumes that the repositories come from GitHub, but the command isn’t limited to any one location.

brew tap命令用来查看tapped repository

brew tap without arguments lists the currently tapped repositories.

brew tap _user_/_repo_命令用来下载指定GitHub仓库的formula到本地

举例1:
brew tap caskroom/versions
brew cask install java8
这个操作用来安装java8.
caskroom表示github用户名
version表示该用户下的仓库名
该命令自动去https://github.com/caskroom/homebrew-versions这个位置下载formula文件

举例2:
brew tap AdoptOpenJDK/openjdk
brew install adoptopenjdk-openjdk8
这个操作用来安装openJDK
这个命令会自动去https://github.com/AdoptOpenJDK/homebrew-openjdk这个位置下载formula文件,然后使用brew install就可以正常安装了

brew术语

然后有一个很有意思的地方,这些词都是和自酿啤酒有关联的,但是他们该如何关联起来呢

术语 释义
homebrew 自酿啤酒
formula 配方
Cellar 酒窖
cask 酒桶
tap 动词,取酒

参考:
https://docs.brew.sh/Formula-Cookbook
https://docs.brew.sh/Taps
https://github.com/Homebrew/homebrew-cask