How to Setup Local APT Repository Server on Ubuntu 20.04

One of the reasons why you may consider setting up a local apt repository server is to minimize the bandwidth required if you have multiple instances of Ubuntu to update. Take for instance a situation where you have 20 or so servers that all need to be updated twice a week. You could save a great deal of bandwidth because all you need to do is to updates all your systems over a LAN from your local repository server.

In this guide, you will learn how to set up a local apt repository server on Ubuntu 20.04 LTS.


  • Ubuntu 20.04 LTS system
  • Apache Web Server
  • Minimum of 170 GB free disk space on /var/www/html file system
  • Stable internet connection

Step 1) Create a local Apache Web Server

First off, log in to your Ubuntu 20.04 and set up the Apache web server as shown.

$ sudo apt install -y apache2

Enable Apache2 service so that it will be persistent across the reboot . Run following command

$ sudo systemctl enable apache2

Apache’s default document root directory is located in the /var/www/html path. We are later going to create a repository directory in this path that will contain the required packages needed.

Step 2) Create a package repository directory

Next, we will create a local repository directory called ubuntu in the /var/www/html path.

$ sudo mkdir -p /var/www/html/ubuntu

Set the required permissions on above created directory.

$ sudo chown www-data:www-data /var/www/html/ubuntu

Step 3) Install apt-mirror

The next step is to install apt-mirror package, after installing this package we will get apt-mirror command or tool which will download and sync the remote debian packages to local repository on our server. So to install it, run following

$ sudo apt update
$ sudo apt install -y apt-mirror

Step 4) Configure repositories to mirror or sync

Once apt-mirror is installed then its configuration ‘/etc/apt/mirrror.list’ is created automatically. This file contains list of repositories that will be downloaded or sync in local folder of our Ubuntu server. In our case local folder is ‘/var/www/html/ubuntu/’. Before making changes to this file let’s backup first.

$ sudo cp /etc/apt/mirror.list /etc/apt/mirror.list-bak

Now edit the file using vi editor and update base_path and repositories as shown below.

$ sudo vi /etc/apt/mirror.list

############# config ###################
set base_path    /var/www/html/ubuntu
set nthreads     20
set _tilde 0
############# end config ##############
deb focal main restricted universe \
deb focal-security main restricted \
universe multiverse
deb focal-updates main restricted \
universe multiverse

Save and exit the file.


In case you might have noticed that I have used Ubuntu 20.04 LTS package repositories and have comment out the src package repositories as I don’t have enough space on my system. If you wish to download or sync src packages too then uncomment the lines which starts with ‘deb-src’.

Step 5) Start mirroring the remote repositories to local folder

Before start mirroring or syncing, first the copy the script to folder /var/www/html/ubuntu/var using below cp command.

$ sudo mkdir -p /var/www/html/ubuntu/var
$ sudo cp /var/spool/apt-mirror/var/ /var/www/html/ubuntu/var

Now, it’s time to start mirroring the packages from remote repositories to our system’s local folder. Execute below:

$ sudo apt-mirror


Above command can also be started in the background using below nohup command,

$ nohup sudo apt-mirror &

To monitor the mirroring progress use below,

$ tail nohup.out

In Ubuntu 20.04 LTS, apt-mirror does sync CNF directory and its files, so we have to manually download and copy the folder and its files. So to avoid manually downloading CNF directory, create a shell script with below contents,

$ vi
for p in "${1:-focal}"{,-{security,updates}}\
/{main,restricted,universe,multiverse};do >&2 echo "${p}"
wget -q -c -r -np -R "index.html*"\
wget -q -c -r -np -R "index.html*"\

save and close the script.

Execute the script to download CNF directory and its files.

$ chmod +x
$ bash

This script will create a folder with name ‘’ in the present working directory. Copy this folder to mirror folder,

$ sudo cp -av  /var/www/html/ubuntu/mirror/

Note : If we don’t sync cnf directory then on client machines we will get following errors, so to resolve these errors we have to create and execute above script.

E: Failed to fetch http://x.x.x.x/ubuntu/mirror/\
focal/restricted/cnf/Commands-amd64  404  Not Found [IP: 80]
E: Failed to fetch http://x.x.x.x/ubuntu/mirror/\
focal-updates/main/cnf/Commands-amd64  404  Not Found [IP: 80]
E: Failed to fetch http://x.x.x.x/ubuntu/mirror/\
focal-security/main/cnf/Commands-amd64  404  Not Found [IP: 80]

Scheduling Automatic Repositories Sync Up

Configure a cron job to automatically update our local apt repositories. It is recommended to setup this cron job in the night daily.

Run ‘crontab -e’ and add following command to be executed daily at 1:00 AM in the night.

$ sudo crontab -e

00  01  *  *  *  /usr/bin/apt-mirror

Save and close.

Note: In case Firewall is running on Ubuntu Server then allow port 80 using following command

$ sudo ufw allow 80

Step 6) Accessing Local APT repository via web browser

To Access our locally configured apt repository via web browser type the following URL:



Step 7) Configure Ubuntu 20.04 client to use local apt repository server

To test and verify whether our apt repository server is working fine or not, I have another Ubuntu 20.04 lts system where I will update /etc/apt/sources.list file so that apt command points to local repositories instead of remote.

So, login to the system, change the following in the sources.list

Here ‘’ is the IP Address of my apt repository server, replace this ip address that suits to your environment.

Also make sure comment out all other repositories which are not mirrored on our apt repository server. So, after making the changes in sources.list file, it would look like below:


