A FAQ Style Introduction to apt-get and apt-cache

Learn how to quickly install, update, remove, and search for software via the command line in this FAQ style introduction to the apt-get and apt-cache commands.

What is apt-get?

The apt-get program is a command line tool used to install, update, or remove software packages from an Ubuntu or Debian based Linux installation. It is an interface to the Advanced Packaging Tool (APT) library. Other higher level interfaces include aptitude, synaptic, and the Ubuntu Software Centre. All of these programs use the same underlying api, so their use can be intermixed without any issues.

What is apt-cache?

Apt-cache is a tool for searching the apt package cache. In other words you can use it to search for software packages and generate other information about what packages are available to install on an Ubuntu or Debian based Linux installation.

The apt-cache FAQ

Q) How can I list all available packages?
A) Run the following command:

apt-cache pkgnames
Q) How can I find out package names of software I want to install?
A) For software who's name contains a particular string in either the package name or the package description, use the search sub command.
Note that using "search" will present you with a short description of any matched packages. e.g.:
apt-cache search string
To search for software that only contains your string in the package name (i.e. do not search for the string in the description), use the "--names-only" or "-n" option:
apt-cache search -n string
To get a better/longer description of the matched packages, use the --full option with search:
apt-cache search -n --full emacs-chess
To search for a package who's name starts with a specific string (i.e. prefixed), use the pkgnames sub command, e.g.:
apt-cache pkgnames prefixString
For example, to find all packages starting with emacs, you could run:
apt-cache pkgnames emacs
Q) How can I display more information (version, section/category, size, installed size, checksums, similar software suggestions, and all other package records) along with a full description for a software package?
A) Use the show sub command, for example:
apt-cache show emacs-chess
Q) How can I display dependencies for specific software packages(s) regardless of whether the package(s) are installed or not?
A) Use the showpkg sub command followed by the package name(s):
apt-cache showpkg packageName

The apt-get FAQ

Q) How can I verify that my system knows about the latest versions of software packages available?
A) Use the update sub command:

sudo apt-get update
Q) How can I upgrade the software on my system?
A) If you want to upgrade all the software that's already installed, but do not want to upgrade any packages that result in other packages being installed or removed (to satisfy upgrade dependencies), simply use the upgrade sub command:
sudo apt-get upgrade
However, if you wish to upgrade regardless of whether packages will be added or removed to satisfy dependencies, use the more thorough dist-upgrade sub command:
sudo apt-get dist-upgrade
Q) How can I install or upgrade specific software package(s)?
A) Use the install sub command:
sudo apt-get install packageName
You can specify more than one package name as part of the command in order to install several packages at the same time:
sudo apt-get install packageName1 packageName2 ...
You may also use a regular expression to specify several packages with one string, for example, using the * wildcard we can install packages that contain the string "name" somewhere in the package name:
sudo apt-get install *name*
Q) How can I tell apt-get to only install, not upgrade existing software packages?
A) Use the --no-upgrade option:
sudo apt-get install packageName --no-upgrade
Q) How can I tell apt-get to only upgrade specific packages if they're already installed (i.e. no new install)?
A) Use the --only-upgrade option:
sudo apt-get install packageName --only-upgrade
Q) How can I install a specific version of a software package?
A) Simply append an "=" to the packagename followed by the desired version:
sudo apt-get install packageName=version
Q) How can I remove software packages, without removing the configuration files (e.g. in case I want to reinstall the software later and re-use the same configs)?
A) Use the remove sub command:
sudo apt-get remove packageName
Q) How can I completely remove software packages including the configuration files?
A) Use the purge sub command:
sudo apt-get purge packageName
or alternatively, use the --purge option with remove:
sudo apt-get remove --purge packageName
Q) How can I free up disk space by clearing away software package install related files (.deb files)?
A) Use the clean sub command
sudo apt-get clean
Q) How can I download the source code for a particular software package?
A) To download only, use the --download-only option:
sudo apt-get --download-only source packageName
To download and unpack, from a suitable directory where you want the code, run:
sudo apt-get source packageName
To download, unpack, and compile the source code, use the --compile option:
sudo apt-get --compile source packageName
Note that if you want to download the source for a specific version, append the version to the package name as mentioned earlier (i.e. packageName=version).

Q) How can I just download a package (.deb file) but not install it?
A) Use the download sub command:
sudo apt-get download packageName
Q) How can I take a look at the change log for a software package?
A) Use the changelog sub command:
sudo apt-get changelog packageName
Q) How can I simulate a command without actually running it for real?
A) Use the "-s", or "--dry-run", or "--simulate" option along with your desired command, e.g:
sudo apt-get --dry-run command
or:
sudo apt-get --simulate command
Q) How can I tell apt-get to always assume a "Yes" answer for each prompt?
A) Use one of these options: "-y", "--yes", or "--assume-yes", e.g.:
sudo apt-get -y command
Q) How can I tell apt-get to always assume a "No" answer for each prompt?
A) Use the --assume-no option:
sudo apt-get --assume-no command
Q) How can I add and remove software packages via the same command?
A) Just append a "+" or "-" to the packagenames.
For example, to remove a package whilst using the "install" command, just append a "-" to the relevant package. So to remove packageName2, while installing other packages, you could use:
sudo apt-get install packageName1 packageName2-
To install a software package whilst using the remove command, append a "+" to the relevant package. So to install packageName1 while removing other packages, you could use:
sudo apt-get remove packageName1+ packageName2
Note that these two examples above both result in packageName1 being installed and packageName2 being removed.

Thanks to Jason Gunthorpe and the APT Team for both apt-get and apt-cache.