Speeding Up Automation Using Apt Mirror URIs
If you use some dev or build automation that spins up new Ubuntu images you will one day see them slow to a crawl when they are accessing the default installation mirror. It is annoying waiting for Docker or Vagrant at the best of times but if a mirror gets slow enough it can be infuriating or even break your builds.
Many people know about the various tools that let you select a fast mirror for Ubuntu. This post is about a nice feature that most people do not know about.
By default, you will be using
Apt supports a mirror URI which will automatically select a close mirror for you. This is the feature most people don't know about:
To use this you need to edit the apt sources list. Of course, you need this done in an automated fashion which is why the mirror syntax is so handy, you don't need to run a program, just do some substitutions in the sources file.
Here is how I do it with Vagrant. I included my boilerplate Ansible playbook too. You don't have to copy and paste, the files are also on Github: https://github.com/mjeg/apt-mirror-vagrant
Vagrant.configure(2) do |config| config.vm.box = "ubuntu/bionic64" config.vm.provision :shell, :inline => "sed -i 's|deb http://archive.ubuntu.com.ubuntu|deb mirror://mirrors.ubuntu.com/mirrors.txt|g' /etc/apt/sources.list" config.vm.provision "shell", inline: "sed -i '/deb-src/d' /etc/apt/sources.list" config.vm.provision "shell", inline: "apt-get update -y" config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end end
--- - hosts: all gather_facts: no pre_tasks: - name: 'install python2' raw: sudo apt-get -y install python tasks: - name: Install all the debs become: yes apt: name: - python-setuptools - python3-pip - build-essential state: present update_cache: yes
After the VM has booted but before Ansible is run we swap out the apt mirrors using the shell provisioning feature of Vagrant.
You will notice that I also remove
deb-src from the repo list. This speeds things up a little bit. However, the main point of doing this is not to slightly speed things up. It will save you from the day when
archive.ubuntu.com is running dog slow and you want to hulk smash everything.