The Go Blog

Homebrew

bantana
24 June 2015

introduction

homebrew是一套在mac下管理开源软件的命令行工具脚本;这个是用ruby写的,主要由Formula和homebrew工具组成;

来看一个官方例子感受下Formula:

brew edit wget

class Wget < Formula
  homepage "https://www.gnu.org/software/wget/"
  url "https://ftp.gnu.org/gnu/wget/wget-1.15.tar.gz"
  sha256 "52126be8cf1bddd7536886e74c053ad7d0ed2aa89b4b630f76785bac21695fcd"

  def install
    system "./configure", "--prefix=#{prefix}"
    system "make", "install"
  end
end

做过源码编译安装的是不是很熟悉啊,这个就是把源码下载,校验,configure,make ,make install, 还可以打个patch什么的这些过程都自动帮你做了;

requirement

  • intel cpu
  • osx 10.6 or high
  • Command line tools for xcode: xcode-select --install
  • bash or zsh
  • ruby

install

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

上面的都干了些啥:

一个ruby script:

-rwxr-xr-x  1 bantana  admin  791 May  9 18:36 /usr/local/bin/brew

/usr/local/

/usr/local
├── .git
├── .gitignore
├── .yardopts
├── CODEOFCONDUCT.md
├── CONTRIBUTING.md
├── Cellar
├── Frameworks
├── LICENSE.txt
├── Library
├── README.md
├── SUPPORTERS.md
├── bin
├── etc
├── include
├── lib
├── opt
├── share
└── var

11 directories, 7 files

git remote -v

origin    https://github.com/Homebrew/homebrew (fetch)
origin    https://github.com/Homebrew/homebrew (push)

git branch -a

* master                  1395ab8 Fix audit when tap is nil
  remotes/origin/gh-pages 6305d2d Redirect to brew.sh.
  remotes/origin/go       e4e1507 install: point to new installer location.
  remotes/origin/master   1395ab8 Fix audit when tap is nil

usage

$ brew

Example usage:
  brew [info | home | options ] [FORMULA...]
  brew install FORMULA...
  brew uninstall FORMULA...
  brew search [foo]
  brew list [FORMULA...]
  brew update
  brew upgrade [--all | FORMULA...]
  brew pin/unpin [FORMULA...]

Troubleshooting:
  brew doctor
  brew install -vd FORMULA
  brew [--env | config]

Brewing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]
  https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Formula-Cookbook.md

Further help:
  man brew
  brew home

首次安装你可能需要

$ brew doctor

一般使用方式

update:

$ brew update

Already up-to-date.

homebrew的Formula更新非常快,每天到每周的都有更新,一般情况下都会使用最新的包,所以每次install之前都建议做brew update;

search:

$ brew search autojump

autojump

info:

$ brew info autojump

