Package Managers: APT, Homebrew, Chocolatey Commands & Best Practices
Package managers automate the installation, upgrade, configuration, and removal of software. They resolve dependencies, verify integrity, and provide a consistent interface across thousands of packages. Every major operating system has at least one package manager, and understanding how to use them effectively is essential for efficient software development. This guide covers the three most widely used package managers — APT, Homebrew, and Chocolatey — with practical commands, workflows, and best practices.
APT: The Debian and Ubuntu Standard
APT (Advanced Package Tool) is the default package manager on Debian-based Linux distributions. It works with .deb packages from configured repositories and handles dependency resolution automatically.
Essential Commands
# Update package index from all configured repositories
sudo apt update
# Upgrade all installed packages to the latest versions
sudo apt upgrade
# Full upgrade (handles changing dependencies)
sudo apt full-upgrade
# Install a package and its dependencies
sudo apt install build-essential python3-dev
# Remove a package (keep configuration files)
sudo apt remove nginx
# Remove package and configuration files completely
sudo apt purge nginx
# Remove unused dependencies
sudo apt autoremove
# Search for packages matching a keyword
apt search python
# Show detailed package information
apt show python3
# List installed packages
apt list --installedManaging Repositories
PPAs (Personal Package Archives) provide packages not available in the default repositories:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.12Third-party repositories require manual configuration. For example, adding the official PostgreSQL repository:
echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt updateVersion Pinning
Control which version of a package is installed using APT pinning. Create a preferences file:
# /etc/apt/preferences.d/pin-nodejs
Package: nodejs
Pin: version 18.*
Pin-Priority: 1001A higher Pin-Priority means APT prefers that version. Values above 1000 force the version even for upgrades.
APT Best Practices
Always run apt update before apt install to avoid installing stale versions. Use apt list --upgradable to review pending upgrades. Avoid apt upgrade in production without testing — automatic updates can introduce breaking changes. Pin major versions of critical tools like Node.js, Python, and PostgreSQL.
Homebrew: macOS and Linux Package Management
Homebrew is the most popular package manager for macOS and also runs on Linux (as Linuxbrew). It installs packages to its own directory (/usr/local on Intel Macs, /opt/homebrew on Apple Silicon) and symlinks binaries into the PATH.
Core Commands
# Update Homebrew and package definitions
brew update
# Install a package
brew install wget
# Install a specific version (if available in a tap)
brew install postgresql@16
# Upgrade a package
brew upgrade wget
# Remove a package and its dependencies
brew uninstall wget
# List installed packages
brew list
# Search for packages
brew search python
# Show package info, dependencies, and caveats
brew info python
# Show outdated packages
brew outdated
# Clean up old versions and cached downloads
brew cleanupHomebrew Cask: GUI Applications
Cask extends Homebrew to install native macOS applications:
# Install GUI applications
brew install --cask visual-studio-code
brew install --cask docker
brew install --cask google-chrome
# List installed casks
brew list --cask
# Upgrade all casks
brew upgrade --caskCasks download the application DMG or ZIP, mount it, copy the app to /Applications, and clean up — all without manual clicking.
Taps: Third-Party Repositories
Taps extend Homebrew’s package collection beyond the core repository:
# Add a tap
brew tap homebrew/cask-versions
# Install from a tap
brew install --cask homebrew/cask-versions/firefox-developer-edition
# List all taps
brew tapBrewfile: Reproducible Environments
Define your development environment in a Brewfile — a declarative Ruby DSL:
tap "homebrew/bundle"
tap "homebrew/cask"
brew "git"
brew "python@3.12"
brew "node"
brew "postgresql@16"
cask "visual-studio-code"
cask "docker"Install everything with brew bundle. Check the system against the Brewfile with brew bundle check. This enables team-wide environment consistency — commit the Brewfile to the repository.
Homebrew Best Practices
Run brew doctor to diagnose common problems. Use brew pin to prevent specific packages from being upgraded. Run brew cleanup regularly to free disk space. For team environments, maintain a Brewfile in version control. Use brew services start postgresql@16 for launchd integration.
Chocolatey: Windows Package Automation
Chocolatey brings package management to Windows, automating installation through PowerShell:
# Install Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol::3072
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))Core Commands
# Install a package
choco install git
# Install a specific version
choco install python --version=3.12.0
# Upgrade a package
choco upgrade git
# Uninstall a package
choco uninstall git
# List installed packages
choco list --local-only
# Search for packages
choco search nodejs
# Show package information
choco info nodejs
# Update all packages
choco upgrade allPackage Options and Custom Arguments
Chocolatey supports installer-specific parameters:
# Install with parameters
choco install nodejs --params "/InstallDir:C:\tools\nodejs"
# Pass arguments to the installer
choco install vscode --install-arguments "/SILENT /NORESTART"
# Suppress prompts (assume yes)
choco install ffmpeg -yThe -y flag is essential for automation — without it, Chocolatey prompts for confirmation.
Package Manager Automation Scripts
Automated Environment Setup
Package managers enable reproducible development environments through declarative configuration files:
#!/bin/bash
# setup-dev.sh — Automated environment setup
set -euo pipefail
echo "Setting up development environment..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo apt update
sudo apt install -y build-essential curl git python3 python3-pip
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew bundle --file=Brewfile
elif [[ "$OSTYPE" == "msys" ]]; then
choco install git python nodejs -y
fi
echo "Development environment ready."This pattern — detecting the OS and invoking the appropriate package manager — is the foundation of cross-platform developer onboarding scripts used by organizations like Google, Meta, and Shopify.
CI/CD Integration
Package managers are essential in CI/CD pipelines. GitHub Actions provides setup actions for common languages, but package manager caching dramatically improves build times:
# .github/workflows/ci.yml
- name: Cache Homebrew
uses: actions/cache@v3
with:
path: ~/Library/Caches/Homebrew
key: brew-${{ hashFiles('Brewfile.lock.json') }}
restore-keys: brew-For Linux CI runners, APT caching through apt-cacher-ng or Docker layer caching reduces dependency installation from minutes to seconds.
Docker Integration
Package managers are the primary tool for installing dependencies in Docker images:
FROM ubuntu:24.04 AS builder
RUN apt update && apt install -y \
build-essential \
cmake \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*
FROM python:3.12-slim
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txtCleaning APT cache (rm -rf /var/lib/apt/lists/*) in the same RUN layer reduces image size by approximately 30-50 MB per layer. Multi-stage builds separate build dependencies from runtime dependencies, further reducing final image size.
Cross-Platform Comparison
| Feature | APT | Homebrew | Chocolatey |
|---|---|---|---|
| Primary Platform | Debian/Ubuntu Linux | macOS, Linux | Windows |
| Package Format | .deb | Formula (.rb) | .nupkg |
| GUI Application Support | Limited (Snap/Flatpak) | Yes (Cask) | Yes |
| Command-Line Tools | Yes | Yes | Yes |
| Dependency Resolution | Automatic | Automatic | Automatic |
| Version Pinning | Yes | Yes (brew pin) | Limited |
| User Installation | Via sudo | By default | Admin required |
| Offline Mode | Limited (apt-offline) | No | Limited |
Frequently Asked Questions
Which package manager should I use for cross-platform development?
Homebrew is the best choice for cross-platform development because it runs on both macOS and Linux. For Windows-specific development, Chocolatey is the standard. Many developers use multiple package managers — Homebrew on macOS for CLI tools, Chocolatey on Windows for Windows applications, and APT on Linux for native packages.
How do I install a specific version of a package?
APT supports apt install package=version. List available versions with apt policy package. Homebrew requires tapping the version repository: brew install package@version. Chocolatey uses choco install package --version=X.Y.Z. Not all versions are available through package managers — older versions are often removed from repositories.
What is the difference between apt update and apt upgrade?
apt update refreshes the package index from repositories — it downloads the list of available packages and their versions. apt upgrade actually installs newer versions of installed packages using the updated index. You must run apt update before apt upgrade to get current package information.
How do I uninstall a package and all its dependencies?
APT: sudo apt purge package && sudo apt autoremove. Homebrew: brew uninstall package. Homebrew automatically removes unused dependencies. Chocolatey: choco uninstall package. Chocolatey may leave dependencies installed — use choco uninstall package --remove-dependencies.
Can I use Homebrew on Linux instead of APT?
Yes. Homebrew on Linux (also called Linuxbrew) installs to /home/linuxbrew/.linuxbrew and does not require sudo. It provides more up-to-date versions than APT for many packages. However, it duplicates package management — APT-managed and Homebrew-managed packages can conflict. Choose one primary package manager for your system.