Why Build a Local APT Repository?
If your enterprise runs 200 Ubuntu servers, downloading the exact same software updates 200 times from the public internet is a massive waste of bandwidth. Furthermore, if you are building highly secure “air-gapped” servers that do not have internet access at all, they cannot use apt-get install. Creating a Local APT Repository solves this by downloading a mirror of the public packages to a single internal server, allowing all your other machines to securely pull their software and updates at lightning-fast LAN speeds.
Step 1: Install Apt-Mirror and Apache
You need a tool to download the packages and a web server to host them. Open the terminal on the server that will act as your repository and install apt-mirror and apache2:
sudo apt update
sudo apt install apt-mirror apache2 -y
Step 2: Configure the Mirror Settings
The apt-mirror configuration file dictates which Ubuntu versions and architectures you want to download. Open the file:
sudo nano /etc/apt/mirror.list
By default, it will mirror the OS version currently running on the server. Look for the set base_path directive. By default, it downloads to /var/spool/apt-mirror. Warning: A full Ubuntu repository is over 150GB. Ensure the disk partition holding this directory has adequate free space.
If you only want 64-bit packages (saving massive amounts of space), you can append -amd64 to the end of the deb lines (e.g., deb-amd64 http://archive.ubuntu.com/ubuntu jammy main).
Step 3: Initiate the First Download
Run the tool as the apt-mirror user to begin the massive initial download:
sudo su - apt-mirror -c apt-mirror
The terminal will output how many gigabytes it needs to download. Depending on your internet speed, this could take several hours. Let it run to completion.
Step 4: Serve the Repository via Apache
Once the download is complete, the files are sitting on your hard drive, but your client servers cannot access them over the network. You must create a symbolic link from your Apache web root directory to the apt-mirror directory.
sudo ln -s /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu /var/www/html/ubuntu
You can verify this works by opening a web browser and navigating to http://your-server-ip/ubuntu. You should see the standard Ubuntu repository folder structure (dists, pool, etc.).
Step 5: Point Client Servers to the Local Mirror
Log in to one of your client Ubuntu servers. You must edit its sources list to stop talking to the public internet and point to your new internal server.
sudo nano /etc/apt/sources.list
Replace the default http://archive.ubuntu.com/ubuntu/ URLs with the IP address of your new local repository:
deb http://192.168.1.100/ubuntu/ jammy main restricted
Save the file and run sudo apt update. The client will instantly fetch the package headers from your local server, allowing you to install software without any external internet connection.