How do the various APT package removal commands differ?
What's the difference between these commands?
sudo apt-get autoremove --purge packagenames
sudo apt-get remove --purge packagenames
sudo apt-get purge purge packagenmaes
sudo apt-get remove packagenames
sudo apt-get autoremove
Solution 1:
Quick Answer
-
remove
uninstalls packages named after it. -
autoremove
uninstalls all automatically installed packages that are no longer needed. It is incorrect to list package names afterautoremove
; they will be ignored (and all eligible packages will be removed), which is probably not what you intend. -
--purge
causes systemwide configuration files associated with packages being removed to be removed as well. It must be accompanied by a command, such asremove
orautoremove
. The--purge
flag modifies the action of a command; it does not act on its own. -
purge
means the same thing as--purge remove
(which is the same asremove --purge
). It is incorrect to saypurge
twice; the secondpurge
is interpreted as the name of a package to bepurge
d, which is probably not what you intend.
Full Answer
To understand what those commands do and how they are different, it helps (and is probably even essential) to understand two concepts:
- the distinction between manually installed and automatically installed packages
- the distinction between a package's package files and its configuration files
Manually Installed vs. Automatically Installed
An installed package is either manually or automatically installed.
When you specify a package for installation, and it is installed, it is manually installed.
But other packages may also be installed. Most packages have dependencies--other packages that must be installed, for those package to work as intended. When you install a package, its dependencies (i.e., the other packages that it depends on) are installed, except for the ones that were installed before.
When a package is not specified by you to be installed, but instead is installed as a dependency of a package you specified to install, the it is automatically installed.
Please note that when you have two packages installed and one of them is a dependency of another, that does not mean the dependency is automatically installed. It's also possible that the dependency was manually installed (i.e., specified by you, for example, installed before or at the same time as the package that depends on it).
Finally:
- If you try to manually install a package that is already installed, it remains installed. But if it was automatically installed, a change is made--trying to manually install it will mark it as manually installed.
- You can mark any package as manually or automatically installed (with the
apt-mark
command), no matter how or under what circumstances they were actually installed. - Packages that come preinstalled with your Ubuntu system are not all automatically installed; some of them come marked automatically installed and others come marked manually installed. This is to satisfy the logic behind distinguishing between manually and automatically installed packages--the idea that some packages are only needed in service of others.
Typically, for every automatically installed package you will have at least one manually installed package that depends on it (and which caused it to be automatically installed). However, if you then remove all the manually installed packages that depend on an automatically installed package, the automatically installed package will no longer be necessary, but will still be installed.
apt-get autoremove
removes automatically installed packages that no longer have any manually installed packages depending on them.
Package Files and Configuration Files
A package provides files that are intended to be, and to remain, the same on every system where that version of that package is installed. Such files provide executables, resources, documentation, and other "assets" for installed programs. We usually just called these "the files" that a package provides.
But a package provides other files, too, which are intended to (possibly) be changed. These are called global (or systemwide) configuration files. They are almost always installed in the /etc
directory, and they can be automatically and manually modified to change the behavior of installed programs. For example, sudo
has a configuration file /etc/sudoers
, and a directory for more configuration files /etc/sudoers.d
. That file, and any files added to that directory, specify who is allowed to use sudo
in what ways.
Typically it is desired that uninstalling a package and then installing it back should not change the way it behaves (unless its package files were corrupt and that's why you reinstalled it, of course). Therefore, typically when a package is uninstalled, its package files are deleted but its systemwide configuration files are left untouched.
When you uninstall a package without specifying that its systemwide configuration files are also supposed to be deleted, that is called "removing" the package.
If you also want to delete a package's systemwide configuration files when you uninstall the package, that is called "purging" the package.
apt-get remove
will remove a package; that is, it will uninstall the package but leave its systemwide configuration files.
apt-get purge
will purge a package; that is, it will uninstall the package and also make sure to delete its systemwide configuration files.
Another way to purge packages is to use the --purge
flag. That doesn't tell apt-get
what to do; instead, it modifies what terms (or "commands") like remove
tell it to do. The --purge
flag causes any package uninstallations to be accompanied by the deletion of systemwide configuration files for the package(s) being uninstalled. In other words, the --purge
flag turns removals into purges.
Why do we have the --purge
flag when we have the purge
command? After all, isn't sudo apt-get --purge remove packagename
equivalent to sudo apt-get purge packagename
. They are equivalent. Where --purge
comes in handy is when you want to modify the behavior of commands other than remove
.
For example, suppose you want to uninstall automatically installed packages that are no longer needed (because there are no longer any manually installed packages depending on them), but you don't just want to remove these automatically installed packages, you also want to delete their systemwide configuration files. Then you could run:
sudo apt-get --purge autoremove
Let's consider one more use of --purge
. There is a flag called --reinstall
, which turns installations into reinstallations. This is to say that, normally, when you try to install a package that is already installed, either nothing will happen, or (if the package is marked automatically installed) the package will be marked manually installed. But with the --reinstall
flag, installing a package that is already installed causes it to be removed and then installed back:
sudo apt-get --reinstall install packagename
But suppose you want to delete the systemwide configuration files of the package you're reinstalling. This is somewhat common; when a package is not working and you don't know why, you might try reinstalling it, and also deleting its configuration files so that its configuration is reset to the default. To achieve this, you can run:
sudo apt-get --purge --reinstall install packagename
The --reinstall
turns installation into reinstallation (i.e., removal followed by installation), and the --purge
turns the removal part of reinstallation into purging.
The Behavior of Your Examples
Now it should be clear exactly what each of your example apt-get
commands do:
-
sudo apt-get autoremove --purge packagenames
This purges automatically installed packages that no longer have any manually installed packages that depend on them. That is, it uninstalls those packages (which entails deleting their package files), and it deletes their systemwide configuration files too.
Here,
packagenames
does nothing. In particular, if you intended to just remove packages whose names are listed there, then this will not behave as you intend. -
sudo apt-get remove --purge packagenames
This purges
packagenames
; assumingpackagenames
is a whitespace-separated list of package names, it will purge all of them. That is, it will uninstall them (which entails deleting their package files), and will also delete their systemwide configuration files.If any of the packages listed in
packagenames
is not installed, its presence in the list will have no effect (except that you will be informed it was not installed). If you list package names that are not the name of any actual package, the command will fail with anunable to locate packagename
error. -
sudo apt-get purge purge packagenmaes
If you removed one of the occurrences of
purge
in that command, it would be equivalent to the previous one. As it stands, it tries to purge a package calledpurge
, and also to purge the packages listed inpackagenames
. Sincepurge
is not the name of any package, it will fail with an error message. -
sudo apt-get remove packagenames
This removes the packages listed in
packagenames
, which entails deleting their package files. This does not delete their systemwide configuration files (because it is merely a removal, not a purge). -
sudo apt-get autoremove
This uninstalls automatically installed packages that no longer have any manually installed packages that depend on them. This entails deleting their package files, but not their systemwide configuration files (that is, it is a removal but not a purge).
For more information about how to use apt-get
, I recommend its manual page. For more general information, see this guide.