諸事情*1により MacPorts から Homebrew に移行した。以下の三つの作業を行なった。

  1. MacPorts 関係のバイナリを使うのをやめる
  2. MacPorts を消す
  3. Homebrew を入れる

1. MacPorts 関係のバイナリを使うのをやめる

MacPorts で入れた zsh を使うのをやめる

chsh する。

$ chsh
Changing shell for nulll.
Password for nulll: 

$EDITOR で以下が開くので、Shell を /bin/zsh 等に変更する。

# Changing user information for nulll.
# Use "passwd" to change the password.
##
# Open Directory: /Local/Default
##
Shell: /bin/zsh
Full Name: nulll
Office Location:
Office Phone:
Home Phone:

/etc/shells から /opt/local/bin/zsh を削除しておく。

$PATH から /opt/local/bin を外す

.zshrc を修正する。

2. MacPorts を消す

MacPorts の公式ドキュメントに消しかた http://guide.macports.org/chunked/installing.macports.uninstalling.html が書いてあったので、だいたいこの通りやった。関係するディレクトリを消した後、

$ mdfind macports

で、引っかかったものも消した。ログを見ると

$ sudo rm -rf /private/var/db/receipts/org.macports.MacPorts.*

とか、やったらしい。

3. Homebrew を入れる

好きな場所に Homebrew を入れる

とりあえず Xcode を入れるのは当然として、Homebrew の公式ドキュメント https://github.com/mxcl/homebrew/wiki/installation を見ると

$ ruby -e "$(curl -fsSLk https://gist.github.com/raw/323731/install_homebrew.rb)"

を実行する、と書いてあるけど、これだと問答無用で /usr/local に入れられる(た)ので、やめる。代わりに Homebrew を入れたいディレクトリを適当に作って移動した後、github から tarball を取得して展開する。個人的に、$HOME 以下に入れたかったので、以下のようにした。

$ mkdir ~/homebrew
$ cd ~/homebrew
$ curl -LsSf https://github.com/mxcl/homebrew/tarball/master | /usr/bin/tar xvz --strip 1

その後 .zshrc を修正するなどして、展開したディレクトリの bin(上記例だと $HOME/homebrew/bin) を $PATH に加える。以降、パスを通した brew コマンドを使って install したアプリケーションやライブラリは、パスを通した brew コマンドの親ディレクトリ以下の bin とか lib に入る(上記例だと $HOME/homebrew/bin とか $HOME/homebrew/lib)。

Homebrew を使えるようにする

とりあえず update しようとすると、

$ brew update
Please `brew install git' first.

まずは git を install しろ、と言われるので、git を入れる。

$ brew install git
==> Downloading http://kernel.org/pub/software/scm/git/git-1.7.4.1.tar.bz2
######################################################################## 100.0%
==> make prefix=/Users/nulll/homebrew/Cellar/git/1.7.4.1 install
...

git を入れた後は brew update で Homebrew を更新する。

$ brew update

Initialized empty Git repository in /Users/nulll/homebrew/.git/
remote: Counting objects: 33487, done.
remote: Compressing objects: 100% (15498/15498), done.
remote: Total 33487 (delta 19204), reused 30271 (delta 17435)
Receiving objects: 100% (33487/33487), 4.55 MiB | 1.15 MiB/s, done.
Resolving deltas: 100% (19204/19204), done.
From http://github.com/mxcl/homebrew
 * branch            master     -> FETCH_HEAD
Updated Homebrew from TAIL to 406c3050.
No formulae were updated.
No external commands were updated.

Library 以下と、brew コマンド本体と man が git で管理されているらしい。

$ cd ~/homebrew
$ git ls-files
.gitignore
Library/Aliases/0mq
...
Library/Contributions/brew_bash_completion.sh
...
Library/Formula/a2ps.rb
...
Library/Homebrew/LICENSE
...
README.md
bin/brew
share/man/man1/brew-man.1
share/man/man1/brew.1
zshbrew コマンドの補完

homebrew を入れたディレクトリの、Library/Contributions に brew_zsh_completion.zsh という、zsh 用の completion ファイルがあったので、http://d.hatena.ne.jp/hagure_beans/20101211/1292081391 を参考にして使えるようにした。ls するとわかるけど、bash とか fish 用のものもある。

$ ls ~/homebrew/Library/Contributions
brew_bash_completion.sh         brew_zsh_completion.zsh         manpages
brew_fish_completion.fish       examples

シンボリックリンクして、

$ ln -s ~/homebrew/Library/Contributions/brew_zsh_completion.zsh ~/.zsh/completion/_homebrew

以下のように .zshrc を修正する。

...

fpath=(~/.zsh/completions ${fpath})
if [ -d ~/.zsh/cache ]; then
    zstyle ':completion:*' use-cache yes
    zstyle ':completion:*' cache-path ~/.zsh/cache
fi
autoload -U compinit
compinit

...

適当に shell を開き直したり、.zshrc を再読み込みした後、brew 以降の補完が効くようになる。

$ source ~/.zshrc
$ brew [TAB]
cat       -- display formula file for a formula
cleanup   -- uninstall unused and old versions of packages
create    -- create a new formula
deps      -- list dependencies and dependants of a formula
doctor    -- audits your installation for common issues
edit      -- edit a formula
home      -- visit the homepage of a formula or the brew project
info      -- information about a formula
install   -- install a formula
link      -- link a formula
list      -- list files in a formula or not-installed formulae
log       -- git commit log for a formula
missing   -- check all installed formuale for missing dependencies.
outdated  -- list formulas for which a newer version is available
prune     -- remove dead links
remove    -- remove a formula
search    -- search for a formula (/regex/ or string)
server    -- start a local web app that lets you browse formulae (requires Sinatra)
unlink    -- unlink a formula
update    -- freshen up linksuses      -- show formulas which depend on a formula
$ brew u[TAB]
unlink    -- unlink a formula
update    -- freshen up links
uses      -- show formulas which depend on a formula

まとめ

とりあえず MacPorts を捨てて、Homebrew を使えるようになった、が、 Formula が git で管理されているということは、自分で勝手に新しい Formula を追加したり、自分で勝手に既存の Fromula を書き換えたりすると brew update 時に conflict する可能性があるということなので、ここらへんをうまくやりたいところである。

*1:作業用マシンを自分以外の人間と微妙に共有しているという状況で MacPorts を使っていたが、MacPorts だと /opt/local という共有エリアに色々入るので、勝手に $ sudo port -u upgrade outdated とかするとバージョンの関係で自分以外の人の何かが動かなくなったりしそうでこわい、かと言って自分が使ってるものだけ選んで upgrade とかめんどい、ので、個人エリアに色々入れて管理するようにしたい