Alpine Linux is a small security-focused Linux Distribution. The base installation of Alpine is only a few MB in size. It is more suitable for container environments such as Docker and Kubernetes, and cloud environments.
In this guide, we learn how to use apk command in Alpine Linux.
Introduction to apk Package Manager
apk is the default package management tool in Alpine Linux. It is very similar to other package managers such as apt for Debian and Ubuntu systems. The apk command is commonly used to install, upgrade and remove packages.
The apk package manager retrieves packages and related details from the online repositories. It uses GPG signatures to verify the authenticity of packages.
All software packages kept in a repository are tar.gz archives with digital signatures. These are sometimes called "a-packs" and have the ".apk" extension.
There are three types of repositories in Alpine Linux:
- Main
- Community
- edge
- Testing
The main repository comprises the packages formally supported by the Alpine Linux core team after extensive testing. The community repository houses the packages that the community supports (through approval from the testing or edge repository).
The edge repository contains the latest builds of all the Linux packages until they become stable. The packages in this repository get frequent updates. You need to take extra care while using a package from this repository.
The most recent, flawed, or out-of-date packages are typically found in the testing repository. The packages in this repository receive no support. The packages get clean if they stay longer than six months in this repository without fulfilling the requirements.
These repositories are stored in a configuration file /etc/apk/repositories. You can view the contents of that file using the following command:
cat /etc/apk/repositories
Example:
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
In the above output, each line in the file corresponds to a repository. You can disable any repositories by adding a "#" sign at the start of the line. Later, you can enable it again by removing the sign.
Apk Command with Examples
Here are a few examples of apk
command usage:
1. Updating Package Index
Before adding any new package, it is the best practice to run the update command. This will download an updated package index from the repositories.
apk update
Running apt update makes sure you always get the latest package when you run the upgrade or add command. This commands works by downloading APKINDEX.tar.gz from each repository and store in /var/cache/apk
The system by default ran apk update every 4 hours when the cache is invalidated.
2. Upgrade All Packages
To upgrade all the installed packages on a running Alpine system, use upgrade command. This makes sure your package gets the latest security and bug fixes patched.
apk update
apk upgrade
These two commands can be combined in a single command like this:
apk -U upgrade
3. Installing Packages
To install or add a new package in Alpine Linux, you can use add option. The add option install (or upgrades) if the package is not present in the system including all dependencies.
Syntax:
apk add PackageName
Replace <package-name> with the name of the package you need to install. For example, to install the zlib package, you can use the following command:
apk add zlib
In case apk finds any dependencies problem, it tries to stick back to the old version of the package.
Interactively install packages
If you want prompts for permission before installing any package use the -i option:
apk -i add PackageName
Example:
apk -i add nettle
This help to discard installation in case you don't want to continue.
Simulate Installation
apk command allows performing a dry installation (or upgrade) using -s option. This actually runs a simulation that shows which versions of packages will get installed or upgraded. Basically won't make any changes to the system.
Syntax:
apk -s add PackageName
Example:
apk -s add tzdata
This comes more useful when you perform the system upgrade (ie apk -s upgrade).
4. Upgrade specific package
Instead of upgrading all the packages using the upgrade command, you can also upgrade a specific package using the -u option. The syntax is as follows:
apk add -u PackageName
Example:
apk add -u nettle
The figure shows updating the nettle package in Alpine Linux.
5. List installed packages
To get the list of all installed packages use apk command with info option.
apk info
apk info command can have a few options that can give specific information about installed packages.
To display only the description of an installed package, add the -d option like this:
apk info -d PackageName
For example to display the description of Zlib package, type:
apk info -d zlib
To show detailed information of the installed packages, you can use the -a option.
apk info -a PackageName
Example:
apk info -a zlib
This shows the package description, webpage, installed size of the package, package dependencies, contains, and more.
6. Verify package installed status
You can verify a package installed status using the apk info command with -e option.
apk -e info PackageName
If your Alpine Linux system has the specific package installed, the output contains the package name. Otherway, if the package is not installed, it will show no output.
Example:
apk -e info zlib
apk -e info tzdata
Please note the that zlib command has an active installation on the given Alpine Linux server, so the name of the package is being displayed as an output. On the other hand, tzdata is not installed and hence, no output is there.
7. Remove a Package
You can use the apk del command to remove a package. This will also remove all dependency packages that are no longer required.
The syntax will be as follows:
apk del PackageName
Example:
apk del nettle
The output shows the nettle package and its dependencies getting purged.
8. Search packages
The Alpine Linux repositories consist of more than 12,000 packages. You may use the Alpine web interface to search for available packages for installation.
You may use apk search command to search for what package to install. It's powerful enough to search everything from package index including binaries and libraries.
For example to list down all available packages from the index
apk search -v
You may perform specific package search:
apk search -v zlib
apk search mechanism can also perform pattern matching. For example to search all packages which have the term 'lib':
apk search -v 'lib*'
9. Hold Specific Package from Upgrade
You might occasionally need to prevent a software package from being upgraded. There are several justifications for doing this. For instance, new package version conflicts with your app or the new package itself have a bug, in those situations you can stick to the old version while upgrading.
Syntax:
apk add PackageName=version
Example:
apk add zlib=1.2.13-r0
This makes sure zlib package stay at version 1.2.13-ro when performing upgrading even if a new version is available.
10. List files in apk Package
To list the files included in a package, use apk info command with -L option. Syntax:
apk info -L PackageName
For example to list files included in zlib package, type:
apk info -L zlib
11. Display Installed package size
To get the installed package size, use apk info command with -s option. Syntax:
apk info -s PackageName
For example to print zlib package installed size:
apk info -s zlib
12. Install .apk local file
To install locally stored .apk files, use apk add with --allow-untrusted flag.
apk add --allow-untrusted packagefile.apk
All dependencies packages should be specified on the same line one after another.
apk add --allow-untrusted /path/to/packagefile.apk /path/to/dependency-packagefile.apk
13. Find package belong to a file
You know a file and would like to know which package that file belongs to ? apk info command has --who-owns
flag to find that.
apk info --who-owns /path/to/filename
Example:
apk info --who-own /lib/libz.so.1
Here you can see the file is owned by zlib-1.2.13-r0 package.
14. List Dependencies of package
To list dependencies of a package use apk info command with -R option. Syntax:
apk -R info PackageName
Example:
apk -R info zlib
15. Clear apk package cache
Its good to perform cache maintenance by cleaning the cache. Basically it deletes all old versions of the packages, which is technically not required as newer packages are added.
apk cache clean
Conclusion
For more information about the apk
command and its usage, you can refer to the Alpine Linux documentation or run apk --help to see a list of available options and commands.
Comments