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 vs apt-dist upgrade

https://askubuntu.com/questions/194651/why-use-apt-get-upgrade-instead-of-apt-get-dist-upgrade#answer-226213

upgrade
    upgrade is used to install the newest versions of all packages
    currently installed on the system from the sources enumerated in
    /etc/apt/sources.list. Packages currently installed with new
    versions available are retrieved and upgraded; under no
    circumstances are currently installed packages removed, or packages
    not already installed retrieved and installed. New versions of
    currently installed packages that cannot be upgraded without
    changing the install status of another package will be left at
    their current version. An update must be performed first so that
    apt-get knows that new versions of packages are available.

dist-upgrade
    dist-upgrade in addition to performing the function of upgrade,
    also intelligently handles changing dependencies with new versions
    of packages; apt-get has a "smart" conflict resolution system, and
    it will attempt to upgrade the most important packages at the
    expense of less important ones if necessary. So, dist-upgrade
    command may remove some packages. The /etc/apt/sources.list file
    contains a list of locations from which to retrieve desired package
    files. See also apt_preferences(5) for a mechanism for overriding
    the general settings for individual packages.

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

Which package does a file belong to?

  • dpkg-query -S /path/to/file

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