Remove package dependencies that are now no longer needed and configure system to perform this task automatically.

I will use dict application as en example to show you how to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed.</>

Install dict package.

$ sudo apt-get install dict --install-recommends --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libmaa3 librecode0 m4 recode
Suggested packages:
  dictd | dict-server m4-doc
The following NEW packages will be installed:
  dict libmaa3 librecode0 m4 recode
0 upgraded, 5 newly installed, 0 to remove and 2 not upgraded.
Inst librecode0 (3.6-23 Debian:9.1/stable [amd64])
Inst recode (3.6-23 Debian:9.1/stable [amd64])
Inst libmaa3 (1.3.2-3+b2 Debian:9.1/stable [amd64])
Inst dict (1.12.1+dfsg-4 Debian:9.1/stable [amd64])
Inst m4 (1.4.18-1 Debian:9.1/stable [amd64])
Conf librecode0 (3.6-23 Debian:9.1/stable [amd64])
Conf recode (3.6-23 Debian:9.1/stable [amd64])
Conf libmaa3 (1.3.2-3+b2 Debian:9.1/stable [amd64])
Conf dict (1.12.1+dfsg-4 Debian:9.1/stable [amd64])
Conf m4 (1.4.18-1 Debian:9.1/stable [amd64])

Additional packages libmaa3, librecode0, m4 and recode will be installed automatically.

$ sudo apt-get install dict --install-recommends

Command-line

Execute the removal command, only the dict package will be removed, dependencies will be mentioned and left intact.

$ sudo apt-get remove dict --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libmaa3 librecode0 m4 recode
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  dict
0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded.
Remv dict [1.12.1+dfsg-4]
$ sudo apt-get remove dict 

To remove unused dependencies execute the autoremove action.

$ sudo apt-get autoremove --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  libmaa3 librecode0 m4 recode
0 upgraded, 0 newly installed, 4 to remove and 8 not upgraded.
Remv libmaa3 [1.3.2-3+b2]
Remv recode [3.6-23]
Remv librecode0 [3.6-23]
Remv m4 [1.4.18-1]
$ sudo apt-get autoremove

You can initiate this action during removal process, but remember that it will act in the same way as the above command and will remove all unused dependencies as it does not not differentiate between unused packages related to this or other packages.

$ sudo apt-get remove --autoremove dict --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  dict libmaa3 librecode0 m4 recode
0 upgraded, 0 newly installed, 5 to remove and 8 not upgraded.
Remv dict [1.12.1+dfsg-4]
Remv libmaa3 [1.3.2-3+b2]
Remv recode [3.6-23]
Remv librecode0 [3.6-23]
Remv m4 [1.4.18-1]
$ sudo apt-get remove --autoremove dict

Display or hide packages that are no longer required

Do not hide information about packages that are no longer required.

This is the default setting.

$ echo -e 'APT::Get::HideAutoRemove "0";' | sudo tee /etc/apt/apt.conf.d/90_display_unused
APT::Get::HideAutoRemove "0";
$ sudo apt-get remove dict --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libmaa3 librecode0 m4 recode
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  dict
0 upgraded, 0 newly installed, 1 to remove and 8 not upgraded.
Remv dict [1.12.1+dfsg-4]

Display only short information without mentioning particular packages.

$ echo -e 'APT::Get::HideAutoRemove "small";' | sudo tee /etc/apt/apt.conf.d/90_display_unused
APT::Get::HideAutoRemove "small";
$ sudo apt-get remove dict --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
4 packages were automatically installed and are no longer required.
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  dict
0 upgraded, 0 newly installed, 1 to remove and 8 not upgraded.
Remv dict [1.12.1+dfsg-4]

Hide information about packages that are no longer required.

$ echo -e 'APT::Get::HideAutoRemove "1";' | sudo tee /etc/apt/apt.conf.d/90_display_unused
APT::Get::HideAutoRemove "1";
$ sudo apt-get remove dict --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  dict
0 upgraded, 0 newly installed, 1 to remove and 8 not upgraded.
Remv dict [1.12.1+dfsg-4]

