Script to list locally-installed deb packages from Terminal

Synaptic has a category “Installed (local or obsolete)” to list only the installed deb packages that either:

  • Are not available in any enabled repository, or
  • Are a newer version than can be obtained from enabled repositories.

Here is a Python script to print this list in Terminal. Requires python3-apt.

apt-locally-installed

#!/usr/bin/python3

import apt

pkglist = []
with apt.Cache() as cache:
  for pkg in (pkg for pkg in cache if pkg.is_installed):
    if (pkg.candidate.downloadable and pkg.candidate >= pkg.installed) or \
       any(v > pkg.installed and v.downloadable and v.policy_priority > 0 for v in pkg.versions):
      continue
    pkglist.append(pkg)

width = 0
for pkg in pkglist:
  if len(pkg.name) > width: width = len(pkg.name)
width += 2

for pkg in pkglist:
  print(f'{pkg.name.ljust(width)}{pkg.installed.version}')
2 Likes