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

Facebook Twitter LinkedIn LINE Skype EverNote GMail Yahoo Email

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)"
{{< / highlight >}}

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

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

if [ -d /usr/local/sbin ]; then
    PATH=/usr/local/sbin:$PATH
fi
{{< / highlight >}}

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:

```console
$ brew doctor
{{< / highlight >}}

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.

```console
$ brew search vim
macvim    vim    vimpager  vimpc
{{< / highlight >}}

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

```console
$ 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
{{< / highlight >}}

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

```console
$ ./configure
$ make
$ make install
{{< / highlight >}}

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.

```console
$ brew install vim --with-perl
{{< / highlight >}}

To uninstall package, type `brew uninstall` *package*.

```console
# remove and purge a package
$ brew remove --force vim
{{< / highlight >}}

To see more usage message of Homebrew, type `brew help` or `man brew` in terminal.
關於作者

身為資訊領域碩士,美思認為開發應用程式的目的是為社會帶來價值。如果在這個過程中該軟體能成為永續經營的項目,那就是開發者和使用者雙贏的局面。

美思喜歡用開源技術來解決各式各樣的問題,但必要時對專有技術也不排斥。閒暇之餘,美思將所學寫成文章,放在這個網站上和大家分享。