Alpine is an open-source Linux distribution based on BusyBox and musl, available freely without any subscription processes. It is a compact and lightweight Linux distribution that prioritizes security and comes with a very light footprint of about 160 MB.
Like every other Linux distribution, Alpine Linux also includes its own package manager, known as Alpine Package Keeper (apk). The apk is pre-installed on all the Alpine Linux versions.
Apk covers all the package management tasks, including installing, searching, updating, listing, and uninstalling the software packages. In this tutorial, we learn how to install packages in Alpine Linux.
Install Package in Alpine Linux using apk
Installing new packages in Alpine Linux is straightforward. You can use the following apk command syntax to install a new package in Alpine:
apk add <package_name>
apk add package_name1 package_name2 #add multiple packages
Replace <package-name> with the name of the package you want to install. For example, to install the openssh package, you can use the following command:
apk add openssh
This will install the openssh package, along with any dependencies it requires. You may add -i option for interactive installation, so apk ask you whether to proceed or abort.
Note that you must have root privileges to run this command, so you may need to use sudo or log in as the root user. Once executed successfully, the above command generates output as:
To install a specific version of the package, use package pinning.
apk add pkgname=1.2.3-suffix
apk add nginx=1.22.1-ro
Remember the official repo can drop the package from the branch, recommended to use your own mirror.
List Installed Packages Using the APK Command
You can use apk info command to list down all the packages currently installed on Alpine:
The output of the info command looks like this:
Note: The output red highlights the recently installed package.
Add Repository Using the APK Command
Alpine Linux packages are tar.gz archive files containing programs, configuration files, and metadata. The packages have the .apk extension and are often called "a-packs."
The Alpine packages are kept in a single or sometimes more repositories. Here, the repository refers to a directory with a collection of *.apk files and an index file named APKINDEX.tar.gz.
You can install packages from multiple repositories using the apk utility. The list of repositories is stored in the /etc/apk/repositories file. This file lists all the repositories, having one repository per line.
To view the contents of this file, use the cat command as depicted by the following snippet:
The three branches of repositories are main, community, and testing.
- Main - Default. Packages tested and supported the Alpine core team. Support cycle of 2 years.
- Community - Package from the community and could be removed in the future. Support cycle 6 months.
- Testing - Development packages.
http://dl-cdn.alpinelinux.org/alpine/v3.17/main #http://dl-cdn.alpinelinux.org/alpine/v3.17/community #http://dl-cdn.alpinelinux.org/alpine/edge/main #http://dl-cdn.alpinelinux.org/alpine/edge/community #http://dl-cdn.alpinelinux.org/alpine/edge/testing #https://dl-cdn.alpinelinux.org/alpine/latest-stable/main #https://dl-cdn.alpinelinux.org/alpine/latest-stable/community
Alpine can fetch some packages from online mirrors. You can add links to these mirrors in the configuration file /etc/apk/repositories.
To make repositories changes effective:
In the repositories file, you can add tags to repositories. The repository with a tag is called a tagged repository.
Alpine package manager does not use a tagged repository unless you specify it while installing a package. You can add a tagged repository in the repositories file by adding an @ sign and tag name before the repository link, as showcased by the following line.
By default, the Alpine Linux package manager will only use untagged repositories. However, applying a tag to a specific package favors the tagged repository over others, even if a later version of the package is available in the other repositories listed.
For example, you can use the following command to add a package from the tagged repository "TaggedRepo":
apk add [email protected]
The above Alpine command installs the newapp package from the "TaggedRepo", even if the latest version of newapp is available with some other repositories.
Alpine package keeper makes it easy to install the packages on Alpine. You can visit the official documentation page to learn more about this default Alpine package manager.