DevOpsil
Linux
87%
Needs Review

Alpine Linux apk Reference: Package Management, Repositories, and System Administration

Raafay AsifRaafay Asif7 min read

Alpine's apk Is Not apt or dnf

Alpine Linux uses apk (Alpine Package Keeper) — a fast, straightforward package manager that's distinct from both apt and dnf. If you're coming from Ubuntu or RHEL, apk works similarly but with different flags and a much simpler dependency model.

This guide covers apk from daily operations to repository management, with enough context about Alpine's design philosophy to make sense of why things work the way they do.


Alpine Releases

Alpine follows a predictable release model:

BranchDescription
edgeRolling release — latest software, less stability
v3.20Latest stable (as of 2026)
v3.19, v3.18Previous stable releases

Alpine stable releases receive security and bug-fix updates for ~2 years. edge is used for active development. For servers, use a numbered stable release.


Core apk Operations

Installing Packages

# Install a package
apk add nginx

# Install multiple packages
apk add nginx curl bash git

# Install without caching index (for Dockerfiles)
apk add --no-cache nginx

# Install a specific version
apk add nginx=1.26.2-r0

# Install only if not already installed (idempotent)
apk add --no-cache nginx

# Simulate installation (dry run)
apk add --simulate nginx

Removing Packages

# Remove a package
apk del nginx

# Remove a package and its unused dependencies
apk del --purge nginx

# Remove packages that were auto-installed as dependencies
# and are no longer needed
apk del --purge $(apk info --rdepends nginx | tail -n +2)

Updating Packages

# Update the package index
apk update

# Upgrade all installed packages
apk upgrade

# Update index and upgrade in one command
apk update && apk upgrade

# Upgrade only a specific package
apk upgrade nginx

# Check for available upgrades without applying
apk version -v -l '<'

Searching and Querying

# Search for packages
apk search nginx

# Search by file (what package provides a file)
apk search -o /usr/sbin/nginx

# Show package info
apk info nginx

# Show package dependencies
apk info -d nginx

# List installed packages
apk info

# List files installed by a package
apk info -L nginx

# Check if a package is installed
apk info nginx 2>/dev/null && echo "installed" || echo "not installed"

# Show reverse dependencies (what depends on this package)
apk info -r nginx

# Show package version
apk version nginx

Repository Management

Alpine's repositories are configured in /etc/apk/repositories. Each line is a URL to a repository.

Default Repository Structure

cat /etc/apk/repositories

# Typical output for Alpine 3.20:
# https://dl-cdn.alpinelinux.org/alpine/v3.20/main
# https://dl-cdn.alpinelinux.org/alpine/v3.20/community

Alpine has three main repository categories:

  • main: Core packages maintained by Alpine
  • community: Broader package set, maintained by contributors
  • testing: Unstable packages not yet in main/community (edge only)

Adding Repositories

# Add the community repo (if not already enabled)
echo "https://dl-cdn.alpinelinux.org/alpine/v3.20/community" >> /etc/apk/repositories
apk update

# Add the edge testing repo (for packages not in stable)
echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
apk update

# Install a package from edge/testing (using tag)
apk add some-package@testing

Repository Tags

Alpine supports repository tagging to pin packages to specific repos:

# In /etc/apk/repositories:
@edge https://dl-cdn.alpinelinux.org/alpine/edge/main
@community https://dl-cdn.alpinelinux.org/alpine/v3.20/community
@testing https://dl-cdn.alpinelinux.org/alpine/edge/testing

# Install from a specific tagged repo
apk add postgresql@edge
apk add some-new-package@testing

Version Pinning and Holds

# Install and pin a specific version
apk add nginx=1.26.2-r0

# Pin using world file (persists across apk upgrade)
# The world file at /etc/apk/world lists all explicitly installed packages
cat /etc/apk/world

# To hold a package at its current version (prevent upgrade):
# Pin it in world with the exact version
apk add 'nginx=~1.26'    # pin to 1.26.x series (fuzzy)
apk add 'nginx=1.26.2-r0'  # pin to exact version

# Verify pinning
apk version nginx

The World File

Alpine tracks explicitly installed packages in /etc/apk/world. This file is what separates explicitly requested packages from auto-installed dependencies.

