Linux apk Command: Manage Packages on Alpine Linux

apk is the package management command for Alpine Linux. It is used for searching, adding, removing, and upgrading repository packages, and the procedure for preserving changes may vary depending on how Alpine is installed.

apk add adds packages to the desired system state, and apk del removes those constraints. The list of desired packages is recorded in /etc/apk/world. apk update refreshes the repository index, and apk upgrade updates installed packages. In diskless mode, a separate procedure may be needed to persist changes.

Basic syntax

apk [global options] command [package...]

The installed implementation and options may vary depending on the distribution. Check the current system's description with man apk.

Examples

Package search

Find candidates in the repository that match the name.

apk search nginx

Package Installation

Check for required dependencies and add them.

sudo apk add nginx

Refreshing repository information and upgrading

Check the difference between the two actions and review the list of changes.

sudo apk update
sudo apk upgrade

Distinguishing Between Installation Status and Repository Update

apk info -e checks whether a specified package is installed by name. In a default Alpine environment, if busybox is installed, it will appear as follows.

apk info -e busybox

Example output:

busybox

apk update only refreshes the repository index and does not change the version of installed packages. To actually upgrade installed packages, apk upgrade is required. In diskless mode or disposable containers, check according to your operational method if package changes persist after restarting or recreating.

Main Options and Format

Options/Format Description
search / info Find repository packages or view details.
add / del Add or remove desired package constraints.
update Update the repository index.
upgrade Upgrade installed packages to the latest possible versions.
--no-cache This is an option to reduce the use of the local package cache when executing commands.

Precautions when using

Installed packages are not upgraded with apk update alone. apk del may remove items depending on whether they are required by other packages, so check the suggestion list. In diskless/container environments, package changes need to be reflected in the image or management configuration to persist after recreation.

Frequently Asked Questions

What is the difference between apk add and apk update?

"add" installs the desired packages, and "update" updates the repository index. Version updates for installed packages are done with "upgrade".

Official Documentation

The exact behavior of the option and differences between implementations can be found in the official documentation related to apk.