コンテンツにスキップ

Homebrew Package Manager

Comprehensive Homebrew commands and workflows for macOS and Linux package management.

Installation & Setup

Install Homebrew

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

# Add to PATH (follow installer instructions)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

Basic Package Operations

Command Description
brew install package_name Install package
brew uninstall package_name Uninstall package
brew upgrade Upgrade all packages
brew upgrade package_name Upgrade specific package
brew list List installed packages
brew outdated List outdated packages

Package Information

Search and Information

Command Description
brew search keyword Search for packages
brew info package_name Show package information
brew list --versions List installed packages with versions
brew deps package_name Show package dependencies
brew uses package_name Show packages that depend on this
brew home package_name Open package homepage

Package Details

Command Description
brew desc package_name Show package description
brew cat package_name Show package formula
brew edit package_name Edit package formula
brew log package_name Show package commit history

Cask Management (GUI Applications)

Cask Operations

Command Description
brew install --cask app_name Install GUI application
brew uninstall --cask app_name Uninstall GUI application
brew list --cask List installed casks
brew search --cask keyword Search for casks
brew info --cask app_name Show cask information
brew upgrade --cask Upgrade all casks
Cask Description
visual-studio-code VS Code editor
google-chrome Chrome browser
docker Docker Desktop
slack Slack messaging
zoom Zoom video conferencing
spotify Spotify music

Services Management

Service Operations

Command Description
brew services list List all services
brew services start service_name Start service
brew services stop service_name Stop service
brew services restart service_name Restart service
brew services run service_name Run service once

Common Services

Service Description
postgresql PostgreSQL database
mysql MySQL database
redis Redis cache
nginx Nginx web server
mongodb-community MongoDB database

Tap Management (Third-party Repositories)

Tap Operations

Command Description
brew tap List tapped repositories
brew tap user/repo Add tap
brew untap user/repo Remove tap
brew tap-info user/repo Show tap information
Tap Description
homebrew/cask-fonts Fonts collection
homebrew/cask-versions Alternative app versions
homebrew/services Service management
mongodb/brew MongoDB packages

System Maintenance

Cleanup Operations

Command Description
brew cleanup Remove old versions
brew cleanup package_name Clean specific package
brew cleanup --prune=all Remove all old downloads
brew autoremove Remove unused dependencies

Health and Diagnostics

Command Description
brew doctor Check system for issues
brew missing Check for missing dependencies
brew config Show Homebrew configuration
brew --version Show Homebrew version

Advanced Usage

Formula Development

Command Description
brew create URL Create new formula
brew edit package_name Edit formula
brew install --build-from-source package Build from source
brew test package_name Test formula

Version Management

Command Description
brew pin package_name Pin package version
brew unpin package_name Unpin package version
brew switch package_name version Switch to specific version
brew list --versions package_name Show available versions

Bundle Management

# Brewfile example
tap "homebrew/bundle"
tap "homebrew/cask"
tap "homebrew/core"

brew "git"
brew "node"
brew "python"
brew "docker"

cask "visual-studio-code"
cask "google-chrome"
cask "slack"

mas "Xcode", id: 497799835
Command Description
brew bundle Install from Brewfile
brew bundle dump Create Brewfile
brew bundle cleanup Uninstall unlisted packages
brew bundle check Check Brewfile status

Configuration

Environment Variables

Variable Description
HOMEBREW_PREFIX Installation prefix
HOMEBREW_CELLAR Package installation directory
HOMEBREW_REPOSITORY Homebrew repository location
HOMEBREW_CACHE Download cache directory

Configuration Files

File Description
~/.homebrew/ User configuration
/opt/homebrew/ Installation directory (Apple Silicon)
/usr/local/ Installation directory (Intel)
Brewfile Package bundle file

Troubleshooting

Common Issues

Problem Solution
Permission errors sudo chown -R $(whoami) /opt/homebrew
Outdated Xcode tools xcode-select --install
Broken symlinks brew doctor && brew cleanup
Formula conflicts brew unlink package && brew link package

Reset and Reinstall

Command Description
brew uninstall --force package_name Force uninstall
brew reinstall package_name Reinstall package
brew link --overwrite package_name Force link
brew prune Remove dead symlinks

Debugging

Command Description
brew --debug install package Debug installation
brew --verbose install package Verbose installation
brew gist-logs package_name Create gist of logs

Performance Optimization

Parallel Operations

# Enable parallel downloads
export HOMEBREW_PARALLEL=4

# Disable analytics
export HOMEBREW_NO_ANALYTICS=1

# Use faster GitHub API
export HOMEBREW_GITHUB_API_TOKEN=your_token

Cache Management

Command Description
brew cleanup --prune=7 Keep 7 days of downloads
du -sh $(brew --cache) Check cache size
rm -rf $(brew --cache) Clear all cache

Security

Package Verification

Command Description
brew audit package_name Audit package formula
brew style package_name Check formula style
brew install --verbose package Show detailed install process

Safe Practices

  1. Regular Updates: Keep Homebrew and packages updated
  2. Trusted Taps: Only add trusted third-party taps
  3. Review Formulas: Check formulas before installation
  4. Backup: Maintain Brewfile for easy restoration
  5. Permissions: Avoid running with sudo

Integration

Shell Integration

# Bash completion
if type brew &>/dev/null; then
  HOMEBREW_PREFIX="$(brew --prefix)"
  if [[ -r "$\\\\{HOMEBREW_PREFIX\\\\}/etc/profile.d/bash_completion.sh" ]]; then
    source "$\\\\{HOMEBREW_PREFIX\\\\}/etc/profile.d/bash_completion.sh"
  fi
fi

# Zsh completion
if type brew &>/dev/null; then
  FPATH="$(brew --prefix)/share/zsh/site-functions:$\\\\{FPATH\\\\}"
  autoload -Uz compinit
  compinit
fi

Development Workflow

# Install development environment
brew install git node python ruby go rust
brew install --cask visual-studio-code docker

# Database setup
brew install postgresql redis
brew services start postgresql
brew services start redis

# Create project
mkdir myproject && cd myproject
npm init -y

Best Practices

Package Management

  1. Regular Maintenance: Run brew update && brew upgrade weekly
  2. Cleanup: Regular cleanup of old versions and cache
  3. Bundle Files: Use Brewfile for team consistency
  4. Pin Critical: Pin packages that need specific versions
  5. Documentation: Document custom taps and configurations

Development Environment

  1. Version Managers: Use version managers for languages (nvm, pyenv, rbenv)
  2. Virtual Environments: Isolate project dependencies
  3. Service Management: Use brew services for development databases
  4. Path Management: Keep PATH clean and organized
  5. Backup Strategy: Regular Brewfile dumps and dotfile backups

Performance

  1. Parallel Downloads: Enable parallel operations
  2. Cache Management: Regular cache cleanup
  3. Analytics: Disable analytics for faster operations
  4. GitHub Token: Use GitHub token for API rate limits
  5. SSD Optimization: Store cache on fastest storage