Local APK cache: Difference between revisions

From Alpine Linux
mNo edit summary
(shortening with proper subsections for every case, special cases at the end)
Line 7: Line 7:
----
----


Alpine Linux is able to install packages from local media into RAM when booting, even before there is a network connection.
Alpine Linux can automatically (re-)install packages from local media into RAM when booting, even before there is a network connection.


To make the last installed packages available during boot, apk supports keeping a cache of the installed packages on a local disk.   
To have the packages available during boot, apk can keep a cache of installed packages on a local disk.   


The cache can be stored on any writable media, or in the same location as the .apkovl file from the [[Alpine_local_backup| local backup utility <code>lbu</code>]].
The cache can be stored on any writable media, or in the same location as the .apkovl file from the [[Alpine_local_backup| local backup utility <code>lbu</code>]].


= Enabling Local Cache =
= 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: {{Cmd|setup-apkcache}}
== Enabling Local Cache with current releases ==


== To enable Local Cache on releases prior v2.3 ==
To enable a local cache execute: {{Cmd|setup-apkcache}}
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 ==
The script creates a symlink named ''/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:
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.
{{Cmd|mkdir -p /var/cache/apk
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.


== 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 }}
{{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 =
Over time, newer packages will replace older ones; the cache directory will contain all older versions of packages.   
Over time, newer packages will replace older ones; the cache directory will contain all older versions of packages.   


== Delete old packages ==
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}}
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}}


Line 65: Line 53:




= Using the Local Cache with tmpfs volumes =
 
 
= Special Caching Cases =
 
== Enabling Local Cache on HDD installs ==
 
Note that HDD 'sys' installs don't need an apk cache to maintain their state, it allows to serve packages over the network, though, e.g. to get installed by other local machines.
 
Manually create a cache dir and then symlink it to ''/etc/apk/cache'':
{{Cmd|mkdir -p /var/cache/apk
ln -s /var/cache/apk /etc/apk/cache}}
 
== Manually Enabling Local Cache (required with releases prior to v2.3) ==
 
# Create a '''cache''' directory on the storage device where you keep the lbu backups (typically, <code>/dev/sda1</code>.)  {{Cmd| mkdir /media/sda1/cache }}
{{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}}
{{Tip|If you needed to remount your disk read-write before, run {{Cmd|mount -o remount,ro /media/sda1}} now that you are done with saving the changes}}
 
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.
 
 
== 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.
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.


Line 81: Line 93:




[[Category:Package Manager]]
 




Line 126: Line 138:
Click '''Save'''
Click '''Save'''
-->
-->
[[Category:Package Manager]]

Revision as of 15:22, 8 May 2020

This page seems to be a redundant copy, taken out of context from Alpine_Linux_package_management#Local_Cache

Please check to fully including this in Alpine_Linux_package_management#Local_Cache, and avoid/remove the redundant duplication at Local_APK_cache.



Alpine Linux can automatically (re-)install packages from local media into RAM when booting, even before there is a network connection.

To have the packages available during boot, apk can keep a cache of installed packages on a local disk.

The cache can be stored on any writable media, or in the same location as the .apkovl file from the local backup utility lbu.

Enabling Local Cache

Enabling Local Cache with current releases

To enable a local cache execute:

setup-apkcache

The script creates 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.


Cache maintenance

Over time, newer packages will replace older ones; the cache directory will contain all older versions of packages.

To clean out older versions of packages, run the clean command.

apk cache clean

or to see what is deleted

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.
Note: Custom shutdown commands were formerly added to a /etc/conf.d/local; but that method is now deprecated.



Special Caching Cases

Enabling Local Cache on HDD installs

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

Manually create a cache dir and then symlink it to /etc/apk/cache:

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

Manually Enabling Local Cache (required with releases prior to v2.3)

  1. Create a cache directory on the storage device where you keep the lbu backups (typically, /dev/sda1.)

    mkdir /media/sda1/cache

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

mount -o remount,rw /media/sda1

and then don't forget to run

mount -o remount,ro /media/sda1

when you are done with the following commands
  1. Create a symlink to this directory from /etc/apk/cache.

    ln -s /media/sda1/cache /etc/apk/cache

  2. Run an lbu commit to save the change (/etc/apk/cache is in /etc and is automatically backed up.)

    lbu commit

Tip: If you needed to remount your disk read-write before, run

mount -o remount,ro /media/sda1

now that you are done with saving the changes

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.


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 per usual, if you want to download currently installed packages into the cache, use apk cache sync.