Local APK cache
Alpine Linux needs to be able to pull packages from local media on boot. (You can't download packages from the net before you have a network connection.) Using remote repositories presents a problem. If the config files have been modified for a newer version of a package, and the older package is on local media, all sorts of fun can result.
The solution is a local cache of updated packages. This cache can be stored on any r/w media, typically the same location as the apkovl.
- 1 Enabling Local Cache
- 2 Cache maintenance
- 3 Using the Local Cache with tmpfs volumes
Enabling Local Cache
The cache is enabled by creating a symlink named /etc/apk/cache that points to the cache directory. Note that apk will ignore any cache residing on a tmpfs volume. If you want this for some reason, see section below on tmpfs caches.To enable local cache run:
To enable Local Cache on releases prior v2.3
Alpine Linux version prior to v2.3 does not have the setup-apkcache tool so the symlink needs to be set up manually.
To manually enable Local Cache on HDD install
If you've installed Alpine to your hard drive (as 'sys'), then create a cache dir and then an /etc/apk/cache symlink pointing to that dir:
You normally don't need apk cache on HDD 'sys' installs but it might be handy if you re-install from net to have the packages cached.
To manually enable Local Cache on run-from-RAM installs
- Create a cache directory on the device you store your lbu backups (typically,
- Create a symlink to this directory from
- Run an lbu commit to save the change (
/etcand is automatically backed up.)
- Done. Now whenever you run an apk command that pulls a new package from a remote repository, the package is stored on your local media. On startup, Alpine Linux will check the local cache for new packages, and will install them if available.
Over time, newer packages will replace older ones; the cache directory will contain all older versions of packages.
Delete old packagesTo clean out older versions of packages, run the clean command. or to see what is deleted
Download missing packagesIf you accidentally delete packages from the cache directory, you can make sure they are there with the download command,
Delete and download in one stepYou can combine the two steps into one with the sync command - this cleans out old packages and downloads missing packages.
Automatically Cleaning Cache on Reboot
To automatically attempt to validate your cache on reboot, you can add the above command to a /etc/local.d/*.stop file:
Using the Local Cache with tmpfs volumes
In some circumstances it might be useful to have the cache reside on tmpfs, for example if you only wish for it to last as long as the system is up.
NOTE: apk is coded to ignore tmpfs caches, and this is correct behaviour in most instances. Using tmpfs as a package cache can consume large amounts of system memory if you install a lot of packages, possibly resulting in a crashed system. You can limit this by restricting the size of your cache to a small number (128M in the example below).
To do it, you need to create an image inside which your cache can live. We do this by creating an image file, formatting it with ext2, and mounting it at /etc/apk/cache.
- apk add e2fsprogs
- dd if=/dev/zero of=/apkcache.img bs=1M count=128
- mkfs.ext2 -F /apkcache.img
- mkdir -p /etc/apk/cache
- mount /cache.img /etc/apk/cache
- apk update
As per usual, if you want to download currently installed packages into the cache, use apk cache sync.