Create Your Own Local apt Repository to Avoid “Dependency Hell”


There are times when you download a .deb file that simply must be installed. Once on your machine, you run the dpkg command on the file only to find yourself in a quagmire of dependencies. Unfortunately, that necessary piece of software cannot be found in a standard repository. Instead of trying to wade through the dependency hell that dpkg can put you through, why not let apt take care of the heavy lifting?

“But how?” you may ask. Believe it or not, the solution lies in creating your very own localized repository that apt can recognize. Because apt will now see your local repository, it can install any .deb file placed in the local repo directory and then resolve the associated dependencies when you issue the command to install the package.

That’s handy. It’s also really easy. Let me walk you through the process.

Necessary addition

The first thing you must do is install the dpkg-dev package. Do this by issuing the command sudo apt-get install dpkg-dev. You will immediately be greeted by several necessary dependencies that must be installed (Figure 1). Fortunately (as is the point of this setup), apt will handle all of those dependencies.

Figure 1: Installing dpkg-dev requires a few dependencies to be resolved.

Once you’ve installed the dpkg-dev package, you’re ready to move on.

Creating the directory

You must have a directory on your system that will serve as the repository. This folder will contain the .deb files you want to install with the apt package manager. We’ll create the folder /usr/local/mydebs with the command sudo mkdir -p /usr/local/mydebs.
With that folder created, you can now move your .deb packages. Say those packages are located in ~/Downloads. Issue the command sudo mv ~/Downloads/*.deb /usr/local/mydebs to move the files into the newly created repository.

The executable script

We now must create a script that will use the dpkg-scanpackages (which will already be installed on your system) tool to scan the local repository and output the results of that scan into a compressed file that apt-get can then read and work from.

Create the new file update-mydebs with the following content:

#! /bin/bash
cd /usr/local/mydebs
dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz

Save the file and then give it executable permissions with the command:

chmod u+x update-mydebs

For running the command, you have two options:

  • Keep the file in the location you created it, knowing you will always have to run the file from that location (as in sudo ./update-mydebs)

  • Move the file into /usr/bin so that it can be run globally

Personally, I prefer to move the file into /usr/bin (for the sake of ease). You might prefer to go the localized route. Either way, the script will work as needed.

Edit sources.list

The next step is adding a line to your /etc/apt/sources.list file. This is necessary in order to make apt aware of the local repository (otherwise, it wouldn’t know of its existence). To take care of this step, open up the /etc/apt/sources.list file in your favorite text editor (mine being nano) and add the following line:

deb file:/usr/local/mydebs ./

Update and run

Finally, you can now update apt and then install the .deb files without having to work with dpkg. Here’s what you must do.

  1. Go back to the terminal window (the one you’ve been working with this whole time)

  2. Issue the command sudo update-mydebs (or sudo ./update-mydebs, if you’ve opted to not move the script into /usr/bin)

  3. Once the update-mydebs command completes, issue the command sudo apt-get update

At this point, apt will now be ready to install any .deb file contained within the /usr/local/mydebs repository. All you have to do now is issue the command sudo apt-get install PACKAGE (where PACKAGE is the name of the package to be installed).

When you do run the installation command, you will have to okay the installation of an unverified package (what you’ve added to the local repository).

What’s also really nice about this setup is that any package you add to the local repository will also be found in your apt front end (e.g., GNOME Software or Synaptic). You can add the .deb files to /usr/local/mydebs, run the updates and then install the software with your favorite GUI tool.

The only thing you have to remember is that any time you add a new .deb file to the /usr/local/mydebs directory, you must once again issue the commands sudo update-mydebs and sudo apt-get update (otherwise, apt will not be aware of the new package).

Make it network-able

If you happen to have a number of Linux machines on a network, you can avoid having to create these local repositories on every machine by housing all of the .deb files on a Samba file server. With that Samba share mounted on each local client, you can adjust the update-mydebs script to reflect the path to the shared folder as well as the sources.list entry.

That does mean each client will have to have the dkpg-dev package installed and the update-mydebs script as well as all of the commands will have to be issued individually. This allows  the .deb files to be housed on only the Samba share (thus saving space and time). It requires a bit more work on the front end, but it could save you from having to constantly copy .deb files over to numerous machines. Once step less is always nice.

Avoiding dependencies is worth the trouble

I remember, so long ago, when installing Linux apps could be the cause of much hair pulling. Times have changed and Linux has evolved into an incredibly easy to use platform. Along with its ease of use, comes a level of flexibility few platforms can match. With the ability to create your own local repositories, to avoid having to manually resolve dependencies, you see some of that flexibility in action. What little trouble this setup adds to your daily grind is very well worth the time involved. Not only will you enable the installation of software outside the standard repositories, you can avoid the headaches that were once an inevitability.

Learn more about system management in the Essentials of System Administration training course from The Linux Foundation.