Apt-Cacher on Raspberry Pi

When you’re developing an application on Raspberry Pi, you might have to install a few dependencies packages from an APT repository. Sometimes you break some applications and a clean install is probably your last resort, and you find yourself downloading the same packages over-and-over again.

In my case, I was running the pi-gen shell script available from https://github.com/RPi-Distro/pi-gen. This shell script is very handy when you want to create your own custom Raspberry Pi OS image, with the packages you need. This is very handy in production, where you want the process to be as simple as burning an image to an SD card, and you’re good to go.

However, when developing the shell script to produce the proper image I need, I find myself sometimes breaking the script and often had to restart it all over again, downloading the same packages from the APT repository. There’s actually a solution for this problem, create an APT cache in your local network in a Debian box (or a spare Raspberry Pi). I use my Pi.hole Raspberry Pi for this APT Cacher. Installation is simple, it’s only by:

# apt install apt-cacher-ng

Once you’re done, you can use that spare Raspberry Pi as an apt cacher. You can simply add a line to your pi-gen script config:

APT_PROXY='http://pi.hole:3142'

This works like a charm and I can shave 9 minutes from downloading the packages from repository since now I have it cached.

[06:58:17] Begin /home/vagrant/pi-gen
[06:58:17] Begin /home/vagrant/pi-gen/stage0
...
[07:13:32] End /home/vagrant/pi-gen/stage2

(15.25 minutes)

[06:45:27] Begin /home/vagrant/pi-gen
[06:45:27] Begin /home/vagrant/pi-gen/stage0
...
[07:09:48] End /home/vagrant/pi-gen/stage2

(24.35 minutes)

By using this APT cache, you can save a few minutes (depending on your network speed) downloading the same base packages over-and-over when you need to develop a bunch of images at the same time.