Blog/apk-upgrade

From Forza's ramblings

apk-upgrade[edit | edit source]

The apk-upgrade script is a convenience tool designed for Alpine Linux users to simplify the process of updating and upgrading system packages. It combines the steps of apk update, apk upgrade -s, and apk upgrade into a single interactive workflow.

Usage[edit | edit source]

Save the script to a file, for example, apk-upgrade.sh, and make it executable with chmod +x apk-upgrade.sh.

The script will:

  1. Update the package index.
  2. Check for and display any available upgrades.
  3. Prompt for confirmation before upgrading packages.

If no upgrades are available, the script will exit without performing any changes.

# ./apk-upgrade.sh
Updating apk cache...
fetch http://dl-cdn.alpinelinux.org/alpine/v3.18/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.18/community/x86_64/APKINDEX.tar.gz
OK: 15634 distinct packages available

Checking for upgradable packages...
The following packages can be upgraded:
(1/2) Upgrading libssl (1.1.1s-r0 -> 1.1.1t-r0)
(2/2) Upgrading openssh (9.1_p1-r0 -> 9.2_p1-r0)

Do you want to upgrade the packages? [y/N]:

Source Code[edit | edit source]

#!/bin/sh

###
# For Alpine Linux
#
# A convenience script that combines
# 'apk update', 'apk upgrade -s' and 'apk upgrade'
#
# SPDX-License-Identifier: CC0-1.0
##

# Update the apk cache
printf "Updating apk cache...\n"
apk update || exit

# Check for upgradable packages
printf "\nChecking for upgradable packages..."
UPGRADABLE=$(apk upgrade -s)

if echo "$UPGRADABLE" | grep -q "Upgrading"; then
	echo "The following packages can be upgraded:"
	echo "$UPGRADABLE"
	echo
	echo "Do you want to upgrade the packages? [y/N]: "
	read -r RESPONSE
	case "$RESPONSE" in
		[yY][eE][sS]|[yY])
			echo "Upgrading packages..."
			apk upgrade
			;;
		*)
			echo "Upgrade cancelled."
			;;
	esac
else
	echo "No packages to upgrade."
fi

You can also download the latest version from https://git.tnonline.net/Forza/misc

Related Links[edit | edit source]

APK, the Alpine Package Keeper is the package management tool used on Alpine Linux.