技術雜談:Homebrew, the missing package manager for OS X

PUBLISHED ON AUG 5, 2014

    Beneath the pretty user interface, OS X is also an Unix. However, OS X lacks a system package manager like APT for Debian and Ubuntu, YUM for Fedora and CentOS and ports for FreeBSD; this situations makes installing source-based software inconvenient. Thanks to Homebrew, OS X has its own package manager now.

    Since Homebrew build and installs software from source, we need a C/C++ compiler here. Get GCC compiler and other tools in Command Line Tools for Xcode at Apple Developer site. Besides, Xquartz is also needed for some X11 applications under OS X. iTerm 2 is not absolutely needed but recommended for better command line experiences.

    After install the above software, you can install Homebrew. To install Homebrew, just paste or type the following command in terminal and press Return key:

    ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

    Homebrew is installed in /usr/local, so you should edit PATH variable, adding /usr/local/bin and /usr/local/sbin in ~/.profile:

    if [ -d /usr/local/bin ]; then
        PATH=/usr/local/bin:$PATH
    fi
    
    if [ -d /usr/local/sbin ]; then
        PATH=/usr/local/sbin:$PATH
    fi

    Homebrew wraps its action in a simple command called brew. After installation of Homebrew, you should check the system environment. To check the environment, type brew doctor in terminal:

    $ brew doctor

    Try to fix the warnings by brew doctor as much as possible. The principle is that you should keep /usr/local and its subdirectories for Homebrew only and installing other software in other location like /opt.

    To update the local repository, type brew update. You should update the repo in the first use each day.

    You can begin to install software after the above two steps. To search software, type brew search package in terminal. Take vim as an example, you can find out vim, macvim and other packages. You can choose between Vim and MacVim.

    $ brew search vim
    macvim    vim    vimpager  vimpc

    To see available options for building package and other information about the chosen package, type brew info package. With the same Vim example:

    $ brew info vim
    (some messages are omitted... )
    ==> Dependencies
    Optional: lua ✘, luajit ✘
    ==> Options
    --disable-nls
    Build vim without National Language Support (translated messages, keymaps)
    --override-system-vi
    Override system vi
    --with-client-server
    Enable client/server mode
    --with-lua
    Build vim with lua support
    --with-luajit
    Build with luajit support
    --with-mzscheme
    Build vim with mzscheme support
    --with-python3
    Build vim with python3 instead of python[2] support
    --with-tcl
    Build vim with tcl support
    --without-perl
    Build vim without perl support
    --without-python
    Build vim without python support
    --without-ruby
    Build vim without ruby support
    --HEAD
    install HEAD version

    Since Homebrew is source-based, you have to compile the software from source. Traditional, we need three steps to install software in Unix are:

    $ ./configure
    $ make
    $ make install

    You do not need to know the details but basic awareness to the process help you to use Homebrew. ./configure generates proper Makefile for later use; make compiles software from source; make install installs software to proper location. Homebrew manages dependencies for you; therefore, you only need to choose among possible options. If you do not know which options are needed, you may also just accept default options.

    Then, install software with brew install package. Say we want to install Vim with Perl support. Homebrew will install all dependent software and libraries automatically.

    $ brew install vim --with-perl

    To uninstall package, type brew uninstall package.

    # remove and purge a package
    $ brew remove --force vim

    To see more usage message of Homebrew, type brew help or man brew in terminal.