Alpine User’s Guide to APK: How to Manage Packages

Your ultimate guide to Alpine Linux APK command! Learn how to manage packages - install, update, remove, and search - easily and confidently.

If you’re navigating the world of Alpine Linux and looking to master its package management, you’ve landed in the right place. We’ve covered you, from installing new packages and updating existing ones to removing or searching for them.

In this comprehensive guide, we’re diving deep into the APK command, Alpine’s powerful package management tool. So, are you ready to become an Alpine APK command wizard? I’m sure you are! But before we start, let’s share some theories.

What’s Alpine’s APK Command?

APK stands for “Alpine Package Keeper.” It’s the tool for installing, updating, removing, and managing software packages in Alpine Linux. Like APT in Debian-based distributions or DNF in Red Hat-based systems, APK is the primary way to manage software on Alpine Linux systems.

With the APK command, you can:

  • Install Packages: Easily add new packages to your system.
  • Update Packages: Keep your installed packages up-to-date with the latest versions.
  • Remove Packages: Cleanly remove packages you no longer need.
  • Search for Packages: Find available packages in the Alpine repositories.
  • Upgrade the System: Update all installed packages to their latest versions.
  • Querying Packages: Inspect packages or files to see various details.
  • Add Repositories: Customize your package sources for additional software.

It’s designed to be simple and efficient, aligning with Alpine’s philosophy of keeping things small and resource-friendly. This makes it an ideal tool for servers, containers, and minimal environments.

Lastly, before we delve into the practical examples, it’s important to note that all the examples provided are executed under the root user.

Should you run these with a standard user account, remember to prefix each command with ‘sudo‘ and ensure that your account has been granted sudo privileges in advance. So, let’s get started!

Update Package Lists

Before installing new packages, it’s a good practice to update the list of available packages, ensuring that the index of our local copy of the package database is up-to-date.

apk update
Update the index of available packages.
Update the index of available packages.

The command forces updating the indexes from all configured package repositories.

System Upgrade

The “apk upgrade” command in Alpine is used to upgrade packages to their latest versions. It checks for updates (if any) and bump all the currently installed packages on your system to the most recent versions available in the repositories.

This ensures your system remains patched with the latest versions, bug fixes, and essential security enhancements.

apk upgrade

If no updates are available, the command will provide output similar to the one below.

Upgrade all the currently installed packages.
Upgrade all the currently installed packages.

In short, you can use this command to keep your Alpine Linux system up to date.

However, if you want to upgrade only a specific package on your Alpine system, you can use the apk add command (details below) with the “-u” option added. For example, to upgrade only the “nano” package, run:

apk add -u nano

Search for the Package

The apk search command allows you to search the repository’s index for a given package or descriptions in the list of available packages. For example, to search for “nano,” enter:

apk search nano
Search for packages.
Search for packages.

Include the “-v (–verbose)” option to get a more comprehensive overview of the matches found. This will give you a concise description of each packet in the results.

apk search -v nano
Search for packages with a description added to the results.
Search for packages with a description added to the results.

Searching Through Installed Packages

The apk info command is a versatile tool for gathering package information. Without additional arguments, it lists all packages currently installed on your system, which helps get a quick overview of all the software and libraries on your Alpine installation.

But what if we want to check if a particular package is installed, for example, “nano“? The simplest approach is to use apk info with an added “-e” option to perform this check.

apk info -e nano

We recommend appending the “v” option to “-e,” as it will display the package’s name and installed version.

Searching through installed packages.
Searching through installed packages.

You can achieve the same result if you pass the apk info command’s output (which without additional parameters lists all installed packages on your system) with an added “-v” option through grep by searching for specific package’s name.

apk info -v | grep nano
Searching through installed packages.
Searching through installed packages.

Installing Packages

Installing packages in Alpine is a straightforward process and one of the most common uses of the apk command. You can install any package with apk add followed by the package’s name. For example, let’s install the Nano terminal text editor:

apk add nano
Installing new package with the APK command.
Installing new package with the APK command.

That’s it. How much easier can it be? What’s more, APK even takes care of dependencies automatically. This means when you install a package, APK will also install any necessary dependencies, should there be any.

Looking to install multiple packages simultaneously? It’s easy! Enumerate the names of the packages you want to install one after the other. For instance, to install “nano” and “vim” together in one go:

apk add nano vim
Install multiple packages simultaneously on Alpine Linux.
Install multiple packages simultaneously on Alpine Linux.

As you notice, the APK also automatically installs the “xxd” package, which depends on the Vim installation.

Batch-Installing Using the ‘world’ File

In Alpine Linux, there is a special file located at “/etc/apk/world,” which records all the packages the user has explicitly installed. It’s important to note that this list does not include dependencies that were automatically installed alongside other packages.

When you install a new package, its name is added to the “world” file. Conversely, when you remove a package, it’s removed from this list. This approach ensures that the system retains only the packages that are necessary and desired by the user.

So, if you want to install multiple packages simultaneously, open the file with a text editor and append their names to the end of the file. For example, let’s add “vim,” “htop,” and “curl” to the list.

nano /etc/apk/world
Batch-installing packages on Alpine Linux using the 'world' file.
Batch-installing packages on Alpine Linux using the ‘world’ file.

Then, run apk add without any additional arguments, and the desired packages will be installed along with their dependencies.

Batch-installing packages on Alpine Linux using the 'world' file.
Batch-installing packages on Alpine Linux using the ‘world’ file.

Installing a Local Package

Installing a local package, usually a file with a “.apk” extension, in Alpine is relatively straightforward. To do so, use the apk add command with the “–allow-untrusted” option, followed by the full path to the package.

apk add --allow-untrusted /tmp/package_name.apk

