Home Command line utilities Download Packages With Dependencies Locally In Ubuntu
Download Packages with dependencies locally in Ubuntu

Download Packages With Dependencies Locally In Ubuntu

By sk
12939 Views

A while ago, we discussed how to Install Softwares offline in Ubuntu. In that guide, we explained how can you download packages in an Internet-enabled system, and install them in another system that has slow or no Internet connection. In this tutorial, we will see how to download packages with dependencies locally in Ubuntu and its derivatives.

Using this method, we can download a .DEB package along with all required dependencies without actually installing it. This way we can download packages from one system and install them later in the same system itself or any other system that has no Internet connection. We can also download packages for different architecture systems. For example, it is possible to download the 32 bit packages from a 64 bit system and vice versa.

Download Packages With Dependencies Locally In Ubuntu

We can do this in two methods. I tested this guide on Ubuntu 16.04 and 18.04 LTS desktop editions. It worked just fine as described below.

Method 1:

This is the simplest and straight-forward method than other other methods given below.

To download a package with all dependencies, without installing them, just run:

$ sudo apt-get install --download-only <package_name>

For instance, let us download the Vim package with all required dependencies, without installing them, using 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:
vim
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 http://archive.ubuntu.com/ubuntu 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 see in the above output, we have downloaded Vim package will all dependencies, but we didn't actually install it.

Download Packages With Dependencies Locally In Ubuntu and derivatives

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

Just copy the entire cache folder on any USB or transfer them via network to a system that you wanted to install the packages in it.

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

$ sudo dpkg -i *

See? It's that simple!

However, this method only works if the system that you use to download the packages does not have the main package or its dependencies installed locally.

If you try to download a package which is already installed in the same system itself, you will see an output like below.

$ sudo apt-get install --download-only vim
Reading package lists... Done
Building dependency tree 
Reading state information... Done
vim is already the newest version (2:8.0.1453-1ubuntu1.3).
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.

In such cases, use "apt-rdepends" to download all packages. If apt-rdepends is not installed yet, install it using command:

$ sudo apt install apt-rdepends

And then download the main package (i.e. Vim in our case) along with all dependencies using command:

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

This command will recursively download all required packages.

Just in case if you encountered with an error like below:

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

Try this command instead:

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

This command will download Vim with all needed packages and save them in the current working directory.

To install all downloaded packages, run:

$ sudo dpkg -i *

Method 2:

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

To display list of all dependencies of a package, for example Python, run:

$ sudo apt-cache depends python

Sample output:

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

Let us download python package with its dependencies to our local disk.

To do so, first create a directory to save the packages.

$ mkdir python

Go to the directory:

$ cd python

And then 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 will download Python package along with all required dependencies and saves them in the current working directory. This command will also save any errors in the errors.txt file.

Let us view the downloaded files using 'ls' command:

$ ls

Sample output:

errors.txt
libpython-stdlib_2.7.11-1_amd64.deb
python2.7_2.7.11-7ubuntu1_amd64.deb
python-doc_2.7.11-1_all.deb
python-minimal_2.7.11-1_amd64.deb
python-tk_2.7.11-2_amd64.deb

Download packages locally in Ubuntu

As you see in the above output, python package with all its dependencies has been downloaded.

Just copy them to your USB drive and install the python packages on any offline system as shown below.

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

$ sudo dpkg -i *

Suggested Read : 


Download packages with dependencies locally for a specific architecture

You might notice that the above command has downloaded the 64 bit packages. It is because I am downloading them from the 64-bit Ubuntu system. What if you want to download packages for 32-bit arch systems? It's also possible!

First, enable the architecture you want in your Ubuntu system using command:

$ sudo dpkg --add-architecture i386

If you don't add the architecture, you will get the following error message when try to download the packages.

E: No packages found

After enabling the Architecture of your choice, run the following command to download 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

As you see in the above output, I have added the architecture 'i386' with 'apt-cache' command.

Sample output:

