Ubuntu 16.04: Download deb package and all package dependencies

This article will describe how to download deb packages and all package dependencies.

1 apt-rdepends

The apt-rdepends shows all package dependencies of certain package recursively.

Install apt-rdepends with apt.

$ sudo apt install -y apt-rdepends

In case of binutils is as below. These are, except " XXX:", package dependencies of binutils.

$ apt-rdepends binutils 2> /dev/null
binutils
  Depends: libc6 (>= 2.14)
  Depends: zlib1g (>= 1:1.2.0)
libc6
  Depends: libgcc1
libgcc1
  Depends: gcc-6-base (= 6.0.1-0ubuntu1)
  Depends: libc6 (>= 2.14)
gcc-6-base
zlib1g
  Depends: libc6 (>= 2.14)

2 virtual package in apt-rdepends

The virtual package is abstract package which has no deb package. For example, there is a virtual package which names awk. And there are awk-original, mawk and gawk which provide awk feature. Installing one of three packages will resolve dependencies of virtual package which names awk.

The apt-rdepends will output virtual package and it will cause download error.

$ apt download awk
E: Can't select candidate version from package awk as it has no candidate

It needs to convert virtual package to provide packages. Running apt install will show provide packages of virtual package.

$ apt install -s awk
NOTE: This is only a simulation!
      apt-get needs root privileges for real execution.
      Keep also in mind that locking is deactivated,
      so don't depend on the relevance to the real current situation!
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package awk is a virtual package provided by:
  original-awk:i386 2012-12-20-5
  original-awk 2012-12-20-5
  mawk:i386 1.3.3-17ubuntu2
  gawk:i386 1:4.1.3+dfsg-0.1
  mawk 1.3.3-17ubuntu2
  gawk 1:4.1.3+dfsg-0.1
You should explicitly select one to install.

E: Package 'awk' has no installation candidate

3 Download deb package and all package dependencies

Download with the following way.

  • Get package dependencies with apt-rdepends.
  • Convert virtual packages in the package dependencies to provide packages.
  • Download deb package and all package dependencies.
#!/bin/sh

if [ $# -ne 1 ]; then
  prog=`basename ${0}`
  echo "usage: ${prog} <package>"
  exit 1
fi

TMP=`mktemp -t a.sh.XXXXXX`
trap "rm $TMP* 2>/dev/null" 0

check_virtual_package()
{
  apt show $1 2> /dev/null | grep "not a real package" > /dev/null
  return $?
}

get_provide_package()
{
  apt install -s $1 > ${TMP} 2> /dev/null

  local state=0
  local pkgs=""
  while read line; do
    if [ "${line}x" = "Package $1 is a virtual package provided by:x" ]; then
      state=1
    elif [ ${state} -eq 1 -a -n "${line}" ]; then
      pkg=`echo ${line} | awk '{ print $1 }'`
      echo ${pkg} | grep -v ':i386' > /dev/null && pkgs="${pkg} ${pkgs}"
    fi
  done < ${TMP}

  echo "${pkgs}"
}

get_depend_package()
{
  local pkgs=""
  local pkg=""

  for pkg in `apt-rdepends $1 2> /dev/null | grep -v "^  "`; do
    check_virtual_package ${pkg}
    if [ $? -eq 0 ]; then
      pkg=`get_provide_package ${pkg}`
    fi
    pkgs="${pkgs} ${pkg}"
  done

  echo "${pkgs}"
}

download_deb_package()
{
  local pkgs=""
  pkgs=`get_depend_package $1`
  apt download ${pkgs}
}

download_deb_package $1