Skip to main content
The apk module manages packages on Alpine Linux systems using the apk package manager.

Functions

packages

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

apk.packages(
    name="Install web utilities",
    packages=["nginx", "curl"],
    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 apk update before installing packages.
upgrade
bool
default:false
Run apk upgrade before installing packages.
Package versions can be pinned like apk: <pkg>=<version>

Examples

from pyinfra.operations import apk

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

update

Update apk package indexes.
from pyinfra.operations import apk

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

upgrade

Upgrade all apk packages.
from pyinfra.operations import apk

apk.upgrade(
    name="Upgrade all packages",
    available=True,
)
available
bool
default:false
Force all packages to be upgraded (recommended on whole Alpine version upgrades).
This operation is not idempotent - it will always execute.

Common Use Cases

Basic System Setup

from pyinfra.operations import apk

# Update repositories
apk.update(
    name="Update package repositories",
)

# Install essential packages
apk.packages(
    name="Install essential tools",
    packages=[
        "bash",
        "curl",
        "wget",
        "git",
        "vim",
    ],
)

Container Base Image Setup

from pyinfra.operations import apk

# Minimal installation for containers
apk.packages(
    name="Install runtime dependencies",
    packages=[
        "ca-certificates",
        "tzdata",
    ],
    update=True,
)

Development Environment

from pyinfra.operations import apk

# Install build tools
apk.packages(
    name="Install build dependencies",
    packages=[
        "build-base",
        "gcc",
        "g++",
        "make",
        "cmake",
    ],
    update=True,
)

# Install development tools
apk.packages(
    name="Install development tools",
    packages=[
        "git",
        "vim",
        "bash",
        "curl",
    ],
)

Python Development

from pyinfra.operations import apk

# Install Python and dependencies
apk.packages(
    name="Install Python development environment",
    packages=[
        "python3",
        "python3-dev",
        "py3-pip",
        "py3-virtualenv",
    ],
    update=True,
)

System Upgrade

from pyinfra.operations import apk

# Update repositories
apk.update(
    name="Update repositories",
)

# Upgrade all packages
apk.upgrade(
    name="Upgrade all packages",
    available=True,
)

Alpine-Specific Tips

Alpine supports virtual packages for build dependencies that can be easily removed:
from pyinfra.operations import apk

# Install build dependencies as virtual package
apk.packages(
    name="Install build dependencies",
    packages=[".build-deps", "gcc", "musl-dev"],
)

# Later, remove all build dependencies
apk.packages(
    name="Remove build dependencies",
    packages=[".build-deps"],
    present=False,
)
For latest packages, you may need to use the edge repository:
from pyinfra.operations import server

# Add edge repository
server.shell(
    name="Enable edge repository",
    commands=[
        'echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories',
        'echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories',
    ],
)
For minimal container images, use —no-cache:
from pyinfra.operations import server

server.shell(
    name="Install without cache",
    commands=["apk add --no-cache nginx"],
)

Build docs developers (and LLMs) love