Alpine Linux is an independent, free, and open-source Linux distribution based on BusyBox and musl. It is a lightweight and security-oriented Linux distribution that comes in a small footprint (about 160 MB).
For this reason, it’s widely used in creating containers that are lightweight and standalone units that provide an isolated environment to deploy and run applications.
Alpine Linux targets users who desire simplicity, security, and efficient resource utilization. It is designed for x86, x86-64. AArch64 and ARM architectures.
Like any other Linux distribution, Alpine Linux comes with its own package manager known as apk (Alpine Package Keeper) and comes pre-installed on all Alpine Linux distributions.
Apk handles all the package management operations including searching, installing, upgrading, listing, and removing software packages just to mention a few. In this guide, we showcase commonly used Apk command examples in Alpine Linux.
Alpine Linux Packages and Repositories
Before we look at the various apk commands that you can leverage to manage your packages, let us touch on Alpine Linux repositories.
Alpine Linux has two repositories enabled by default: the main and community repositories.
- The main repository comprises packages that are rigorously tested and approved to be officially hosted by the Alpine Linux core development team.
- The community repository, on the other hand, comprises community-supported packages which are ported from the edge or testing repositories.
On your local Alpine Linux system, you can find the repositories in the /etc/apk/repositories file, you can use the cat command to view them as follows.
$ cat /etc/apk/repositories
Having looked at the repositories, let us straight away jump into managing packages using the apk package manager.
1. Update Alpine Linux
To update the repositories and package lists on Alpine Linux, run the command
$ apk update
2. Search for an Availability of Packages
Before installing packages, it’s worthwhile to check if the packages have been officially been hosted in the repositories. To do so, use the syntax:
$ apk search package_name
For example, to search for a nano package in the repositories, run the command:
$ apk search nano
3. Get a Description of an Installed Package
To get a description of a package in the repositories, about the package pass the -v
and -d
flags as shown. The option -d
is short for description whilst the -v
option prints out verbose output.
$ apk search -v -d nano
4. Install Packages in Alpine Linux
To install packages on Alpine Linux, use the syntax:
$ apk add package_name
For example, to install the nano text editor, run the command:
$ apk add nano
Additionally, you can install multiple packages in a single command using the syntax:
$ apk add package1 package2
For example, the command below installs neofetch and vim editor at a go.
$ apk add neofetch vim
You can confirm if you installed neofetch by running the command:
$ neofetch
This populates information about the operating system such as OS type, kernel, uptime, and underlying hardware such as CPU and memory.
To confirm that vim editor is installed, simply run the vim command without any arguments and this will display information about vim.
$ vim
The -i
option prompts for user interaction when installing packages. It causes apk to ask you whether to continue with the installation of the package or abort.
$ apk -i add apache2
5. Check Installed Package in Alpine Linux
To probe if a certain package is already installed, use the syntax:
$ apk -e info package_name
In this example, we are checking if Nano is installed.
$ apk -e info nano
In addition, you can check if multiple packages exist by listing them in the same line. For this example, we are verifying if both nano and vim are installed.
$ apk -e info nano vim
To list additional information such as the version and size of the installed package simply run:
$ apk info nano
6. List Files Associated with a Package
The -L
flag allows you to list the files associated with a package, which includes the binary and configuration files and other files.
$ apk -L info nano
7. List Dependencies of a Package
With the -R
option, you can list the packages that the package depends on. In the following example, we are listing the dependencies that vim depends on.
$ apk -R info vim
8. Find the Installed Size of a Package
To view the installed size of a package, use the -s
option (lowercase) as follows:
$ apk -s info vim
9. List All Installed Packages
To list all installed packages on Alpine Linux, run the command:
$ apk info
10. Upgrade Alpine Linux
To upgrade all the packages on Alpine Linux to their latest versions, run the command
$ apk upgrade
To perform a dry run of the upgrade, pass the -s
option. This merely runs a simulation and shows the versions that the packages will be upgraded to. It does not upgrade the packages.
$ apk -s upgrade
11. Hold a Package Upgrade
There are instances where you may want to keep a few packages back from an upgrade. For instance to keep nano in its current version – nano-5.9-r0 – run the command.
$ apk add nano=5.9-r0
This will exempt the nano package from the upgrade as other packages are upgraded to their latest versions.
To later release the package for the upgrade, run:
$ apk add 'nano>5.9'
12. Remove a Package in Alpine Linux
If you no longer require a package, you can remove it using the syntax:
$ apk del package_name
For example, to delete vim, run the command.
$ apk del vim
13. Getting Help with Apk Command
For additional apk commands, you can browse the apk help catalog as shown
$ apk --help
In this guide, we focussed on Alpine apk command examples. We hope that this will help you as you get started installing and managing packages on Alpine Linux.
Why apk and not apt?
Why apk add and not apk install?
I wish I could get rid of this strange distro from my docker containers.