I recently ran into an issue with a cloned Ubuntu 24.04 template that was using as a Bitbucket runner, so it didn't need anything too special. However, after spinning it up, I noticed trying to install packages was brutally slow. Was getting like 20-35 kb/sec downloads. Here are ultimately the tweaks and steps I took to get the apt update working fast once again:
Step 1: Force APT to use IPv4
If you are not using IPv6 in your environment, this keeps APT from wasting time trying broken or unreachable IPv6 paths.
echo 'Acquire::ForceIPv4 "true";' | sudo tee /etc/apt/apt.conf.d/99force-ipv4
You can make sure it "sticks" with the following:
cat /etc/apt/apt.conf.d/99force-ipv4
You should see something like this:
Acquire::ForceIPv4 "true";
Step 2: Change Ubuntu repositories from HTTP to HTTPS
This is the one that I found made the biggest difference, since it looks like it is hitting cloudflare IPs. On Ubuntu 24.04 (Noble), edit the .sources file with this:
sudo sed -i \
's| http://archive.ubuntu.com/ubuntu|https://archive.ubuntu.com/ubuntu|g;
s| http://security.ubuntu.com/ubuntu|https://security.ubuntu.com/ubuntu| g' \
/etc/apt/sources.list.d/ubuntu.sources
Make sure of your changes.
cat /etc/apt/sources.list.d/ubuntu.sources
You should now see:
URIs: https://archive.ubuntu.com/ubuntu
and this also:
URIs: https://security.ubuntu.com/ubuntu
Step 3: Refresh APT
sudo apt clean
sudo apt update
Step 4: Test download speed
To make sure of your speed improvement, below is just an example that I used since I was trying to install JAVA:
wget -4 -O /dev/null \
https://archive.ubuntu.com/ubuntu/pool/main/o/openjdk-17/openjdk-17-jre-headless_17.0.19 +10-1~24.04.2_amd64.deb
45.86 MB in 3.2 seconds
≈14.5 MB/sec
