Skip to main content
The brew module manages packages and casks on macOS using Homebrew package manager.

Functions

packages

Install, remove, or update brew packages.
from pyinfra.operations import brew

brew.packages(
    name="Install development tools",
    packages=["vim", "git"],
    update=True,
)
packages
str | list[str]
List of packages to ensure.
present
bool
default:true
Whether the packages should be installed.
latest
bool
default:false
Whether to upgrade packages without a specified version.
update
bool
default:false
Run brew update before installing packages.
upgrade
bool
default:false
Run brew upgrade before installing packages.
Package versions can be pinned like brew: <pkg>@<version>

Examples

from pyinfra.operations import brew

# Update package list and install packages
brew.packages(
    name="Install Vim and vimpager",
    packages=["vimpager", "vim"],
    update=True,
)

update

Update brew package repositories.
from pyinfra.operations import brew

brew.update(
    name="Update brew repositories",
)
This operation is not idempotent - it will always execute.

upgrade

Upgrade all brew packages.
from pyinfra.operations import brew

brew.upgrade(
    name="Upgrade all packages",
)
This operation is not idempotent - it will always execute.

casks

Install, remove, or update brew casks (applications).
from pyinfra.operations import brew

brew.casks(
    name="Install desktop applications",
    casks=["firefox", "visual-studio-code"],
)
casks
str | list[str]
List of casks to ensure.
present
bool
default:true
Whether the casks should be installed.
latest
bool
default:false
Whether to upgrade casks without a specified version.
upgrade
bool
default:false
Run brew cask upgrade before installing casks.
Cask versions can be pinned like brew: <pkg>@<version>

Example

from pyinfra.operations import brew

brew.casks(
    name="Upgrade and install the latest cask",
    casks=["godot"],
    upgrade=True,
    latest=True,
)

cask_upgrade

Upgrade all brew casks.
from pyinfra.operations import brew

brew.cask_upgrade(
    name="Upgrade all casks",
)
This operation is not idempotent - it will always execute.

tap

Add or remove brew taps (third-party repositories).
from pyinfra.operations import brew

brew.tap(
    name="Add a brew tap",
    src="homebrew/cask-fonts",
)
src
str
The name of the tap (e.g., user/repo).
present
bool
default:true
Whether this tap should be present or not.
url
str
The URL of the tap. See https://docs.brew.sh/Taps

Examples

from pyinfra.operations import brew

brew.tap(
    name="Add a brew tap",
    src="includeos/includeos",
)

Common Use Cases

Development Environment Setup

from pyinfra.operations import brew

# Update brew
brew.update(
    name="Update Homebrew",
)

# Install development tools
brew.packages(
    name="Install development tools",
    packages=[
        "git",
        "vim",
        "tmux",
        "wget",
        "curl",
        "jq",
        "[email protected]",
        "node",
    ],
)

# Install desktop applications
brew.casks(
    name="Install development applications",
    casks=[
        "visual-studio-code",
        "iterm2",
        "docker",
        "postman",
    ],
)

Install from Custom Tap

from pyinfra.operations import brew

# Add custom tap
brew.tap(
    name="Add custom tap",
    src="homebrew/cask-fonts",
)

# Install from tap
brew.casks(
    name="Install fonts",
    casks=[
        "font-fira-code",
        "font-jetbrains-mono",
    ],
)

Full System Update

from pyinfra.operations import brew

# Update brew
brew.update(
    name="Update Homebrew",
)

# Upgrade all packages
brew.upgrade(
    name="Upgrade all packages",
)

# Upgrade all casks
brew.cask_upgrade(
    name="Upgrade all casks",
)

Database Tools

from pyinfra.operations import brew

brew.packages(
    name="Install database tools",
    packages=[
        "postgresql@14",
        "redis",
        "mysql",
    ],
    update=True,
)

Install Browser for Testing

from pyinfra.operations import brew

brew.casks(
    name="Install browsers",
    casks=[
        "google-chrome",
        "firefox",
        "microsoft-edge",
    ],
)

Homebrew-Specific Tips

Homebrew provides service management for installed packages:
from pyinfra.operations import server

# Start a service
server.shell(
    name="Start PostgreSQL",
    commands=["brew services start postgresql@14"],
)

# Stop a service
server.shell(
    name="Stop PostgreSQL",
    commands=["brew services stop postgresql@14"],
)

# Restart a service
server.shell(
    name="Restart PostgreSQL",
    commands=["brew services restart postgresql@14"],
)
Remove old versions of installed packages:
from pyinfra.operations import server

server.shell(
    name="Clean up old versions",
    commands=["brew cleanup"],
)
Verify Homebrew installation:
from pyinfra.operations import server

server.shell(
    name="Run brew doctor",
    commands=["brew doctor"],
)
  • Formulae are command-line tools and libraries (use brew.packages)
  • Casks are macOS applications (use brew.casks)
from pyinfra.operations import brew

# Command-line tool
brew.packages(
    name="Install git",
    packages=["git"],
)

# Desktop application
brew.casks(
    name="Install VS Code",
    casks=["visual-studio-code"],
)

Build docs developers (and LLMs) love