Using A Raspberry Pi As An Apt Cache (Raspberry Pi)

If you have multiple Linux devices (e.g. Raspberry Pis) you may find it helpful to set up an apt cache to help with updates, especially if you have a poor internet connection or limited data allowance.

An apt cache works by caching (i.e. saving) all the updates on one device so that other devices can download them from the first device instead of having to connect to the online repository.

The RaspberryPi.org archive repository
The RaspberryPi.org archive repository

When apt-get update / apt-get upgrade is run it normally connects to an online repository, e.g. http://archive.raspberrypi.org. These repository addresses are saved in the following files:

  • /etc/apt/sources.list
  • /etc/apt/sources.list.d/raspi.list

Today I am going to point them to a local Raspberry Pi so that the other Pis on my home network can get their updates from that Pi. I am going to use a Raspberry Pi 4 and a Raspberry Pi Zero W in this example.

  • The Pi 4 is going to be the server (hosting the updates post download).
  • The Pi Zero W is going to be the client, which needs updates.
Installing apt-cacher-ng
Installing apt-cacher-ng

On the Pi 4 I have run sudo apt install apt-cacher-ng to install the apt-cacher-ng program. During install it asks about creating HTTP tunnels, I went with the default answer of “No”.

geektechstuff_apt-cache-3
HTTP tunnel via apt-cacher-ng

The install took a few moments on the Pi 4, which I followed with a sudo apt-get update and sudo apt-get upgrade, and then a reboot. Some sources online discussed making changes post install, but the official documentation says they should not be required.

On the Pi Zero W I edited the files mentioned earlier using:

  • sudo nano /etc/apt/sources.list
  • sudo nano /etc/apt/sources.list.d/raspi.list
sources.list file
sources.list file
raspi.list file
raspi.list file

I changed the lines:

http://archive.raspberrypi.org/debian to http://IP_OF_PI_4:PORT/archive.raspberrypir.org/debian

and

http://raspbian.raspberrypi.org/raspbian to http://IP_OF_PI_4:PORT/raspbian.raspberrypi.org/raspbian

By default apt-cacher-ng uses port 3142.

geektechstuff_apt-cache-7
Altered sources.list file

After saving the modified files, I ran sudo apt-get update on the Pi Zero W and it started to query the Pi 4 for the updates.

Pi Zero asking Pi 4 for updates
Pi Zero asking Pi 4 for updates
Pi Zero asking Pi 4 for updates
Pi Zero asking Pi 4 for updates

If you want to check the cache folders on the server (in my case the Pi 4), then by default apt-cacher-ng uses the location /var/cache/apt-cacher-ng/

/var/cache/apt-cacher-ng/
/var/cache/apt-cacher-ng/

By default apt-cacher-ng stores log files under /var/log/apt-cacher-ng

/var/log/apt-cacher-ng
/var/log/apt-cacher-ng

The log files should show the connections from computers as they query the device for updates.

geektechstuff_apt-cache-13
apt-cacher log

If you want to chance the log or cache location then use nano to edit /etc/apt-cacher-ng/acng.conf

/etc/apt-cacher-ng/acng.conf
/etc/apt-cacher-ng/acng.conf

Note: If you are using a firewall, make sure to open the relevant ports for access. If you have multiple devices that need the sources.list files updating consider using Ansible.

One thought on “Using A Raspberry Pi As An Apt Cache (Raspberry Pi)

  1. Brilliant. Can one use the alphanumeric name (i.e., “raspberrypi.local”) to access instead of an IP address? I don’t use fixed IP addresses for my pis unless truly necessary (usually isn’t necessary).

    Like

Comments are closed.