Download locally dependent packages on Ubuntu

A while ago, How to Install Software Offline on Ubuntu. That guide showed you how to download a package to an Internet-enabled system and install the package offline on another system that has a slow or disconnected Internet connection. This tutorial describes how to download packages that have local dependencies on Ubuntu and its derivatives.

Using this method, you can download the .DEB package with all the required dependencies without actually installing it. In this way, you can download a package from one system and later install the package on the same system or on another system that is not connected to the Internet. You can also download packages for different architecture systems. For example, you can download a 32-bit package from a 64-bit system and vice versa. Alright, let’s get started without any further effort.

Download locally dependent packages on Ubuntu

There are two ways to do this. This guide has been tested on Ubuntu 16.04 and 18.04 LTS Desktop Edition. I worked fine.

Method 1:

This is a simpler and easier method than all the other methods listed below.

To download a package without installing all dependencies:

$ sudo apt-get install --download-only 

For example, let’s download Vim Package with all required dependencies without installing using the command:

$ sudo apt-get install --download-only vim

Sample output:

Reading package lists... Done
Building dependency tree 
Reading state information... Done
Suggested packages:
ctags vim-doc vim-scripts
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 82 not upgraded.
Need to get 1,152 kB of archives.
After this operation, 2,852 kB of additional disk space will be used.
Get:1 bionic-updates/main amd64 vim amd64 2:8.0.1453-1ubuntu1.1 [1,152 kB]
Fetched 1,152 kB in 3s (372 kB/s)
Download complete and in download only mode

As you can see from the output above, the Vim package downloaded all the dependencies, but did not actually install them.

All downloaded files will be saved to / var / cache / apt / archives directory.

Copy the entire cache folder on USB or transfer it over the network to the system where you want to install the package.

To install the downloaded package, go to the cache folder and install as shown below.

$ sudo dpkg -i *

to see? It’s easier than you think Yes, it is!

However, this only works if the main package or its dependencies are not installed locally on the system used to download the package.

In such cases, use “Apt-rdepends” Get all packages. Install apt-rdepends using the command if it is not already installed.

$ sudo apt install apt-rdepends

Then use the command to download the main package (ie Vim in this case) with all the dependencies.

$ apt download $(apt-rdepends vim | grep -v "^ ")

This command recursively downloads all required packages.

In case you get an error like:

E: Can't select candidate version from package debconf-2.0 as it has no candidate

Try the following command instead:

$ apt-get download $(apt-rdepends vim | grep -v "^ " | sed 's/debconf-2.0/debconf/g')

This command downloads Vim with all required packages and saves it to your current working directory.

To install all downloaded packages:

$ sudo dpkg -i *

Method 2:

First, download the dependencies of the package you want to download.

For example, to list all dependencies for a package Python, Run:

$ sudo apt-cache depends python

Sample output:

PreDepends: python-minimal
Depends: python2.7
Depends: libpython-stdlib
Breaks: update-manager-core
Suggests: python-doc
Suggests: python-tk
Replaces: python-dev

Download the python package and its dependencies to your local disk.

To do so, first create a directory to store the package.

$ mkdir python

Change to the directory.

$ cd python

And run:

$ for i in $(apt-cache depends python | grep -E 'Depends|Recommends|Suggests' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/); do sudo apt-get download $i 2>>errors.txt; done

The above command downloads a Python package with all necessary dependencies and saves it to your current working directory. This command saves the error errors.txt File.

Use the “ls” command to view the downloaded file.

$ ls

Sample output:


Download packages locally on Ubuntu

As you can see from the output above, a python package with all the dependencies has been downloaded.

Simply copy them to a USB drive and install the Python package on your offline system as shown below.

Mount the USB drive, go to the location where you mounted the drive, and run the following command to install Python.

$ sudo dpkg -i *

Recommended reading:

  • How to download RPM package with all dependencies on CentOS

Download packages with specific architecture dependencies locally

You may notice that the above command downloaded a 64-bit package. Because you are downloading from a 64-bit Ubuntu system. What if I download a package for a 32-bit arch system? It is also possible

First, enable the required architecture on your Ubuntu system using commands.

$ sudo dpkg --add-architecture i386

If you do not add the architecture, you will receive the following error message when you try to download the package:

E: No packages found

After enabling the selected architecture, run the following command to download the specific architecture related packages:

$ for i in $(apt-cache depends python:i386 | grep -E 'Depends|Recommends|Suggests' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/); do sudo apt-get download $i 2>>errors.txt; done

Added architecture as you can see in the output above “I386” When “Apt-cache” command.

Sample output:

Get:1 xenial/main i386 python-minimal i386 2.7.11-1 [28.2 kB]
Fetched 28.2 kB in 1s (25.8 kB/s) 
Get:1 xenial/main i386 python2.7 i386 2.7.11-7ubuntu1 [220 kB]
Fetched 220 kB in 1s (116 kB/s) 
Get:1 xenial/main i386 libpython-stdlib i386 2.7.11-1 [7,664 B]
Fetched 7,664 B in 0s (13.3 kB/s) 
Get:1 xenial/main i386 python-tk i386 2.7.11-2 [28.0 kB]
Fetched 28.0 kB in 1s (24.8 kB/s)
Download the package locally 

Check the downloaded package.

$ ls

Sample output:


to see? The above command downloaded only the 32-bit package.

Recommended reading:

  • How to simulate Linux commands without changing anything in the system

You now know how to download dependent packages on an Ubuntu system. This method is the same for all DEB-based systems. Hope it helps.