Protect unused packages

You can protect packages that are no longer required from being removed.

$ cat << EOF | sudo tee /etc/apt/apt.conf.d/90_protect_unused
APT
{
  NeverAutoRemove
  {
    "^m4$";
    "^libmaa.*$";
  };
}
EOF
APT
{
  NeverAutoRemove
  {
    "^m4$";
    "^libmaa.*$";
  };
}

The above configuration will protect m4 package and libmaa3 from being automatically removed.

$ sudo apt-get remove --autoremove dict --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  dict librecode0 recode
0 upgraded, 0 newly installed, 3 to remove and 6 not upgraded.
Remv dict [1.12.1+dfsg-4]
Remv recode [3.6-23]
Remv librecode0 [3.6-23]

Automatically remove unused packages

Enable automatic removal of the unused packages.

$ echo -e 'APT::Get::AutomaticRemove true;' | sudo tee /etc/apt/apt.conf.d/90_auto_remove_unused
APT::Get::AutomaticRemove true;
$ sudo apt-get remove dict --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  dict libmaa3 librecode0 m4 recode
0 upgraded, 0 newly installed, 5 to remove and 6 not upgraded.
Remv dict [1.12.1+dfsg-4]
Remv libmaa3 [1.3.2-3+b2]
Remv recode [3.6-23]
Remv librecode0 [3.6-23]
Remv m4 [1.4.18-1]

Debug automatic removal process

In case of problems or doubts, enable debug information during automatic removal of the unused packages.

$ echo -e 'Debug::pkgAutoRemove "true";' | sudo tee /etc/apt/apt.conf.d/90_debug_remove_unused
Debug::pkgAutoRemove "true";
$ sudo apt-get remove dict --dry-run
Reading package lists...
Building dependency tree...
Reading state information...Auto-Installed : libc-l10n:amd64
Auto-Installed : linux-base:amd64
Auto-Installed : libklibc:amd64
[...]
AutoDep: libpng16-16:amd64
Marking: libpam0g:amd64 1.1.8-3.6
Following dep: libpam0g:amd64 Depends on libaudit1:amd64 < 1:2.6.7-2 @ii K > (>= 1:2.2.1)
Marking: libaudit1:amd64 1:2.6.7-2
Following dep: libaudit1:amd64 Depends on libaudit-common:amd64 < 1:2.6.7-2 @ii K > (>= 1:2.6.7-2)
[...]
Marking: gzip:amd64 1.6-5+b1
Marking: traceroute:amd64 1:2.1.0-2
Marking: tree:amd64 1.7.0-5
Marking: hostname:amd64 3.18+b1
Marking: hdparm:amd64 9.51+ds-1
Marking: tzdata:amd64 2017b-1
Following dep: tzdata:amd64 Depends on debconf-2.0:amd64 < none @un H >, provided by debconf:amd64 1.5.61 (1/1)
Garbage: dict:amd64
Garbage: librecode0:amd64
Garbage: recode:amd64
Garbage: libmaa3:amd64

Mark package as manually installed

Automatic removal will take care of the automatically installed packages.

$ apt-mark showmanual dict
dict
$ apt-mark showauto m4
m4

You can mark important ones as installed manually to do not take these into account during the whole automatic removal process.

$ sudo apt-mark manual m4
m4 set to manually installed.
$ sudo apt-get remove --autoremove dict --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  dict libmaa3 librecode0 recode
0 upgraded, 0 newly installed, 4 to remove and 6 not upgraded.
Remv dict [1.12.1+dfsg-4]
Remv libmaa3 [1.3.2-3+b2]
Remv recode [3.6-23]
Remv librecode0 [3.6-23]

Additional notes

This is out of scope of this article, but you can use additional --purge parameter to also remove configuration files associated with removed packages.