Get:1 http://in.archive.ubuntu.com/ubuntu 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 http://in.archive.ubuntu.com/ubuntu xenial/main i386 python2.7 i386 2.7.11-7ubuntu1 [220 kB]
Fetched 220 kB in 1s (116 kB/s) 
Get:1 http://in.archive.ubuntu.com/ubuntu 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 http://in.archive.ubuntu.com/ubuntu xenial/main i386 python-tk i386 2.7.11-2 [28.0 kB]
Fetched 28.0 kB in 1s (24.8 kB/s)
Download packages locally 

Let us check the downloaded packages.

$ ls

Sample output:

errors.txt
libpython-stdlib_2.7.11-1_i386.deb
python2.7_2.7.11-7ubuntu1_i386.deb
python-minimal_2.7.11-1_i386.deb
python-tk_2.7.11-2_i386.deb

See? The above command downloaded the 32 bit packages only.


Recommended read:


You know now how to download packages with dependencies in Ubuntu systems. This method is same for all DEB-based systems. Hope this helps.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

14 comments

jack October 1, 2018 - 7:16 am

Awsome!! Thank you so much 🙂

Reply
jayesh December 11, 2019 - 10:24 pm

superb

Reply
Mr MZLPCZK January 28, 2020 - 2:23 am

This is exactly the info I needed.

Reply
Ashish March 15, 2020 - 3:11 pm

Thank you soo much 😉😉😉
Loved your content & the simplicity of your explanation…
Keep it up

-Love from India 🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳

Reply
douirc April 11, 2020 - 11:19 pm

Thank you for the post. Is it possible the dependencies have dependencies? Is there a way to rewrite the script to download dependencies recursively? Also, if your package has multiple dependencies, how does dpkg -i * know what order to install the packages to avoid errors?

Reply
sk April 12, 2020 - 11:46 am

Hi,

For Question 1 – Yes, It is possible the dependencies may have other dependencies.

Question 2 – Yes, we can download the dependencies recursively. The following command will download the dependencies of Vlc package recursively in the current directory.

apt-get download $(apt-cache depends –recurse –no-recommends –no-suggests –no-conflicts –no-breaks –no-replaces –no-enhances vlc | grep “^\w” | sort -u)

Question 3 – I don’t know the answer on top of my head right now. May be you should refer man pages of dpkg command.

Thanks.

Reply
Bob April 19, 2020 - 4:53 pm

Very useful info, thank you very much SK!

Reply
sk April 19, 2020 - 5:21 pm

You’re welcome. Happy to help.

Reply
iris May 25, 2020 - 7:31 pm

hi thank you i want use this method for installed package how i do
i search in google and find it can with dpkg-repack
i want backup installed package and his dependencies for use later offline

Reply
Dave June 11, 2020 - 6:34 am

Hi nice info!! Can you bundle this into an sh script that can receive the package as parameter ( “download.sh python” where download.sh is the script and python any package ) then the script will do:
1 – Create a folder “python-installer” (where python is the package name provided)
2- Download the package dependencies and recursive dependencies.
3- Compress all downloaded packages in a file (resources.tgz for example) and clean the folder.
4- Create a script inside the folder with the name “setup.sh” this script will do:
—1- extract all the packages in a temp folder an run the “sudo dpkg -i *” command on it

Recapitulating, we will have in our USB memory:
| python-installer ( folder )
|—–resources.tgz ( compressed .deb packages )
|—–setup.sh ( script for auto installation )

I don’t have the skills to do that, it will be awesome if you can make it

Reply
sk June 11, 2020 - 1:15 pm

Hi Dave,

I don’t have much knowledge in scripting/programming. Please post your question in Stackoverflow or /r/linuxquestions. Good luck!

Reply
kapil kumar sharma September 27, 2020 - 9:27 pm

I messed up with some dependencies and i removed wifi and ethernet software. Due to guidance from this tutorial i could get them back. Thank you sir

Reply
sk September 28, 2020 - 11:53 am

Glad I could help. You are welcome!

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More