Ubuntu – How to use the mirror option of apt-cacher-ng for debootstrap

debootstrapscripts

I run debootstrap like this:

sudo debootstrap --verbose --arch=amd64 zesty chroot

I have already installed apt-cacher-ng.

How do I make them work together so that when I run debootstrap multiple times I use the cache instead of repeatedly downloading the same packages?

I have tried using the MIRROR option and setting the http_proxy variable but it errors when I run offline:

$ sudo http_proxy="http://localhost:3142" debootstrap --verbose --arch=amd64 zesty chroot http://localhost:3142/uburep
I: Retrieving InRelease 
I: Failed to retrieve InRelease
I: Retrieving Release 
E: Failed getting release file http://localhost:3142/uburep/dists/zesty/Release

I used http://localhost:3142/uburep because that is what I found in the cache:

$ find /var/cache/apt-cacher-ng -name Release
/var/cache/apt-cacher-ng/security.ubuntu.com/ubuntu/dists/zesty-security/Release
/var/cache/apt-cacher-ng/uburep/dists/zesty-updates/Release
/var/cache/apt-cacher-ng/uburep/dists/zesty-backports/Release
/var/cache/apt-cacher-ng/uburep/dists/zesty/Release

I tried adding Defaults env_keep += "http_proxy https_proxy ftp_proxy" to /etc/sudoers (via sudo visudo) but it didn't help either.

If I remove the MIRROR option and run debootstrap offline it then doesn't seem to use apt-cacher-ng at all (note how it is not looking for localhost any more).

E: Failed getting release file http://archive.ubuntu.com/ubuntu/dists/zesty/Release

Best Answer

There is a trick to get this to work without explicit proxy settings. You can in fact use your configured apt-cacher-ng as a mirror URL like so:

http://localhost:3142/us.archive.ubuntu.com/ubuntu/

So as you can see after specifying the base URL to your apt-cacher-ng instance you can then give a server and a path as per this Wiki article. I've used this method in my own bootstrap scripts and it works like a charm.

Note: you may want to verify that your apt-cacher-ng instance is running by checking with lsof -i :3142 (requires the lsof package).