# View the world file
cat /etc/apk/world

# Reconstruct packages from world (useful for reproducing a system)
apk add $(cat /etc/apk/world)

# Sync the system to match world file exactly
# (installs missing, removes unlisted packages)
apk fix

Running Alpine as a Server

Network Configuration

Alpine uses /etc/network/interfaces for networking:

# Static IP configuration
cat > /etc/network/interfaces << 'EOF'
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address 192.168.1.100/24
    gateway 192.168.1.1
EOF

# DNS
echo "nameserver 1.1.1.1" > /etc/resolv.conf
echo "nameserver 8.8.8.8" >> /etc/resolv.conf

# Apply network config
/etc/init.d/networking restart

Init System: OpenRC

Alpine uses OpenRC, not systemd. Service management is different:

# Start a service
rc-service nginx start

# Stop a service
rc-service nginx stop

# Enable on boot (add to default runlevel)
rc-update add nginx default

# Disable from boot
rc-update del nginx default

# List services and their runlevels
rc-update show

# Check service status
rc-service nginx status

# Restart
rc-service nginx restart

OpenRC runlevels:

  • boot — essential services (networking, logging)
  • default — normal services
  • nonetwork — services that run without network

Disk and Filesystem

# Alpine runs from RAM by default (diskless mode)
# For persistent install, use setup-alpine to install to disk

# Check disk usage
df -h

# Alpine's setup scripts handle common tasks:
setup-alpine      # Full system setup wizard
setup-disk        # Configure disk/partitions
setup-networking  # Configure network
setup-apkrepos    # Configure repositories
setup-sshd        # Configure SSH

# Add a swap file
dd if=/dev/zero of=/swapfile bs=1M count=1024
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile none swap sw 0 0" >> /etc/fstab

Building Custom apk Packages

For packaging your own software:

# Install build tools
apk add alpine-sdk abuild

# Set up a build user
adduser -D builduser
addgroup builduser abuild

# Generate signing key
su - builduser -c "abuild-keygen -a -i"

# Create APKBUILD file
mkdir -p /home/builduser/myapp
cat > /home/builduser/myapp/APKBUILD << 'EOF'
pkgname=myapp
pkgver=1.0.0
pkgrel=0
pkgdesc="My application"
url="https://example.com"
arch="x86_64"
license="MIT"
depends="nginx"
source="myapp-$pkgver.tar.gz"

build() {
    cd "$builddir"
    make
}

package() {
    cd "$builddir"
    make DESTDIR="$pkgdir" install
}
EOF

# Build the package
cd /home/builduser/myapp
su - builduser -c "cd /home/builduser/myapp && abuild -r"

Common Alpine Server Packages

# Web server
apk add nginx

# Database
apk add postgresql postgresql-client
apk add mariadb mariadb-client

# Languages
apk add python3 py3-pip
apk add nodejs npm
apk add php83 php83-fpm

# Security
apk add openssh fail2ban

# Monitoring
apk add htop iotop sysstat

# Networking tools
apk add curl wget bind-tools tcpdump nmap

# File management
apk add rsync tar gzip

apk vs apt vs dnf Quick Reference

Operationapkaptdnf
Installapk add pkgapt install pkgdnf install pkg
Removeapk del pkgapt remove pkgdnf remove pkg
Update indexapk updateapt updatednf check-update
Upgrade allapk upgradeapt upgradednf upgrade
Searchapk search pkgapt search pkgdnf search pkg
Infoapk info pkgapt show pkgdnf info pkg
List filesapk info -L pkgdpkg -L pkgrpm -ql pkg
What providesapk search -o /filedpkg -S /filednf provides /file

Alpine's apk is significantly faster than both apt and dnf due to its simpler dependency solver and the compact package format. For container images and resource-constrained systems, this speed difference is noticeable at scale.

Share:

Was this article helpful?

Raafay Asif
Raafay Asif

Linux Systems Engineer

Everything runs on Linux — I make sure it runs well. From kernel tuning to systemd debugging, I live in the terminal. If your server is misbehaving, I've probably seen that exact dmesg output before.

Related Articles

More in Linux

View all →

Discussion