# Homebrew クイックリファレンス

*パッケージ管理、cask、tap、サービス、クリーンアップ*

> Source: Homebrew Documentation (brew.sh) · MIT

## インストール

### Homebrew のインストール

```
/bin/bash -c "$(curl -fsSL \
  https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew --version       # インストールの確認
```

### インストール後の設定（Apple Silicon）

```
# add Homebrew to PATH (Apple Silicon default)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' \
  >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
```

### インストールパス

| Command | Description |
|---------|-------------|
| `/opt/homebrew` | Apple Silicon（ARM）プレフィックス |
| `/usr/local` | Intel Mac プレフィックス |
| `/home/linuxbrew/.linuxbrew` | Linux プレフィックス |

## パッケージ管理

### インストールと削除

```
brew install git           # install formula
brew install git@2.40      # install specific version
brew uninstall git         # remove formula
brew reinstall git         # reinstall formula
```

### アップデートとアップグレード

```
brew update                # update Homebrew itself
brew upgrade               # upgrade all packages
brew upgrade git           # upgrade specific package
brew pin git               # prevent auto-upgrade
brew unpin git             # allow upgrade again
```

### インストール済みパッケージの一覧

```
brew list                  # all installed formulae
brew list --cask           # all installed casks
brew leaves                # top-level (not dependencies)
brew deps git              # show dependencies of git
```

## 検索と情報

### パッケージの検索

```
brew search postgres       # search by name
brew search --cask chrome  # search casks only
brew info git              # package details
brew home git              # open homepage in browser
```

### 情報フィールド

| Command | Description |
|---------|-------------|
| `Name / Version` | パッケージ名とインストール済みバージョン |
| `Dependencies` | 必要なパッケージ |
| `Conflicts` | 共存できないパッケージ |
| `Caveats` | インストール後の注意事項（PATH、設定） |
| `Analytics` | インストール数（過去 30/90/365 日） |

## サービス

### サービスの管理

```
brew services list             # all services + status
brew services start postgresql # start and auto-launch
brew services stop postgresql  # stop service
brew services restart nginx    # restart service
brew services run redis        # start without auto-launch
```

### サービスの注意事項

| Command | Description |
|---------|-------------|
| `start` | 今すぐ起動 + ログイン時の自動起動を登録 |
| `run` | 今すぐ起動のみ（自動起動なし） |
| `stop` | 停止して自動起動を解除 |
| `restart` | 停止してから起動 |
| `list` | すべてのサービスとステータスを表示 |
| `Logs` | `~/Library/Logs/Homebrew/` を確認 |

## Cask

### GUI アプリケーションの管理

```
brew install --cask firefox      # install GUI app
brew uninstall --cask firefox     # remove GUI app
brew upgrade --cask               # upgrade all casks
brew list --cask                  # list installed casks
```

### よく使う Cask

| Command | Description |
|---------|-------------|
| `google-chrome` | Chrome ブラウザ |
| `visual-studio-code` | VS Code エディタ |
| `docker` | Docker Desktop |
| `iterm2` | iTerm2 ターミナル |
| `slack` | Slack メッセージング |
| `rectangle` | ウィンドウ管理 |
| `1password` | パスワードマネージャー |
| `raycast` | ランチャー（Spotlight の代替） |

## Tap

### サードパーティリポジトリ

```
brew tap                         # list tapped repos
brew tap hashicorp/tap           # add a tap
brew untap hashicorp/tap         # remove a tap
brew install hashicorp/tap/terraform  # install from tap
```

### 人気の Tap

| Command | Description |
|---------|-------------|
| `homebrew/cask` | GUI アプリ（デフォルトで含まれる） |
| `homebrew/cask-fonts` | フォント: `brew install --cask font-fira-code` |
| `homebrew/bundle` | Brewfile サポート（Homebrew 用 Bundler） |
| `hashicorp/tap` | Terraform、Vault、Consul |

### Brewfile（Bundle）

```
brew bundle dump               # generate Brewfile
brew bundle install            # install from Brewfile
brew bundle cleanup            # remove unlisted packages
```

## クリーンアップ

### ディスク容量の解放

```
brew cleanup                 # remove old versions
brew cleanup -s              # also remove cache
brew cleanup --prune=all     # remove all cached downloads
brew autoremove              # remove unused dependencies
```

### 診断

```
brew doctor                  # check for issues
brew config                  # show Homebrew config
brew missing                 # list missing dependencies
du -sh $(brew --cache)       # check cache size
```

## よくあるパターン

### 日常のワークフロー

| Command | Description |
|---------|-------------|
| `Morning update` | `brew update && brew upgrade && brew cleanup` |
| `New Mac setup` | Brewfile から `brew bundle install` |
| `Export setup` | `brew bundle dump --file=~/Brewfile` |
| `Check health` | `brew doctor` |
| `Find what uses space` | `brew list --formula \| xargs brew info` |
| `Uninstall + deps` | `brew uninstall pkg && brew autoremove` |

### Brewfile の例

```
# ~/Brewfile
tap "homebrew/bundle"
brew "git"
brew "node"
brew "python"
cask "visual-studio-code"
cask "docker"
```
