In high-density server environments or offline labs, downloading packages from the official Ubuntu or Debian repositories can consume significant bandwidth and introduce latency. By configuring a local mirror, administrators can drastically reduce installation times and ensure network stability during critical updates. This guide walks you through setting up an internal APT mirror using debsnap or standard directory structures, ensuring your team can update systems rapidly without relying on external internet connectivity.
Why You Should Use a Local Mirror
Public repositories like those hosted by Canonical are globally distributed and highly reliable for the general public. However, in specific scenarios—such as air-gapped networks, remote sites with poor ISP connections, or development labs where thousands of engineers install software simultaneously—the overhead becomes a bottleneck. A local mirror acts as a cache, allowing multiple machines to pull packages from a single internal server rather than fighting for bandwidth on the public internet.
The primary benefits include reduced download times, lower bandwidth costs, and the ability to enforce security policies by hosting only verified package versions. It is particularly useful during initial system provisioning or when troubleshooting network issues where external access is intermittent.
Prerequisites and Environment Setup
Before beginning, ensure you have a Linux server (Ubuntu 20.04 LTS or 22.04 LTS is recommended) with sufficient disk space allocated for the repository cache. You will need root or sudo privileges to manage the directory structure and update lists.
You should also have squashfs-tools installed if you plan to create an immutable squashfs image later, though for a standard writable mirror, this step can be skipped. Ensure your local server has access to the official Ubuntu mirrors (usually via HTTPS) so it can populate the cache initially.
sudo apt update
sudo apt install -y squashfs-tools rsync
These commands ensure you have rsync for efficient file synchronization and squashfs-tools if you intend to create a read-only image later. The rsync utility is crucial because it only transfers changed files, saving time during initial population and subsequent updates.
Step 1: Populate the Local Repository Cache
The first actionable step is to download the necessary package lists and binary packages from the official upstream repository to your local server. We will use debsnap, a robust tool designed specifically for creating efficient local mirrors that handle versioning and architecture changes automatically.
First, create a working directory for the mirror:
sudo mkdir -p /srv/apt-mirror
cd /srv/apt-mirror
Next, we will run debsnap to fetch the repository data. This tool simplifies the complex process of maintaining multiple architectures (amd64, arm64) and versions.
# Fetching all supported releases and architectures
sudo debsnap -a amd64,arm64 --architectures=amd64,arm64 \
--releases focal,jammy,noble --prefixes focal,jammy,noble \
--output-dir /srv/apt-mirror/repo --no-rebuild-index
Note: Adjust the --releases flags based on your specific Ubuntu versions. The command downloads package lists, .deb files, and associated metadata.
After the download completes, you must generate the Packages file that APT uses to index the contents of the local repository. Without this file, client machines will not see the available packages.
sudo debsnap -a amd64,arm64 --architectures=amd64,arm64 \
--releases focal,jammy,noble --prefixes focal,jammy,noble \
--output-dir /srv/apt-mirror/repo --generate-index
This step creates the Packages file for each architecture. You can verify the presence of these files by listing the directory:
ls -l /srv/apt-mirror/repo/pool/main/
# Expected output: directories for packages like 'linux-image', 'openssh-server' appear here.
ls -l /srv/apt-mirror/repo/dists/focal/main/binary-amd64/Packages.gz
Step 2: Configure the APT Sources List on Clients
Once the local repository is populated, you must configure your client machines to point to this new internal source. Edit the /etc/apt/sources.list file or files in /etc/apt/sources.list.d/ on each client machine.
Replace the default upstream URLs with your local server’s IP address or hostname. For example, if your mirror is hosted at 192.168.1.50, update the entry:
deb [trusted=yes] http://192.168.1.50/repo focal main restricted universe multiverse
deb [trusted=yes] http://192.168.1.50/repo jammy main restricted universe multiverse
deb [trusted=yes] http://192.168.1.50/repo noble main restricted universe multiverse
Illustrative Output after running apt update on a client:
Get:1 http://192.168.1.50/repo focal InRelease [12.3 kB]
Get:2 http://192.168.1.50/repo focal/main/binary-amd64/Packages.gz [1,234 kB]
Hit:3 http://us.archive.ubuntu.com/ubuntu noble InRelease (skipped due to local priority)
Reading package lists... Done
The [trusted=yes] tag is a temporary measure often used in lab environments or trusted internal networks where GPG signature verification might fail due to missing keys from the upstream signer. In production, you should configure your mirror server to sign the packages using debsnap --sign.
Step 3: Automate Updates and Maintenance
A static mirror is not enough; it must update as upstream repositories change. You can automate this process by adding a cron job to your local mirror server. This ensures that when a new version of a package is released, it becomes available to your internal network within minutes.
Create a daily maintenance script:
# /usr/local/bin/update-local-mirror.sh
#!/bin/bash
MIRROR_DIR="/srv/apt-mirror/repo"
UPSTREAM_URL="http://archive.ubuntu.com/ubuntu"
# Check if new packages are available and sync
rsync -avz --delete \
--exclude='.git' \
--exclude='index' \
$UPSTREAM_URL/dists/focal/ \
$MIRROR_DIR/dists/focal/
# Regenerate the index for APT
cd $MIRROR_DIR
debsnap -a amd64,arm64 --architectures=amd64,arm64 \
--releases focal,jammy,noble --prefixes focal,jammy,noble \
--output-dir . --generate-index
# Sign the new packages (requires GPG key setup on mirror server)
# debsnap --sign ...
Make the script executable and add it to your cron schedule:
sudo crontab -e
# Add this line to run every night at 2 AM
0 2 * * * /usr/local/bin/update-local-mirror.sh >> /var/log/mirror-update.log 2>&1
Common Errors and Troubleshooting
When configuring a local mirror, several issues frequently arise. One common error is the “No valid signature” message. This occurs because the upstream packages are signed with keys that your local system does not trust unless you explicitly add them or use the trusted=yes flag (as shown earlier). To fix this permanently, export the GPG keys from the upstream mirror and import them into your local server’s keyring using apt-key add (deprecated) or by configuring the APT trusted keys directory in newer Ubuntu versions.
Another issue is broken symlinks or missing directories after an rsync operation. Always ensure you use the --delete flag with caution; it removes files from the destination that are not present in the source, which can accidentally delete custom local modifications if not configured correctly. Verify the integrity of your mirror by running a test installation on a clean VM:
sudo apt update
sudo apt dist-upgrade
# Check for any errors in /var/log/apt/term.log
If you encounter SSL certificate errors when pulling from upstream, ensure your local server has valid certificates or configure APT to ignore them using Acquire::http::SSLVerify "false", though this is less secure and should be avoided if possible.
Industry Application and Student Practice Task
In a real-world industry scenario, a DevOps team at a financial firm sets up a local mirror to comply with data sovereignty laws, ensuring no package metadata leaves the country. They configure their internal registry to only pull packages from specific, vetted upstream mirrors.
Student Practice Task:
1. Set up a virtual machine acting as your “Mirror Server.”
2. Install Ubuntu 22.04 LTS and allocate 50GB of disk space for the repository.
3. Use debsnap to populate the repo with focal, jammy, and noble releases for amd64.
4. Generate the index files.
5. Create a second VM acting as a “Client.”
6. Configure the client’s /etc/apt/sources.list to point to your Mirror Server.
7. Run apt update on the client and install a heavy package like docker-ce or the latest kernel (linux-image-generic).
8. Measure the time taken for the download and compare it with a direct upstream download.
By mastering these steps, you gain essential skills in network optimization, system administration, and secure package management, which are highly valued in both entry-level engineering roles and advanced infrastructure positions.
Educational-use notice: This tutorial is for learning and controlled lab use only. Test carefully, use backups, and obtain authorization before making changes. Do not run unfamiliar commands on production systems. We accept no responsibility for misuse, damage, data loss, or production issues.

