Home Alpine Linux Alpine Linux Apk Command Examples

Alpine Linux Apk Command Examples

By sk
Published: Updated: 19.5K views

This guide explains what is Apk package manager and how to do various Alpine Linux package management operations using Apk command line package manager with examples.

A brief introduction to apk package manager

Apk, stands for Alpine Package Keeper, is the default package manager for Alpine Linux. It is used to install, update, upgrade, search, list and remove packages on a running Alpine Linux system. Apk is the part of apk-tools package which comes pre-installed in all Alpine Linux versions.

Apk retrieves packages as well as information about available packages in the online repositories. Each repository contains a special index file, named APKINDEX.tar.gz. All software packages stored in a repository are digitally signed tar.gz archives. They have the extension .apk, and are often called "a-packs".

Starting from Alpine Linux version 3.3, there are two repositories enabled by default. They are - main and community.

The [main] repository consists of the packages that are thoroughly tested and officially supported by Alpine Linux core team. The [community] repository contains the community supported packages which are accepted from the [testing] or [edge] repository. The [testing] repository usually have the new, broken and/or outdated packages. No support is provided for this repository and it is not included in the release snapshots.

All repositories are configured in /etc/apk/repositories file. Let us take a look at the contents of this file using cat command:

$ cat /etc/apk/repositories

Sample output:

https://sjc.edge.kernel.org/alpine/edge/main
https://sjc.edge.kernel.org/alpine/edge/community
Alpine Linux repositories configuration file
Alpine Linux repositories configuration file

Each line in the above file indicates a repository. As you can see in the above output, I have enabled [edge] repository, so it is rolling release version.

If you are using stable release, you should have seen the version number, for example v3.12, like below:

https://sjc.edge.kernel.org/alpine/v3.12/main
https://sjc.edge.kernel.org/alpine/v3.12/community

Apk command examples to install and manage packages in Alpine Linux

Package management using apk in Alpine Linux is quite simple and straight forward. Apk doesn't include much options and the syntax is easier to remember.

The typical usage of apk package manager is:

apk [<OPTIONS>...] COMMAND [<ARGUMENTS>...]

Let me show you a few important commands for day-today operations.

1. Display apk command help

If you are new to apk, you can view the apk command help section at any time by running this command:

$ apk --help

apk has many sub-commands to perform a specific operation. To display the help section of a sub-command, run:

$ apk add --help

2. Update package list in Alpine Linux

To get the list of available packages in Alpine Linux repositories, simply run:

$ sudo apk update

Sample output:

