apk add --no-cache: Install Packages on Alpine
apk add --no-cache installs packages on Alpine without leaving an index cache behind, ideal for small Docker images.
Alpine images are the default for slim containers, and apk is its package manager. The --no-cache flag is what keeps those images tiny.
What it does
apk add installs the named packages and their dependencies from the configured repositories. --no-cache fetches a fresh index for this command and does not write it to /var/cache/apk, so there is nothing to clean up afterward.
Common usage
apk add --no-cache curl ca-certificates
# pin a version
apk add --no-cache "nodejs=~20"
# build deps in a removable virtual group
apk add --no-cache --virtual .build-deps gcc musl-dev make
# ... build ...
apk del .build-depsOptions
| Flag | What it does |
|---|---|
| --no-cache | Do not cache the index; keeps images small |
| --virtual <name> | Group packages so apk del <name> removes them all |
| --repository <url> | Add a repository just for this command |
| --no-progress | Suppress the progress bar (cleaner logs) |
| -t / --virtual | Alias for creating a virtual package group |
| pkg=version | Install an exact version |
In CI
Use --no-cache instead of the older apk update && apk add ... && rm -rf /var/cache/apk/* pattern; it is one flag and always correct. Wrap compile-time dependencies in --virtual .build-deps so a single apk del removes them before the final layer.
Common errors in CI
"ERROR: unable to select packages" with "so:libX.so.1 (no such package)" means a dependency is missing from the enabled repos, often the community or edge repo; enable it. "WARNING: Ignoring ... No such file or directory" for a repo means the mirror is unreachable. "ERROR: <pkg> (no such package)" means the name is wrong or absent in this Alpine version.