apt / dpkg cheatsheet

Fix broken/aborted installation

  • apt-get update --fix-missing
  • dpkg --configure -a
  • apt-get install -f

Reconfigure

  • dpkg-reconfigure package

No questions asked / use old config:

  • ??

apt upgrade unattended, use default answers (no) or fallback to old config

export DEBIAN_FRONTEND=noninteractive; apt update; apt --assume-yes -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold" dist-upgrade: apt autoremove

  • DEBIAN_FRONTEND=noninteractive
  • --assume-yes
    • Answer questions with yes (Do you want to upgrade...)
  • -o "Dpkg::Options::=--force-confdef"
    • Use default package config for new packages
  • -o "Dpkg::Options::=--force-confold"
    • Use existing config for already installed packages (to not overwrite the existing config!)

Needed?

  • export DEBIAN_PRIORITY=critical

    • ask only absolutely necessary questions

  • apt -q

    • Quiet, don't ask questions

 

Find out if a package is installed

  • dpkg -s packageName

List manualy / 3rd party packages

E.g. after dist-upgrade

  • apt list --installed | grep -F "$(gettext 'apt' '[installed,local]')"

Install a package manually

If you installed an older version make sure to set the package on "hold" (see below)

Prevent a package from beeing updated (Hold)

  • sudo apt-mark hold packageName
  • Deprecated: echo "packageName hold" | dpkg --set-selections

Revert hold with

  • apt-mark unhold packageName

Uninstall a package manually

  • dpkg -r packageName

Information about a .deb package

Basic information

  • dpkg-deb -I packageName
     

List of files

  • dpkg-deb -c packageName

Try to install dependencies automatically

  • dpkg -i packageName_2.48.3-1ubuntu1_amd64.deb
    • dpkg: dependency problems...
  • apt-get -f install

Force installation even if dependencies are unmet

  • dpkg -i --force-depends packageName_2.48.3-1ubuntu1_amd64.deb
     

Get a list of manually installed packages

http://askubuntu.com/questions/17823/how-to-list-all-installed-packages

  • zgrep -hE '^(Start-Date:|Commandline:)' $(ls -tr /var/log/apt/history.log*.gz ) | egrep -v 'aptdaemon|upgrade' | egrep -B1 '^Commandline:'

Fix errors in pre/post scripts

  • The scripts are in /var/lib/dpkg/info
  • Example for w3m
    • w3m.postinst
      w3m.postrm
      w3m.preinst
      w3m.prerm

Re-Install all Packages

  • sudo aptitude reinstall '~i'
  • for pkg in `dpkg --get-selections | awk '{print $1}' | egrep -v '(dpkg|apt|mysql|mythtv)'` ; do apt-get -y --force-yes install --reinstall $pkg ; done
     

Install list of packages, eg. replicate on second computer

  • apt install dselect
  • apt update

  • dselect update

  • dpkg --get-selections > selections.txt

  • # copy selections.txt to other computer

  • dpkg --set-selections < selections.txt

  • dselect install

Manage alternatives

  • update-alternatives --get-selections
    • List items with alternatives
  • update-alternatives --get-selections | grep php
    • php                            auto     /usr/bin/php8.0
  • php --version
    • 8.0
  • update-alternatives --set php /usr/bin/php7.4
  • php --version
    • 7.4

APT Cache

  • apt autoclean
    • removes all stored archives in your cache for packages that can not be downloaded anymore (thus packages that are no longer in the repo or that have a newer version in the repo).
  • apt clean
    • removes all stored archives in your cache.

Automatic apt clean (does this work?)

vi /etc/apt/apt.conf.d/99custom-conf

  • # Remove downloaded .deb files after installation by apt-get
    APT::Keep-Downloaded-Packages "false";

 

Get a list of installed packages ordered by priority

dpkg-query -Wf '${Package;-40}${Priority}\n' | sort -b -k2,2 -k1,1