# Tham Khảo Nhanh Homebrew

*Quản lý gói, casks, taps, services, cleanup*

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

## Cài Đặt

### Cài Đặt Homebrew

```
/bin/bash -c "$(curl -fsSL \
  https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew --version       # verify installation
```

### Sau Cài Đặt (Apple Silicon)

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

### Đường Dẫn Cài Đặt

| Command | Description |
|---------|-------------|
| `/opt/homebrew` | Tiền tố Apple Silicon (ARM) |
| `/usr/local` | Tiền tố Intel Mac |
| `/home/linuxbrew/.linuxbrew` | Tiền tố Linux |

## Quản Lý Gói

### Cài Đặt & Gỡ Bỏ

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

### Cập Nhật & Nâng Cấp

```
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
```

### Liệt Kê Đã Cài

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

## Tìm Kiếm & Thông Tin

### Tìm Gói

```
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
```

### Trường Thông Tin

| Command | Description |
|---------|-------------|
| `Name / Version` | Tên và phiên bản đã cài |
| `Dependencies` | Gói phụ thuộc bắt buộc |
| `Conflicts` | Gói không thể cùng tồn tại |
| `Caveats` | Ghi chú sau cài đặt (PATH, config) |
| `Analytics` | Số lần cài đặt (30/90/365 ngày qua) |

## Services

### Quản Lý Services

```
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
```

### Ghi Chú Service

| Command | Description |
|---------|-------------|
| `start` | Khởi động ngay + đăng ký tự khởi động khi đăng nhập |
| `run` | Chỉ khởi động ngay (không tự khởi động) |
| `stop` | Dừng và hủy đăng ký khởi động |
| `restart` | Dừng rồi khởi động lại |
| `list` | Hiển thị tất cả services và trạng thái |
| `Logs` | Kiểm tra `~/Library/Logs/Homebrew/` |

## Casks

### Quản Lý Ứng Dụng 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
```

### Casks Phổ Biến

| Command | Description |
|---------|-------------|
| `google-chrome` | Trình duyệt Chrome |
| `visual-studio-code` | Editor VS Code |
| `docker` | Docker Desktop |
| `iterm2` | Terminal iTerm2 |
| `slack` | Nhắn tin Slack |
| `rectangle` | Quản lý cửa sổ |
| `1password` | Quản lý mật khẩu |
| `raycast` | Launcher (thay thế Spotlight) |

## Taps

### Kho Bên Thứ Ba

```
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
```

### Taps Phổ Biến

| Command | Description |
|---------|-------------|
| `homebrew/cask` | Ứng dụng GUI (mặc định đã có) |
| `homebrew/cask-fonts` | Fonts: `brew install --cask font-fira-code` |
| `homebrew/bundle` | Hỗ trợ Brewfile (Bundler cho Homebrew) |
| `hashicorp/tap` | Terraform, Vault, Consul |

### Brewfile (Bundle)

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

## Dọn Dẹp

### Giải Phóng Dung Lượng Đĩa

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

### Chẩn Đoán

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

## Mẫu Phổ Biến

### Quy Trình Hàng Ngày

| Command | Description |
|---------|-------------|
| `Morning update` | `brew update && brew upgrade && brew cleanup` |
| `New Mac setup` | `brew bundle install` từ Brewfile |
| `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` |

### Ví Dụ Brewfile

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