Local APK cache: Difference between revisions

From Alpine Linux
m (Removed reference to yum and apt-get)
(cleaned up headlines organized content. removed information related to older versions ( already commented out))
 
(30 intermediate revisions by 7 users not shown)
Line 1: Line 1:
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.
<!-- NOTE This page ([[Local_APK_cache]]) also gets included at [[Alpine_Linux_package_management#Local_Cache]] to avoid duplication. -->
=== Overview ===


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.
APK can keep a cache of installed packages on a local disk.  


The cache is enabled by creating a symlink named ''/etc/apk/cache'' that points to the cache directory.
In [[Installation#Diskless_Mode|diskless]] installations, since these packages are available during boot, packages can then be automatically (re-)installed from local media into RAM when booting, without requiring, and even before there is a network connection. The cache can be stored on any writable media, or at the same location as the .apkovl file from the [[Alpine_local_backup| local backup utility <code>lbu</code>]].


To enable local cache run: {{Cmd|setup-apkcache}}
HDD or [[Installation#System_Disk_Mode|sys mode]] installs don't need an apk cache to maintain their state, it still allows to serve packages over the network, though, e.g. to get installed by other local machines.


= To enable Local Cache on releases prior v2.3 =
=== Enabling Local Cache ===
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 ==
Execute the script <code>setup-apkcache</code> will assist in enabling a local cache. The script creates a symlink named {{Path|/etc/apk/cache}} that points to the cache directory.
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:
{{Cmd|setup-apkcache}}
 
Cache can also be manually enabled by creating a cache dir and then symlink it to {{Path|/etc/apk/cache}}:
{{Cmd|mkdir -p /var/cache/apk
{{Cmd|mkdir -p /var/cache/apk
ln -s /var/cache/apk /etc/apk/cache}}
ln -s /var/cache/apk /etc/apk/cache}}


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.
On a [[Installation#Diskless_Mode|diskless]] installation, to make the disk where the cache directory is present be automatically mounted at boot create an empty file {{Path|.boot_repository}} inside the cache directory.


== To manually enable Local Cache on run-from-RAM installs ==
=== Cache maintenance ===


# Create a '''cache''' directory on the device you store your lbu backups (typically, <code>/dev/sda1</code>.)  {{Cmd| mkdir /media/sda1/cache }}
==== Removing older packages ====
{{Tip|If you get an error that says "mkdir: can't create directory '/media/usbdisk/cache': Read-only file system", then you probably need to remount your disk read-write temporarily.  Try {{Cmd|mount -o remount,rw /media/sda1}} and then don't forget to run {{Cmd|mount -o remount,ro /media/sda1}} when you are done with the following commands}}
# Create a symlink to this directory from <code>/etc/apk/cache</code>.  {{Cmd|ln -s /media/sda1/cache /etc/apk/cache}}
# Run an lbu commit to save the change (<code>/etc/apk/cache</code> is in <code>/etc</code> and is automatically backed up.) {{Cmd|lbu commit}}
# 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.


= Cache maintenance =
When newer packages are added to the cache over time, the older versions of the packages default to remain in the cache directory.   
Over time, newer packages will replace older ones; the cache directory will contain all older versions of packages.   


== Delete old packages ==
The older versions of packages can be removed with the '''clean''' command.  {{cmd|apk cache clean}} Or to see what is deleted include the verbose switch: {{cmd|apk -v cache clean}}
To clean out older versions of packages, run the '''clean''' command.  {{cmd|apk cache clean}} or to see what is deleted {{cmd|apk -v cache clean}}


== Download missing packages ==
==== Download missing packages ====
If you accidentally delete packages from the cache directory, you can make sure they are there with the '''download''' command, {{cmd|apk cache download}}
If you accidentally delete packages from the cache directory, you can make sure they are there with the '''download''' command, {{cmd|apk cache download}}


== Delete and download in one step ==
==== Delete and download in one step ====
You can combine the two steps into one with the '''sync''' command - this cleans out old packages and downloads missing packages. {{cmd|apk cache -v sync}}
You can combine the two steps into one with the '''sync''' command - this cleans out old packages and downloads missing packages. {{cmd|apk cache -v sync}}


== Automatically Cleaning Cache on Reboot ==
==== Automatically Cleaning Cache on Reboot ====
To automatically attempt to validate your cache on reboot, you can add the above command to a {{Path|/etc/local.d/*.stop}} file:
To automatically attempt to validate your cache on reboot, you can add the above command to a {{Path|/etc/local.d/*.stop}} file:


{{Cat|/etc/local.d/cache.stop|#!/bin/sh
{{Cat|/etc/local.d/cache.stop|#!/bin/sh
# verify the local cache on shutdown
# verify the local cache on shutdown
apk cache -v sync
apk cache -v sync
Line 50: Line 47:
{{Tip|Usually the only time you need to reboot is when things have gone horribly wrong; so this is a "best effort" to cover forgetting to sync the cache; It is much better to run '''sync''' immediately after adding or upgrading packages.}}
{{Tip|Usually the only time you need to reboot is when things have gone horribly wrong; so this is a "best effort" to cover forgetting to sync the cache; It is much better to run '''sync''' immediately after adding or upgrading packages.}}


{{Note|Custom shutdown commands were formerly added to a {{Path|/etc/conf.d/local}}; but that method is now deprecated.}}
=== Local Cache on tmpfs volumes ===
 
[[Category:Package Manager]]


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 {{Path|/etc/apk/cache}}.
{{Cmd|<nowiki>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 -t ext2 /apkcache.img /etc/apk/cache
apk update</nowiki>}}


== Enabling Package Caching (Older) ==
As usual, if you want to download currently installed packages into the cache, use ''apk cache sync''.


{{Note|This document applies to Alpine 1.9 and later versions only}}
[[Category:Package Manager]]
 
Package caching is useful when the need arises to upgrade packages on read-only media. Package caching allows you to store newer packages in a location on writable media, which Alpine checks for when loading packages on start-up.
 
This can be enabled either from the console or through the ACF.
 
{{Tip|For package upgrades, it is useful to point Alpine Package Manager to repositories on the internet, to easily obtain package upgrades when they become available.}}
 
{{:Include:Using_Internet_Repositories_for_apk-tools}}
 
== Enable caching from either console or ACF ==
 
Choose one of the two methods below to enable, either from the console or from the ACF web interface.
 
=== Option 1: Enable caching from the console ===
 
''Substitute 'sda1' in the example below for the actual device you will use to store the caching directory.''
 
First upgrade apk-tools:
{{Cmd|apk add –u apk-tools}}
 
Now enable package caching:
{{Cmd|mkdir –p /media/sda1/cache
ln –s /media/sda1/cache /etc/apk/cache}}
 
=== Option 2: From the ACF ===
 
Browse to '''System > Packages > Cache'''
 
''Edit Cache Settings:''
 
Tick '''Enable Cache'''
 
Specify the '''Cache Directory''', for example:
/media/sda1/cache
 
Click '''Save'''
-->

Latest revision as of 10:45, 12 October 2024

Overview

APK can keep a cache of installed packages on a local disk.

In diskless installations, since these packages are available during boot, packages can then be automatically (re-)installed from local media into RAM when booting, without requiring, and even before there is a network connection. The cache can be stored on any writable media, or at the same location as the .apkovl file from the local backup utility lbu.

HDD or sys mode installs don't need an apk cache to maintain their state, it still allows to serve packages over the network, though, e.g. to get installed by other local machines.

Enabling Local Cache

Execute the script setup-apkcache will assist in enabling a local cache. The script creates a symlink named /etc/apk/cache that points to the cache directory.

setup-apkcache

Cache can also be manually enabled by creating a cache dir and then symlink it to /etc/apk/cache:

mkdir -p /var/cache/apk ln -s /var/cache/apk /etc/apk/cache

On a diskless installation, to make the disk where the cache directory is present be automatically mounted at boot create an empty file .boot_repository inside the cache directory.

Cache maintenance

Removing older packages

When newer packages are added to the cache over time, the older versions of the packages default to remain in the cache directory.

The older versions of packages can be removed with the clean command.

apk cache clean

Or to see what is deleted include the verbose switch:

apk -v cache clean

Download missing packages

If you accidentally delete packages from the cache directory, you can make sure they are there with the download command,

apk cache download

Delete and download in one step

You can combine the two steps into one with the sync command - this cleans out old packages and downloads missing packages.

apk cache -v sync

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:

Contents of /etc/local.d/cache.stop

#!/bin/sh # verify the local cache on shutdown apk cache -v sync # We should always return 0 return 0
Tip: Usually the only time you need to reboot is when things have gone horribly wrong; so this is a "best effort" to cover forgetting to sync the cache; It is much better to run sync immediately after adding or upgrading packages.

Local Cache on 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 -t ext2 /apkcache.img /etc/apk/cache apk update

As usual, if you want to download currently installed packages into the cache, use apk cache sync.