Now Run ‘apt update’ command to verify that client machine is getting update from our local apt repository server,

$ sudo apt update


Perfect, above output confirms that client machine is successfully able to connect to our repository for fetching the packages and updates. That’s all from this article, I hope this guide helps you to setup local apt repository server on Ubuntu 20.04 system.

Also Read14 Useful ls Command Examples in Linux

15 thoughts on “How to Setup Local APT Repository Server on Ubuntu 20.04”

  1. Nice tutorial, but I get an blocking error, when I have tried to apt update && apt upgrade in the first time.

    I have founded that you need to add [trusted=yes] after deb instances in your sources.list.
    Otherwise, you will have the following error :

    After that, apt update && apt upgrade has been running successfully 🙂

  2. How often do you need to rerun the script? And if you have multiple repositories you are mirroring do you need to run one for each? Also I am assuming that if you do not use the i386 architecture you do not need to include that line in your file?

    Thank you,

    • Hi Francis,
      You would need to run script once. If you have multiple repositories for different versions of Ubuntu then you should execute this script.
      In case you are not creating repositories of 32-bit packages then you can comment out the line in script which is downloading 32-bit package

  3. Thank you for this guide.
    But the script does not download the required files when I run it.

    Not sure what I am doing wrong

    • Without “\” it works.
      I extended it with proposed and backports

      $ vi
      for p in “${1:-focal}”{,-{security,updates,proposed,backports}}/{main,restricted,universe,multiverse};do >&2 echo “${p}”
      wget -q -c -r -np -R “index.html*” “${p}/cnf/Commands-amd64.xz”
      wget -q -c -r -np -R “index.html*” “${p}/cnf/Commands-i386.xz”

  4. One thing of note, (as I just hit this), if you’re looking to do a dist-upgrade eg Ubuntu focal to groovy, you need to also mirror the sources for that release otherwise the upgrade will fail.

  5. Hi friends,
    I did all the steps in the tutorial instead the cronjob step. I get some errors as following:
    E: Failed to fetch ‘’ 404 Not Found [IP: 80]
    E: Failed to fetch ‘’ 404 Not Found [IP: 80]
    E: Failed to fetch ‘’ Hash Sum mismatch
    Hashes of expected file:
    – Filesize:13388 [weak]
    – SHA256:759e37bb211f72003e4573f776b05ba62761c79389224d0993df995c9ac5b39b
    – SHA1:69625e9176a66f160e9bd2e88d798156f44fdd0f [weak]
    – MD5Sum:f7fc39f674e86e173c89fdd942080bb3 [weak]
    Hashes of received file:
    – SHA256:85039a373678f7f8d538b7fc4faaab23b76a34ea0cf7abaf11c4c9ef449a6eb5
    – SHA1:98b3a6d70fe956b6c10fc752e99ef6e6fa76f9e1 [weak]
    – MD5Sum:06ab45f7bda69967c209a30eccdc91e3 [weak]
    – Filesize:13388 [weak]
    Last modification reported: Wed, 19 May 2021 17:18:02 +0000
    Release file created at: Wed, 19 May 2021 08:04:53 +0000
    E: Failed to fetch ‘’
    E: Some index files failed to download. They have been ignored, or old ones used instead.

    I will be happy to your attention

  6. I set it up exactly as per your article, however, I get an error when trying to install vlc. Most other packages work fine. Any ideas?

    [email protected]:~# apt install vlc
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    The following additional packages will be installed:


    Need to get 3,201 kB/212 MB of archives.
    After this operation, 1,153 MB of additional disk space will be used.
    Do you want to continue? [Y/n]
    Get:1 ‘ focal/main amd64 libmozjs-68-0 amd64 68.6.0-1ubuntu1’ [3,201 kB]
    Err:1 ‘ focal/main amd64 libmozjs-68-0 amd64 68.6.0-1ubuntu1’
    File has unexpected size (195435 != 3200548). Mirror sync in progress? [IP: 80]
    Hashes of expected file:
    – SHA256:8bc08cbc7eae15a9a74e5b2ac3c992f7527f86b6e601a3eb8a7933c0b88e5a90
    – SHA1:8c96584fcc54203bdce83ff23dc60295381c590e [weak]
    – MD5Sum:b0bf9e07fa3d22b9a672ef3ecadf751f [weak]
    – Filesize:3200548 [weak]
    E: Failed to fetch ‘’ File has unexpected size (195435 != 3200548). Mirror sync in progress? [IP: 80]
    Hashes of expected file:
    – SHA256:8bc08cbc7eae15a9a74e5b2ac3c992f7527f86b6e601a3eb8a7933c0b88e5a90
    – SHA1:8c96584fcc54203bdce83ff23dc60295381c590e [weak]
    – MD5Sum:b0bf9e07fa3d22b9a672ef3ecadf751f [weak]
    – Filesize:3200548 [weak]
    E: Unable to fetch some archives, maybe run apt-get update or try with –fix-missing?

    • I solved the problem by downloading libmozjs-68-0 amd64 68.6.0-1ubuntu1 manually to the repo directory tree. I am guessing my original sync cut the file short.

  7. Had to comment “clean ‘'” in /etc/apt/sources.list as it kept removing cnf directories and contents.

  8. Is there a way to remove an newly mirrored package that breaks some of our clients? Specifically I have setup a mirror of Mariadb, on Feb 2nd, apt-mirror pulled down the version 10.6.12 which breaks some of our systems. Is there a way to remove the latest package from our mirror and lock the version at 10.6.11?


Leave a Comment