How to Setup Local Repository in Ubuntu

Have you ever wanted to save bandwidth when updating your Ubuntu computers? It’s very possible by creating a local APT repository server for your local LAN using Apache and some standard tools. This method allows for the ability to host any Debian package file you desire locally to make it easier to distribute.

There are multiple reasons to have a local repository. The main reason would be to capture large updates downstream and re-distribute them via a local repository to save on Internet usage. Other reasons someone might want to do this would be to more easily distribute software that isn’t found on the default Ubuntu servers.

So, how do you get it installed? Let’s find out!

For the local repository to work, an Apache server will need to be set up on the host system. Open up a terminal and enter the following command:

sudo apt-get install apache2

local-apt-repository-apache-server-start-page

This will set up a website on Ubuntu and the folder structure that it needs to function. It can be tested out in any web browser by going to “http://localhost” or the LAN IP address of the machine using it (http://192.168.1.XX).

The Apache server folder structure lies entirely in “/var/www/.” The default website page that the server sets up is in “/var/www/html/” and is labeled as “index.” Setting up a local repository has little use for this default Apache page. However, it may be a good idea to make a start page to advertise ways LAN users can connect to the local repository.

The local Ubuntu package repository will not work without the correct folder structure. The Debian package repository directory structure is necessary here. In a terminal, do the following:

sudo -i

local-apt-repository-folders

Now that the user has root permission, start off by entering the correct directory with the cd command.

cd /var/www/

The first part of the package repository structure is the “deb” folder. Create it with the mkdir command.

mkdir -p debs
cd debs
mkdir -p amd64
mkdir -p i386

local-apt-repository-generate-apt-catalog

The folders are set up. Now it is time to configure the catalog for APT to use. First, place any and all .Deb package files you’d like to keep in either the “/var/www/debs/amd64” folder or the “/var/debs/i386” folder (for 32 bit packages).

dpkg-scanpackages amd64 | gzip -9c > Packages.gz

or for 32bit

dpkg-scanpackages i386 | gzip -9c > Packages.gz

With everything set up, the APT repository is no different than any other repository that a user would find online. This makes it very simple and easy to add to Ubuntu.

Inside the sources file, add your LAN APT repository using your APT servers LAN IP address. This is found with ip addr show in the terminal. Look for “inet 192.168..” under the network adapter on the system communicating with the Internet. Alternatively, log into your router and find the IP address it has given the machine hosting the APT repo server.

Note: to make things easier on laptops, consider port forwarding the IP address of the APT repo host machine outside of LAN to the Internet on port 80 and using that in place of a local IP address like the tutorial says. This will ensure that updates will not break when a computer leaves the LAN for a brief time.

local-apt-repository-edit-sources-list

With the internal IP address known, enter the following in the terminal:

sudo nano /etc/apt/sources.list

This will allow the user to edit the list of servers that Ubuntu pings for updates. Add the repository to the list and save it.

deb http://192.168.1.X/debs/ amd64/

or for 32bit

deb http://192.168.1.X/debs/ i386/

Press “Ctrl + o” to save the changes once done. Finally, update Ubuntu by doing sudo apt-update.

Note: replace X with whatever internal IP address your router has given out. Alternatively, change 192.168.1.X with the external IP address so that your APT server is ping-able when not at home.

Now that everything is set up, any user with the repository in the “sources.list” file will be able to easily get packages from the local APT cache as if they were regular updates. No other special configurations required, and bandwidth savings can begin.

This method is the superior way to go as it makes it possible to host packages locally for bandwidth sake.The other positive is that there is very little configuration to do, and end-users will not know any better.

Would you host your own APT cache server on Ubuntu to save bandwidth? Tell us below!

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.