macOS | Homebrewパッケージマネージャーのインストールと使い方

macOSでHomebrewをインストールし、よくあるインストール問題、基本的なbrewコマンド、設定確認、削除方法を扱う

Homebrewとは?

Homebrewをインストールすると、パッケージのバージョン管理がしやすくなります。
UbuntuやCentOSにはapt-getyumのようなパッケージマネージャーがありますが、macOSにはこのようなパッケージマネージャーが標準では用意されていないため、自分でインストールする必要があります。

macOS用パッケージマネージャー — Homebrew

Homebrewのインストール

ターミナルで次のコマンドを入力して実行します。

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

新しいインストールコマンド:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

インストール成功

==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew

Press RETURN to continue or any other key to abort
==> Downloading and installing Homebrew...
remote: Counting objects: 95726, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 95726 (delta 3), reused 8 (delta 3), pack-reused 95717
Receiving objects: 100% (95726/95726), 21.86 MiB | 127.00 KiB/s, done.
Resolving deltas: 100% (69501/69501), done.
From https://github.com/Homebrew/brew
 * [new branch]          master     -> origin/master
 * [new tag]             0.1        -> 0.1
 * [new tag]             0.2        -> 0.2

... 省略 ...

 * [new tag]             1.4.0      -> 1.4.0
HEAD is now at b4d43e950 Merge pull request #3401 from DomT4/vendor_or_na
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 4673, done.
remote: Compressing objects: 100% (4415/4415), done.
remote: Total 4673 (delta 62), reused 1098 (delta 46), pack-reused 0
Receiving objects: 100% (4673/4673), 3.81 MiB | 116.00 KiB/s, done.
Resolving deltas: 100% (62/62), done.
Tapped 4445 formulae (4,720 files, 12.0MB)
==> Cleaning up /Library/Caches/Homebrew...
==> Migrating /Library/Caches/Homebrew to /Users/sanghopark/Library/Caches/Homebrew...
==> Deleting /Library/Caches/Homebrew...
Already up-to-date.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics.html

==> Next steps:
- Run `brew help` to get started
- Further documentation:
    https://docs.brew.sh

インストール失敗

Xcodeをインストールしたが同意していないため発生する問題

インストール時に次のようなエラーが表示され、インストールできないことがあります。

Press RETURN to continue or any other key to abort
You have not agreed to the Xcode license.
Before running the installer again please agree to the license by opening
Xcode.app or running:
    sudo xcodebuild -license

これは、Xcodeをインストールしたもののライセンスに同意していないために発生します。この場合はXcodeを初回起動し、Agreeをクリックします。

Warning: /opt/homebrew/bin is not in your PATH

インストール時に次のようなメッセージが表示され、brewを実行できないことがあります。

==> Downloading and installing Homebrew...
HEAD is now at 98999cc51 Merge pull request #13754 from Rylan12/cask-artifacts-hash
Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.

これは、/opt/homebrew/bin is not in your PATHというメッセージのとおり、まだ/opt/homebrew/binPATHに登録されていないために発生します。/opt/homebrew/binを登録すれば解決できます。

ターミナルで次のように入力します。

echo 'export PATH=/opt/homebrew/bin:$PATH' >> ~/.zshrc

Homebrewの使い方

バージョン確認

$ brew -v
Homebrew 1.3.1
Homebrew/homebrew-core (git revision d6b4; last commit 2017-08-16)

パッケージのインストール

$ brew install {プログラム名}

パッケージの無効化と有効化

$ brew unlink {プログラム名} # 一時的に無効化
$ brew link {プログラム名}   # 有効化

パッケージ一覧の更新

$ brew update   # formulaを更新
$ brew upgrade  # 更新対象パッケージを再ビルド

インストール済み一覧の表示

$ brew list

パッケージの削除

$ brew remove {プログラム名}

Homebrew設定一覧

$ brew --config
HOMEBREW_VERSION: 1.3.1
ORIGIN: https://github.com/Homebrew/brew
HEAD: 69799d97b1e7314912b2ee234dec2c179c5fb969
Last commit: 9 days ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: d6b4b6ba05bbff6a3731f80580ebee7574bf6494
Core tap last commit: 2 hours ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_REPOSITORY: /usr/local/Homebrew
HOMEBREW_CELLAR: /usr/local/Cellar
HOMEBREW_BOTTLE_DOMAIN: https://homebrew.bintray.com
CPU: quad-core 64-bit sandybridge
Homebrew Ruby: 2.0.0-p481
Clang: 7.0 build 700
Git: 2.4.9 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Perl: /usr/bin/perl
Python: /usr/bin/python
Ruby: /usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
Java: 1.8.0_121
macOS: 10.10.5-x86_64
Xcode: 7.1.1
CLT: N/A
X11: N/A

Homebrewの削除

$ cd `brew --prefix`
$ rm -rf Cellar
$ brew prune
$ rm `git ls-files`
$ rmdir Library/Homebrew Library/Aliases Library/Formula Library/Contributions
$ rm -rf .git
$ rm -rf ~/Library/Caches/Homebrew

参考