80

I am often interested in the installation triggers (postinst, postrm) or certain parts of packages (like /usr/share and /etc). Currently, I am running the next command to retrieve the source code:

apt-get source [package-name]

The downside is, this file is often much bigger than the binary package and does not reflect the installation tree.

Right now, I am downloading the packages through http://packages.ubuntu.com/:

  1. Search for [package-name]
  2. Select the package
  3. Click on amd64/i386 for download
  4. Download the actual file

This takes too long for me and as someone who really likes the shell, I would like to do something like the next (imaginary) command:

apt-get get-deb-file [package-name]

I could not find something like this in the apt-get manual page. The most close I found was the --download-only switch, but this puts the package in /var/cache/apt/archives (which requires root permissions) and not in the current directory.

Lekensteyn
  • 171,743
  • 65
  • 311
  • 401

7 Answers7

108

You can use the download sub-command of apt, apt-get or aptitude. For example, if $PKG is the package you want, any of these will do:

apt-get download $PKG
apt download $PKG
aptitude download $PKG

This doesn't require root privileges. The same can also be approximated using apt-get and wget:

wget $(apt-get install --reinstall --print-uris -qq $PKG | cut -d"'" -f2)

This will, however, fetch all packages required to install the package, so you can attempt to limit it instead:

wget $(apt-get install --reinstall --print-uris -qq $PKG | cut -d"'" -f2 | grep "/${PKG}_")

You can also put a wget line into a function, to be able to use it as a command apt-download with the package name as a parameter:

function apt-download { wget -c $(apt-get install --reinstall --print-uris -qq $1 | cut -d"'" -f2); }

Note the modifications: The $PKG is replaced with $1 and the -c parameter enables continuing interrupted downloads.

muru
  • 193,181
  • 53
  • 473
  • 722
Kees Cook
  • 17,243
  • 9
  • 68
  • 96
  • 1
    Excellent, this is what I'm looking for. Another reason to keep aptitude on my system, the other useful command that is not provided by `apt` is `aptitude changelog $PKG`. – Lekensteyn Mar 16 '11 at 12:27
  • 4
    Update: from Natty (apt version 0.8.11 to be precise), the `aptitude download` feature is available in `apt` as well: `apt-get download $PKG`. – Lekensteyn Apr 29 '11 at 19:22
  • sweet, works despite my ancient apt v0.8.10 – rogerdpack Dec 12 '13 at 23:29
  • it seems they did away with the --print-urls option – erjoalgo Apr 29 '14 at 04:06
  • 1
    The --print-urls option is still there – kumarharsh Sep 16 '14 at 10:32
  • Is there a way to accomplish this and download for a specific architecture (ie: `amd64`/`i386`/`armhf`/`arm64`)? [I've posted a similarly themed question](https://askubuntu.com/questions/770569/get-apt-packages-for-cross-compiling-against-armhf-arm64-platforms), and this gets me about 90% of the way to the finish line. Thank you. – Cloud May 11 '16 at 04:05
  • @Cloud, few years passed, but maybe it'll be useful for someone: I think You can use the syntax `apt download :`, for example `apt download ncdu:amd64`. – kcpr Jun 24 '23 at 17:59
10
sudo apt-get -o dir::cache::archives="/path/to/folder/" -d install package

Note:

You need to create an folder named partial in destination folder.

mount.cifs
  • 1,330
  • 8
  • 13
  • Doesn't sound that bad. The options seems to be described by `man apt.conf`. I would like to avoid the creation of the folder. – Lekensteyn Mar 15 '11 at 15:02
  • I'm just curious, why would you avoid creating the folder? – mount.cifs Mar 16 '11 at 07:44
  • I do not need to store the package forever and avoid creating the folder saves time too. – Lekensteyn Mar 16 '11 at 12:26
  • Just wanted to note that this solution will also insist on package removal (e.g. if you change version via `/etc/apt/sources.list` to a newer one, with the intent to download later packages) - in such a case, the `wget` method above may be more useful, if you don't want to remove anything... – sdaau Feb 17 '14 at 18:45
5

In Ubuntu 14.04 (apt package version 1.0.1ubuntu2, I believe), apt-get includes the download command to download the given package as a .deb in the current directory.

For example, suppose we want to download the file manager Ranger:

$ apt-get download ranger

Results in:

$ ls . | grep ranger
ranger_1.6.0-1_all.deb
  • This is perfect. All other solutions I tried don't work if `apt install` can't meet all dependency requirements. I just wanted the .deb file for tinkering! – Thomas Glaser Aug 26 '17 at 12:02
3

If you want to download all deb packages from a list, you can do this:

cat path/to/text/file.txt | xargs apt-get install --reinstall --print-uris -qq $PKG | cut -d"'" -f2 | xargs wget

Just put one package name per line. Like in a requirements.txt file. For example, with contents like this:

apache2-mpm-event
curl
dmidecode
ethtool
libapache2-mod-wsgi
libapache2-mod-python

Hope this helps. ;)

Jayme Tosi Neto
  • 161
  • 1
  • 6
2

sudo apt-get install devscripts

dget [package-name]

Michael Terry
  • 3,675
  • 20
  • 34
  • How doesn't it provide an answer? This downloads a deb from the archive to the current directory. Is that not exactly what the question was? – Michael Terry Apr 14 '15 at 22:54
  • A bit more detail may be useful. While this *technically* qualifies as an answer, you may still want to provide more than two commands. – s3lph Apr 14 '15 at 22:56
  • ::shrug:: Unlike the other answers here, this one has no provisos or caveats. It's a simple answer to a straightforward question. – Michael Terry Apr 14 '15 at 23:06
0

You can use command debget which is included in the package debian-goodies.

Install it with:

sudo apt install debian-goodies

Download packages using:

debget <package_name>

For example:

debget debian-goodies

Which will download debian-goodies_0.79_all.deb in your current directory (do pwd to print your current working directory).

Shayan
  • 1,481
  • 16
  • 33
  • `debian-goodies` comes with lots of useful commands, see also: https://askubuntu.com/a/1170001/777279 – Shayan Sep 01 '19 at 14:09
0

/var/cache/apt/archives is world readable. After apt-get -d, just extract it from there to your home directory. Run dpkg -e /var/cache/apt/archives/foo_version.deb foo while in your home directory and the control files will be dumped into foo/.

αғsнιη
  • 35,092
  • 41
  • 129
  • 192
psusi
  • 37,033
  • 2
  • 68
  • 106
  • I think the problem with this method is that the download still requires administrative privileges. It seems silly to go to such extremes just to get a file into your home directory. – ændrük Mar 15 '11 at 14:56
  • Opening the .debs is not the problem, downloading it is. It might be a bug or not, but the packages in /var/cache/apt/archives` are affected by umask too. I've set an umask of 027 and therefore, the packages in `/var/cache/apt/archives` cannot be read by me. As I need just to examine the package contents, I just need to download it once: to `/tmp`. – Lekensteyn Mar 15 '11 at 15:01