fetch https://sjc.edge.kernel.org/alpine/edge/main/x86_64/APKINDEX.tar.gz
fetch https://sjc.edge.kernel.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
v20201218-310-g44bdae590f [https://sjc.edge.kernel.org/alpine/edge/main]
v20201218-333-ga9f7a8a644 [https://sjc.edge.kernel.org/alpine/edge/community]
OK: 13780 distinct packages available
Update package list in Alpine Linux
Update package list in Alpine Linux

As you can see in the above output, currently there are 13780 packages available in Alpine repositories. Please note that these are not the total number of locally installed packages but the total number of available packages.

3. Install packages in Alpine Linux

To add or install a package, for example vim, in Alpine Linux, simply run:

$ sudo apk add vim

The above command will install vim and its dependencies.

(1/3) Installing xxd (8.2.2137-r0)
(2/3) Installing lua5.3-libs (5.3.6-r0)
(3/3) Installing vim (8.2.2137-r0)
Executing busybox-1.32.0-r8.trigger
OK: 163 MiB in 87 packages
Install packages in Alpine Linux
Install packages in Alpine Linux

Similarly, you can add multiple packages by mentioning them with a space like below:

$ sudo apk PackageName1 PackageName2

3.1. Install local packages

If you already have downloaded a package, you can then install it like below.

$ sudo apk add --allow-untrusted vim-8.2.2137-r0.apk

You can also install multiple packages as well:

$ sudo apk add --allow-untrusted PackageName1.apk PackageName2.apk PackageName3.apk

4. Remove packages in Alpine Linux

To uninstall or remove an installed package from your Alpine Linux system, run:

$ sudo apk del vim

This command will remove vim and its dependencies.

(1/3) Purging vim (8.2.2137-r0)
(2/3) Purging xxd (8.2.2137-r0)
(3/3) Purging lua5.3-libs (5.3.6-r0)
Executing busybox-1.32.0-r8.trigger
OK: 144 MiB in 84 packages
Remove packages in Alpine Linux
Remove packages in Alpine Linux

If you want to delete multiple packages, mention them with space-separated.

5. Search packages in Alpine Linux

Alpine Linux repositories contains more than 13,000 packages. You can list all available packages, along with their description, using command:

$ apk search -v

Sample output:

kleopatra-20.08.3-r0 - Certificate Manager and Unified Crypto GUI
py3-libevdev-0.9-r0 - Python3 wrapper around the evdev library
telepathy-farstream-dev-0.6.2_git20190919-r0 - Telepathy GLib-based client library that uses Farstream to handle Call channels (development files)
vulkan-loader-1.2.137-r0 - Vulkan Installable Client Driver (ICD) Loader
mtd-utils-ubi-2.1.2-r0 - MTD utils (UBI and UBIFS commands)
hwids-udev-20201207-r0 - Hardware identification databases (udev integration)
py3-rsa-4.6-r0 - Pure-Python3 RSA implementation
[...]

To display only the total number of packages, pipe the output to wc command like below:

$ apk search -v | wc -l

You can also get the list of all packages matching a pattern. For example, the following command will display all packages that matches the search term "vim".

$ apk search -v 'vim*'

If you only want to display the packages that contains a specific term in their description, run:

$ apk search -v --description 'disk'

Or, shortly use -d switch:

$ apk search -v -d 'disk'

6. Display package details in Alpine Linux

To display the details of a specific package, we can use info command like below:

$ apk info vim

You will see the small description of the given package and it's website where it is hosted.

Sample output:

vim-8.2.2137-r0 description:
Improved vi-style text editor

vim-8.2.2137-r0 webpage:
https://www.vim.org/

vim-8.2.2137-r0 installed size:
18 MiB

gvim-8.2.2137-r0 description:
advanced text editor, with GUI

gvim-8.2.2137-r0 webpage:
https://www.vim.org/

gvim-8.2.2137-r0 installed size:
29 MiB
Display package details in Alpine Linux
Display package details in Alpine Linux

As you can see, this command not only lists the vim package details but also the other packages' details that contains the pattern "vim" in their name.

If you want the detailed information about the given package and its dependencies, use -a flag.

$ apk info -a vim

The above command will display the following details:

  • package description.
  • website of the package.
  • installed size (i.e. the size required by the package after it is installed).
  • the contents of the package (i.e. list of files that the package installs).
  • package dependencies i.e. what packages are required to use this one.
  • what packages require this one to be installed.
  • and more.

7. List installed packages in Alpine Linux

To view list of all installed packages in your Alpine Linux system, run:

$ apk info

Sample output:

lm_sensors
man
virtualbox-guest-modules-virt
musl
libcrypto1.1
libssl1.1
apk-tools
musl-utils
busybox
alpine-baselayout
[...]

You can also list the installed packages with their version number and description:

$ apk info -vv

Sample output:

lm_sensors-3.4.0-r6 - Collection of user space tools for general SMBus access and hardware monitoring.
man-1.14.3-r0 - dummy package for upgrade compatibility.  this can safely be removed
virtualbox-guest-modules-virt-4.14.167-r0 - VirtualBox Additions kernel modules for virt
musl-1.2.2_pre6-r0 - the musl c library (libc) implementation
libcrypto1.1-1.1.1i-r0 - Crypto library from openssl
libssl1.1-1.1.1i-r0 - SSL shared libraries
apk-tools-2.12.0-r3 - Alpine Package Keeper - package manager for alpine
musl-utils-1.2.2_pre6-r0 - the musl c library (libc) implementation
busybox-1.32.0-r8 - Size optimized toolbox of many common UNIX utilities
alpine-baselayout-3.2.0-r8 - Alpine base dir structure and init scripts
[...]

List only total number of installed packages:

$ apk info | wc -l
87

8. Upgrade packages in Alpine Linux

To upgrade a specific package, run:

$ sudo apk update
$ sudo apk add -u vim

Or,

$ sudo apk add --upgrade vim

To upgrade all installed packages, run:

$ sudo apk update
$ sudo apk upgrade

You can also combine the above command into one like below:

$ sudo apk -U upgrade
Upgrade packages in Alpine Linux
Upgrade packages in Alpine Linux

9. Upgrade Alpine Linux

Upgrading a running Alpine Linux to next available version is easy! Alpine Linux comes in two editions:

  • stable
  • rolling release (edge)

You can either upgrade from current stable version to newer version or convert a stable version into a rolling release version.

More detailed instructions are available in the following guide:

10. Hold a package from upgrade

Sometimes, you want to prevent or hold a package from being automatically upgraded for some reason when upgrading your Alpine Linux system. There could be many reasons to hold a package back. For instance, a particular version is required by an application in order to work properly. Or the new package may break your system. So you want to ignore the packages from being upgraded when you upgrade the system.

To hold a specific package from upgrade, use any one of the following commands:

$ sudo apk add vim=8.2.0-r0

Or,

$ sudo apk add 'vim<8.2.1'

And then, do the system upgrade using command:

 $ sudo apk upgrade

This command will upgrade all packages but keep the vim package at 8.2.0 or lower version.

You can later upgrade vim package to currently available version like below:

$ sudo apk add 'vim>8.2.1'

You can also use "fuzzy" version matching to pin the version to a major/minor release. For instance, the following command will match any version of vim that starts with 8.2.

$ sudo apk add 'vim=~8.2'

Related read:

11. Download packages in Alpine Linux

Sometimes, you want to download a specific package but don't want to install it. For instance, let us download vim package using command:

$ apk fetch vim

The above command will display the vim package from Alpine repositories and save it in the current directory.

$ ls
vim-8.2.2137-r0.apk

If you want to save the package in different path, use -o switch.

$ apk fetch vim -o ~/Downloads

Please note that this command will not download all required dependencies but only the actual package.

To download the given package along with all required dependencies, use -R (--recursive) flag:

$ apk fetch -R vim

Sample output:

Downloading xxd-8.2.2137-r0
Downloading ncurses-libs-6.2_p20201219-r0
Downloading musl-1.2.2_pre6-r0
Downloading lua5.3-libs-5.3.6-r0
Downloading vim-8.2.2137-r0
Downloading ncurses-terminfo-base-6.2_p20201219-r0

You can install the locally downloaded packages as shown in 3.1. Install local packages section.

12. Display repository details of a package

You might want to find which repository a package comes from. Apk has a sub-command for that too.

To display the repository a package (E.g. vim) was installed from and will be updated from, use apk policy command:

$ apk policy vim

Sample output:

vim policy:
  8.2.2137-r0:
    lib/apk/db/installed
    etc/apk/cache
    https://sjc.edge.kernel.org/alpine/edge/main

As you can see, vim package has been installed from the [main] repository.

13. Show statistics of packages and repositories

We can print the statistics about installed packages and package repositories using command:

$ apk stats

Sample output:

installed:
  packages: 87
  dirs: 485
  files: 9617
  bytes: 171270144
  triggers: 5
available:
  names: 30975
  packages: 13773
atoms:
  num: 9761

14. Clean package cache

By default, all installed packages are cached in /etc/apk/cache/ directory. When newer packages are added to the cache, the older versions of the packages remains by default.

If you don't want the older versions of the packages, you can simply clear the package cache using command:

$ sudo apk cache clean

If you want to see what versions are deleted, use -v (verbose) switch:

$ sudo apk -v cache clean

If you have accidentally delete packages from the cache, you can download them using command:

$ sudo apk cache download

You can also delete the cache and download the missing package in a single command:

$ sudo apk cache -v sync

Conclusion

In this comprehensive tutorial, we discussed 14 Alpine Linux APK command examples to do various package management operations.

Hope this helps.

Resource:

Related read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More