The “–allow-untrusted” flag is necessary only if your package is not from a trusted source, as defined in your APK repositories. Of course, the package must also be compatible with your Alpine version.

Remember, it’s crucial to install APK packages only from sources you trust, as installing from untrusted sources can compromise the security of your system.

Removing Packages

To remove (uninstall) an installed package in Alpine, use the apk del command followed by the package’s name. For example, to remove a “nano,” execute:

apk del nano
Delete packages from the running system.
Delete packages from the running system.

You can also remove multiple packages simultaneously by specifying packages’ names separated by spaces:

apk del nano vim
Removing multiple packages simultaneously on Alpine Linux.
Removing multiple packages simultaneously on Alpine Linux.

Additionally, if you’re unsure about the full name of the package, APK supports wildcards. For example, the command below would remove all packages starting with “vim.”

apk del vim*

However, using a wildcard always brings risks with it. Thus, we highly advise you to assess the potential outcomes using the “–simulate” option provided by APK so you can first check what exactly will happen without actual execution.

apk del --simulate vim*

Lastly, when you install a package in Alpine and modify its configuration files, those altered files stay on your system even if you uninstall the package. Include the “–purge” option to the apk del command to ensure these files are also removed when deleting the package.

apk del --purge vim

Getting Package Details

The apk info command is used to display information about packages. It can query various details about the packages available or installed on an Alpine Linux system. Here are some of its common uses.

You can use apk info followed by the package’s name to get detailed information about a specific package. This will show the package version, description, and size.

apk info nano
Prints information about installed or available packages.
Prints information about installed or available packages.

To get more detailed information, including dependencies and which files belong to a package, add the “-a” option.

apk info -a nano
View detailed information about installed or available packages.
View detailed information about installed or available packages.

View Package’s File List

In Alpine, you can use apk info with the added “-L” option to list the files a package provides. When you run this command, followed by a specific package name, it displays all the files in that package.

This is particularly useful for understanding what files a package installs on your system and where these files are located. For example, let’s check this for the “nano” package:

apk info -L nano
List the files provided by a package.
List the files provided by a package.

View Package’s Dependencies

Another great feature of apk info is to display the reverse dependencies of a package. When you run this command with a “-R” option, followed by a package name, it shows you a list of all dependencies that require the specified package to function.

apk info -R nano
View the package’s dependencies.
View the package’s dependencies.

This is useful for understanding if you uninstall a specific package and what other packages might also be affected by this action.

Trace File’s Package Origin

Have you ever stumbled upon a binary or plain text configuration file and wondered about its origins or which package added it to your Alpine system?

The solution is simple: use the apk info command, but this time include the “-W (–who-owns)” option, followed by the full path to the file.

apk info -W /etc/rc.conf
Trace the file's package origin in Alpine Linux.
Trace the file’s package origin in Alpine Linux.

Check Package’s Installed Size

When you run apk info with the “-s” option followed by the package’s name, it will provide details about the specified package, including its installable size, which helps understand how much space the package will take up when installed.

apk info -s nano
Check package's installed size.
Check package’s installed size.

This is particularly useful in environments where storage space is a concern, such as in minimalistic setups or Docker containers often used in Alpine Linux environments.

Cleaning the Cache

The apk cache command manages the local cache of package archives that the APK has downloaded. But if you’ve installed numerous packages, they can consume significant disk space.

In this case, to clean up the cache by removing downloaded packages that are no longer needed, use the command provided below, which helps in freeing up disk space.

apk cache clean

Broken Packages’ Repair

As unpleasant as it may be, sometimes the installation of specific packages may proceed differently than planned, resulting in a broken package. Here’s where the apk fix command comes to the rescue.

It checks for broken dependencies and attempts to fix them by updating or reinstalling packages. If a package is partially installed or removed, apk fix can help resolve these issues, ensuring that all packages are consistent and functional.

apk fix

It’s beneficial when you have a corrupted package database, or after a failed package installation or upgrade. By running apk fix, you can ensure that all the dependencies are correctly met and the packages are installed on your Alpine system.

View Package’s Policy

The apk policy command is used to show information about the policies of a package. Specifically, it details which repository a package is coming from and its available versions.

This is useful in scenarios where multiple repositories are configured, and you want to understand from which repository a specific package is being pulled or could be pulled and what versions are available in these repositories.

apk policy nano
View information about the policies of a package.
View information about the policies of a package.

Lists Stats for the Installed Packages

The apk stats command displays statistics about the installed packages in the Alpine package management system. It provides information such as the number of installed packages, disk space used by the installed packages, etc.

apk stats
Lists summarized statistics for the installed packages.
Lists summarized statistics for the installed packages.

It helps get a quick overview of the package management system’s status on your Alpine Linux system, especially regarding storage and the number of packages managed by APK.

APK Command Cheat Sheet

Finally, let’s recap all the primary uses of the APK command in Alpine for package management.

CommandUsage
apk updateUpdate the index of available packages.
apk upgradeUpgrade the currently installed packages.
apk searchSearch for packages or descriptions.
apk addInstall new packages or upgrade existing ones.
apk delDelete packages.
apk infoPrints information about installed or available packages.
apk cacheMaintenance operations for locally cached packages.
apk fixRepair broken installed packages.
apk policyShow information about the policies of a package.
apk stats Displays summarized statistics about the installed packages.

Conclusion

As we wrap up this Alpine User’s Guide to APK, I hope you feel more equipped and confident in managing packages on Alpine Linux. Remember, whether installing, updating, removing, or searching for packages, the APK command is your trusty companion in this journey.

Moreover, don’t hesitate to revisit this guide whenever you need a refresher. Thanks for joining me on this adventure through Alpine Linux package management.

I also strongly recommend checking out the official manual. It’s an excellent resource for quick reference on the key features of Alpine Package Keeper.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *