Easily Change Your Ubuntu Mirror for Faster Package Downloads

Last Updated: 2023-01-13

We saw that archive.ubuntu.com had gone down and was quite slow on our CI, therefore we knew we needed to update the Ubuntu mirror.

One solution to this problem is to alter your system’s default Ubuntu mirror. Changing the mirror can significantly increase download speeds, especially if you live far away from the default mirror location. We’ll show you how to effortlessly modify your Ubuntu mirror for speedier package downloads in this blog post.

We will cover different methods, including using the command line, editing the sources.list file and using a Dockerfile. We will also discuss the importance of updating the package list after changing the mirror and some tips to optimize the process. Whether you’re a developer, system administrator, or a casual Ubuntu user, this post will help you get the most out of your Ubuntu system.

Finding mirrors

When looking for an Ubuntu mirror, there are a few different places to check. The official Ubuntu website (https://ubuntu.com/) provides a list of official mirrors, which can be found at https://launchpad.net/ubuntu/+archivemirrors. These mirrors are maintained by the Ubuntu community and are generally considered to be reliable. Additionally, there are many third-party mirrors available, which can be found by searching for “Ubuntu mirrors” on your preferred search engine.

Be sure to check the location of the mirror and its uptime before using it, as some mirrors may be located in different regions or may not be frequently updated. It’s also important to note that some mirrors may provide different versions of packages and updates, it’s best to check the mirror’s website and read the information provided.

Base commands to use

To change the Ubuntu mirror, you can use the sed command to edit the /etc/apt/sources.list file and replace the current mirror URL with the desired one. Here’s an example of how you might do this:

Copy code

sudo sed -i 's|http://archive.ubuntu.com/ubuntu|http://NEW_MIRROR_URL|g' /etc/apt/sources.list

You can also use the ‘sed’ command to edit the sources.list file and add the desired mirror to the file before you run your command.

Copy code

echo "deb http://NEW_MIRROR_URL/ubuntu bionic main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list

This will append the new mirror url in the sources.list file, you can then run your command that uses apt-get or apt package manager.

You should replace “NEW_MIRROR_URL” with the actual URL of the mirror you want to use.

Update Ubuntu

In order to update Ubuntu, you can use the apt-get package manager. The basic command to update all installed packages is:

Copy Code

sudo apt-get update
sudo apt-get upgrade

Now that we’ve covered how to update the mirror url on a standalone machine, how about on a CI or Dockerfile?

Change it in Circle CI

In CircleCI, you can run the sed command to change the Ubuntu mirror in a config.yml file, which is used to configure your CircleCI build. Here’s an example of how you might do this:

Copy code

version: 2.1

jobs:
  build:
    working_directory: ~/repo
    steps:
    - run:
        name: Change Ubuntu mirror
        command: |
          sudo sed -i 's|http://archive.ubuntu.com/ubuntu|http://NEW_MIRROR_URL|g' /etc/apt/sources.list
          apt-get update

You can also use the echo command to add the mirror to the sources.list file before you run your command

Copy code

version: 2.1

jobs:
  build:
    working_directory: ~/repo
    steps:
    - run:
        name: Change Ubuntu mirror
        command: |
          echo "deb http://NEW_MIRROR_URL/ubuntu bionic main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
          apt-get update

You should replace “NEW_MIRROR_URL” with the actual URL of the mirror you want to use.

It’s important to note that you may need to include a sudo command before the sed command if you are running it on a CircleCI build that is using a container with a non-root user.

Also, you should update the package lists after changing the mirrors. You can do this by running the command apt-get update after the sed command.

It’s also important to note that this only change the mirror for package downloading not for apt-get upgrade or apt-get dist-upgrade.

Change ubuntu mirror in Dockerfile

You can change the Ubuntu mirror in a Dockerfile by using the RUN command to execute the sed command as shown above. Here’s an example of how you might do this:

Copy code:

RUN sed -i 's|http://archive.ubuntu.com/ubuntu|http://NEW_MIRROR_URL|g' /etc/apt/sources.list

or

Copy code:

RUN echo "deb http://NEW_MIRROR_URL/ubuntu bionic main restricted universe multiverse" | tee -a /etc/apt/sources.list

You should replace “NEW_MIRROR_URL” with the actual URL of the mirror you want to use.

It’s important to note that the changes made to the sources.list file in a Dockerfile will be discarded when the container stops or exits. If you want to make the change permanent, you can use a volume to mount the sources.list file to the host machine.

You should also make sure that the package list is updated after the change in the sources.list file. You can do this by running the command

Copy code:

RUN apt-get update

It’s also important to note that this only change the mirror for package downloading not for apt-get upgrade or apt-get dist-upgrade.

Change in Github Actions

To modify the Ubuntu mirror in a.yml workflow file, use the sed command in GitHub Actions. Here’s an example of how you could go about it:

Copy code

name: Ubuntu Mirror

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Change Ubuntu mirror
      run: |
        sudo sed -i 's|http://archive.ubuntu.com/ubuntu|http://NEW_MIRROR_URL|g' /etc/apt/sources.list
        apt-get update

You can also use the echo command to add the mirror to the sources.list file before you run your command

Copy code

name: Ubuntu Mirror

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Change Ubuntu mirror
      run: |
        echo "deb http://NEW_MIRROR_URL/ubuntu bionic main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
        apt-get update

You should replace “NEW_MIRROR_URL” with the actual URL of the mirror you want to use.

It’s important to note that you may need to include a sudo command before the sed command if you are running it on a GitHub Actions workflow that is using a container with a non-root user.

Also, you should update the package lists after changing the mirrors. You can do this by running the command apt-get update after the sed command.

It’s also important to note that this only change the mirror for package downloading not for apt-get upgrade or apt-get dist-upgrade.

In addition, you should also make sure that your workflow has the proper permissions to run commands that require sudo and that the commands are compatible with the specific version of Ubuntu that you are using.

In conclusion, changing your Ubuntu mirror can greatly improve your package download speeds and overall system performance. Whether you’re a developer, system administrator, or a casual Ubuntu user, the methods outlined in this blog post can help you easily change your Ubuntu mirror and optimize your system.

Remember to update your package list after changing the mirror and to always check the location and uptime of the mirror before using it. With these tips in mind, you can enjoy faster package downloads and a more efficient Ubuntu system. Thanks for reading and happy optimizing