autojump: stable 22.2.4, HEAD
Shell extension to jump to frequently used directories
https://github.com/joelthelion/autojump
/usr/local/Cellar/autojump/22.2.4 (23 files, 376K) *
  Built from source
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/autojump.rb
==> Caveats
Add the following line to your ~/.bash_profile or ~/.zshrc file (and remember
to source the file to update your current session):
  [[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh

If you use the Fish shell then add the following line to your ~/.config/fish/config.fish:
  [ -f /usr/local/share/autojump/autojump.fish ]; and . /usr/local/share/autojump/autojump.fish


zsh completion has been installed to:
  /usr/local/share/zsh/site-functions

install:

$ brew install autojump

where is my package install:

$ brew install autojump

/usr/local/Cellar
├── autojump
│   └── 22.2.4

list package file:

$ brew list autojump

/usr/local/Cellar/autojump/22.2.4/AUTHORS
/usr/local/Cellar/autojump/22.2.4/bin/autojump
/usr/local/Cellar/autojump/22.2.4/CHANGES.md
/usr/local/Cellar/autojump/22.2.4/etc/autojump.sh
/usr/local/Cellar/autojump/22.2.4/etc/profile.d/autojump.sh
/usr/local/Cellar/autojump/22.2.4/INSTALL_RECEIPT.json
/usr/local/Cellar/autojump/22.2.4/libexec/bin/__pycache__/autojump_argparse.cpython-34.pyc
/usr/local/Cellar/autojump/22.2.4/libexec/bin/__pycache__/autojump_data.cpython-34.pyc
/usr/local/Cellar/autojump/22.2.4/libexec/bin/__pycache__/autojump_utils.cpython-34.pyc
/usr/local/Cellar/autojump/22.2.4/libexec/bin/autojump
/usr/local/Cellar/autojump/22.2.4/libexec/bin/autojump_argparse.py
/usr/local/Cellar/autojump/22.2.4/libexec/bin/autojump_argparse.pyc
/usr/local/Cellar/autojump/22.2.4/libexec/bin/autojump_data.py
/usr/local/Cellar/autojump/22.2.4/libexec/bin/autojump_data.pyc
/usr/local/Cellar/autojump/22.2.4/libexec/bin/autojump_utils.py
/usr/local/Cellar/autojump/22.2.4/libexec/bin/autojump_utils.pyc
/usr/local/Cellar/autojump/22.2.4/LICENSE
/usr/local/Cellar/autojump/22.2.4/README.md
/usr/local/Cellar/autojump/22.2.4/share/autojump/autojump.bash
/usr/local/Cellar/autojump/22.2.4/share/autojump/autojump.fish
/usr/local/Cellar/autojump/22.2.4/share/autojump/autojump.zsh
/usr/local/Cellar/autojump/22.2.4/share/autojump/icon.png
/usr/local/Cellar/autojump/22.2.4/share/man/man1/autojump.1
/usr/local/Cellar/autojump/22.2.4/share/zsh/site-functions/_j

check outdated:

$ brew outdated

docker (1.6.2 < 1.7.0)
node (0.12.4 < 0.12.5)
plt-racket (6.1.1 < 6.2)
wireshark (1.12.4, 1.12.5 < 1.12.6)
youtube-dl (2015.05.29, 2015.06.04.1 < 2015.06.15)

upgrade choise:

$ brew upgrade docker

upgrade all:

$ brew upgrade

switch version:

有时候需要保留不同的版本,他们可能不兼容,比如docker 1.6.2 , 1.7.0 就是这样:

docker(1.6.2, 1.7.0)

$ brew info docker

docker: stable 1.7.0 (bottled)
Pack, ship and run any application as a lightweight container
https://www.docker.com/
/usr/local/Cellar/docker/1.6.2 (9 files, 7.3M)          // version 1.6.2
  Poured from bottle
/usr/local/Cellar/docker/1.7.0 (9 files, 7.4M) *        // version 1.7.0 * 号表示当前版本
  Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/docker.rb
==> Dependencies
Build: go ✘
==> Options
--without-completions
    Disable bash/zsh completions
==> Caveats

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completion has been installed to:
  /usr/local/share/zsh/site-functions

switch to version 1.6.2:

$ brew switch docker 1.6.2

switch to version 1.7.0:

$ brew switch docker 1.7.0

cleanup:

上面看到这个是不是有些奇怪,

wireshark (1.12.4, 1.12.5 < 1.12.6)

这个formula保留了不同的版本,执行brew upgrade后做cleanup动作会清理掉多余的版本,只保留最新的。

$ brew cleanup wireshark

慎用,这个会清理所有的低版本,

$ brew cleanup

如果不慎清理掉了不兼容的版本,比如上面的docker 1.6.2, brew没有提供命令帮你恢复这个,不过可以用git找回历史的Formula

$ cd /usr/local/Library/Forumla
$ git log docker.rb |grep -i 1.6.2 -4

commit 0c3af2daa8e3e7f66d99e1c7a7c1a14a6eef5963
Author: BrewTestBot <brew-test-bot@googlegroups.com>
Date:   Thu May 14 11:30:01 2015 +0100

    docker: update 1.6.2 bottle.

commit 67e40c1b4baebc0db236f2136e98e72b8e9d468f
Author: PikachuEXE <pikachuexe@gmail.com>
Date:   Thu May 14 11:56:44 2015 +0800
--
--
commit 67e40c1b4baebc0db236f2136e98e72b8e9d468f
Author: PikachuEXE <pikachuexe@gmail.com>
Date:   Thu May 14 11:56:44 2015 +0800

    docker, boot2docker 1.6.2

    Closes #39743.

    Signed-off-by: Mike McQuaid <mike@mikemcquaid.com>

然后用git checkout获取历史的docker.rb文件,用这个commit 0c3af2daa8e3e7f66d99e1c7a7c1a14a6eef5963

$ git checkout 0c3af2daa8e3e7f66d99e1c7a7c1a14a6eef5963 docker.rb

这样你得到了1.6.2 bottle的docker.rb, 然后安装1.6.2的docker

$ brew install docker

恢复git目录下的docker.rb到最新:

$ git checkout HEAD^ docker.rb

然后可以在需要的时候brew switch到不同版本的docker工作.

need more:

$ man brew

tips

$ cat /usr/local/Library/brew.rb

aliases = {'ls' => 'list',
           'homepage' => 'home',
           '-S' => 'search',
           'up' => 'update',
           'ln' => 'link',
           'instal' => 'install', # gem does the same
           'rm' => 'uninstall',
           'remove' => 'uninstall',
           'configure' => 'diy',
           'abv' => 'info',
           'dr' => 'doctor',
           '--repo' => '--repository',
           'environment' => '--env',
           '--config' => 'config',
           }

官方的Formula

我要修改我的编译安装选项:

brew edit
brew edit [FORMULA...]

如何建立自己的brew 安装包: 以google snappy为例:

$ brew create https://github.com/google/snappy/archive/master.zip --set-name mysnappy

官方snappy已有,需要用--set-name 来建立自己的包名mysnappy

file /usr/local/Library/Formula/mysnappy.rb

# Documentation: https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Formula-Cookbook.md
#                /usr/local/Library/Contributions/example-formula.rb
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!

class Mysnappy < Formula
  desc ""
  homepage ""
  url "https://github.com/google/snappy/archive/master.zip"
  sha256 ""

  # depends_on "cmake" => :build
  depends_on :x11 # if your formula requires any X11/XQuartz components

  def install
    # ENV.deparallelize  # if your formula fails when building in parallel

    # Remove unrecognized options if warned by configure
    system "./configure", "--disable-debug",
                          "--disable-dependency-tracking",
                          "--disable-silent-rules",
                          "--prefix=#{prefix}"
    # system "cmake", ".", *std_cmake_args
    system "make", "install" # if this fails, try separate make/make install steps
  end

  test do
    # `test do` will create, run in and delete a temporary directory.
    #
    # This test will fail and we won't accept that! It's enough to just replace
    # "false" with the main program this formula installs, but it'd be nice if you
    # were more thorough. Run the test with `brew test mysnappy`. Options passed
    # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
    #
    # The installed folder is not in the path, so use the entire path to any
    # executables being tested: `system "#{bin}/program", "do", "something"`.
    system "false"
  end
end

把上述文件修改为你需要的Formula。