Reuse packages between builds¶
When Snapcraft downloads packages while it builds a snap, it doesn’t store them persistently. On subsequent builds that rely on those same packages, on any project, Snapcraft will download them again, costing time and bandwidth.
By setting up a cache, your packages will be reused across all snap builds. For an example of the performance gain that a cache provides, the ROS2 Talker/Listener snap packs 52% faster with a cache than without.
Requirements¶
There are multiple ways to set up a package cache, depending on your deployment and network and storage needs. What follows is a cache for all HTTP traffic that requires:
- A local Linux host 
- LXD as the build provider 
Set up the proxy¶
If you’re on a host that has never run Snapcraft before, start by packing a snap to create the LXD image:
snapcraft pack
Create a new Ubuntu 24.04 container instance for the proxy, reusing the Snapcraft namespace in LXD.
lxc launch ubuntu:24.04 package-cache --project snapcraft
Install and configure the proxy¶
Enter the container.
lxc shell package-cache --project snapcraft
Once inside the container, install Squid.
apt install squid
Next, we’ll configure Squid. Before you begin, it’s a good idea to back up the default configuration.
cp /etc/squid/squid.conf /etc/squid/squid.conf.orig
chmod a-w /etc/squid/squid.conf.orig
In /etc/squid/squid.conf, set the following options.
# Allow http for the local network
http_access allow localnet
# Amount of memory used for live objects
cache_mem 1000 MB
maximum_object_size_in_memory 256 MB
# Cold storage of ~4GiB
cache_dir ufs /var/spool/squid 4096 16 256
# Cache more IPs than the default
# APT can easily hit hundreds of URLs when installing deep dependencies
ipcache_size 16384
# Prevent others from tracking the traffic of the proxy server
via off
forwarded_for delete
If you need to tailor the Squid configuration
Configuring Squid in the Squid Cache wiki covers many different networking and storage cases.
Restart Squid so the configuration takes effect:
systemctl restart squid.service
Before exiting the container, run tail to continuously print the server’s logs to
the output.
tail -f /var/log/squid/access.log
Exit the container by pressing Ctrl + D.
Integrate with Snapcraft¶
With the container for the proxy server configured and running in the background, you can begin accessing the APT cache with Snapcraft.
When you launch Snapcraft, pass the proxy server to it with the http_proxy
environment variable.
http_proxy="http://package-cache.lxd:3128/" snapcraft pack
Alternatively, you can pass the IP address of the proxy server. Copy the IP address from LXC:
lxc list --project snapcraft | grep package-cache
Then, when launching Snapcraft, pass the IP address to http_proxy:
http_proxy="http://<package-cache-ip>:3128/" snapcraft pack
Monitor the cache¶
While a build is running, you can monitor the cache’s activity in a separate terminal.
Run tail to continuously print the Squid log to the output.
lxc exec package-cache --project snapcraft -- tail -f /var/log/squid/access.log
