ttaka/tmp

d.hatena.ne.jp/ttaka_tmp

Macにgitをインストールする方法

Mac OS Xでgitを使いたい場合、今時は次のどちらかをインストールするのが良いようです。

前者はGUIインストーラーになっていて、お手軽にインストールできます。
後者のHomebrewは、Mac OS X用のパッケージ管理システムで、git以外にも様々なコマンドラインツールをインストールできます。

なお、Macにおけるパッケージ管理システムとしては、他にも

などがありますが、Homebrewは/usr/local以下にインストールされ、PATHを追加せずともコマンドが利用できます。

OSX Installer for Gitをインストールする場合

gitの公式サイトからdmgをダウンロードして、中に入っているpkgをインストールします。

インストール先は /usr/local/git/bin になるため、ターミナル以外のプログラムからも利用できるように、次のスクリプトも実行します。

$ cd /Volumes/Git\ 1.7.4.1\ x86_64\ Leopard/
$ ./setup\ git\ PATH\ for\ non-terminal\ programs.sh 
2011-04-12 12:58:01.125 defaults[10660:903] 
The domain/default pair of (/Users/ttaka/.MacOSX/environment, PATH) does not exist
Variable PATH in ~/.MacOSX/environment.plist changed from '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/bin' to '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/bin:/usr/local/git/bin'
~ /Volumes/Git 1.7.4.1 x86_64 Leopard
/Volumes/Git 1.7.4.1 x86_64 Leopard

これで完了です。

また、アンインストールする場合は、次のスクリプトを実行します。

$ cd /Volumes/Git\ 1.7.4.1\ x86_64\ Leopard/
$ ./uninstall.sh This will uninstall git by removing /usr/local/git/**/*, /etc/paths.d/git, /etc/manpaths.d/git
Type 'yes' if you sure you wish to continue: yes
Password:
Uninstalled

なお、~/.MacOSX/environment.plistはそのまま残るため、不要であれば削除するか内容を書き換える必要があります。

Homebrewのgitをインストールする場合

まずはHomebrewをインストールし、次にgitをインストールします。

Homebrewのインストール

公式ドキュメントの
  https://github.com/mxcl/homebrew/wiki/installation
に書かれている通りに実行します。
また、Xcodeもインストールしておきます。

$ ruby -e "$(curl -fsSLk https://gist.github.com/raw/323731/install_homebrew.rb)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/Formula/...
/usr/local/Library/Homebrew/...
==> The following directories will be made group writable:
/usr/local/.
/usr/local/include
/usr/local/lib
/usr/local/lib/pkgconfig
==> The following directories will have their group set to staff:
/usr/local/.
/usr/local/include
/usr/local/lib
/usr/local/lib/pkgconfig

Press enter to continue
==> /usr/bin/sudo /bin/chmod g+w /usr/local/. /usr/local/include /usr/local/lib /usr/local/lib/pkgconfig
Password:
==> /usr/bin/sudo /usr/bin/chgrp staff /usr/local/. /usr/local/include /usr/local/lib /usr/local/lib/pkgconfig
==> Downloading and Installing Homebrew...
==> Installation successful!

これで完了です。

この際、インストール先のパーミッションが自分に変更されるため、sudoを使わなくともbrewコマンドでパッケージを追加できます。

gitのインストール

ターミナルで、次のコマンドを実行するだけです。

$ brew install git
==> Downloading http://kernel.org/pub/software/scm/git/git-1.7.4.4.tar.bz2
######################################################################## 100.0%
==> make prefix=/usr/local/Cellar/git/1.7.4.4 install
==> Downloading http://kernel.org/pub/software/scm/git/git-manpages-1.7.4.4.tar.
######################################################################## 100.0%
==> Downloading http://kernel.org/pub/software/scm/git/git-htmldocs-1.7.4.4.tar.
######################################################################## 100.0%
==> Caveats
Bash completion and emacs support have been installed.

The rest of the "contrib" folder has been copied to:
  /usr/local/Cellar/git/1.7.4.4/share/contrib
==> Summary
/usr/local/Cellar/git/1.7.4.4: 1054 files, 19M, built in 98 seconds

また、アンインストールする場合は、次のコマンドを実行します。

$ brew uninstall git
Uninstalling /usr/local/Cellar/git/1.7.4.4...

gitの最新バージョンへの追従は、git-osx-installerよりもHomebrewのほうが早く、他のコマンドラインツールをインストールしたい場合に、やはりパッケージ管理システムはあったほうがいいので、Homebrewのgitを入れるのがオススメかと。