A FAQ style introduction to apt-get and apt-cache

console

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

Reading time:
5 min

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

How can I list all available packages?

Run the following command:

apt-cache pkgnames

How can I find out package names of software I want to install?

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; for example: apt-cache search string

To search for software that only contains your string in the package name (that is 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 (that is prefixed), use the pkgnames sub command; for example: apt-cache pkgnames prefixString For example, to find all packages starting with emacs, you could run:

apt-cache pkgnames emacs

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?

Use the show sub command, for example:

apt-cache show emacs-chess

How can I display dependencies for specific software packages(s) regardless of whether the package(s) are installed or not?

Use the showpkg sub command followed by the package name(s):

apt-cache showpkg packageName

The apt-get FAQ

How can I verify that my system knows about the latest versions of software packages available?

Use the update sub command:

sudo apt-get update

How can I upgrade the software on my system?

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

How can I install or upgrade _specific_ software package(s)?

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*

How can I tell apt-get to only install, not upgrade existing software packages?

Use the --no-upgrade option:

sudo apt-get install packageName --no-upgrade

How can I tell apt-get to only upgrade specific packages if they're already installed (that is no new install)?

Use the --only-upgrade option:

sudo apt-get install packageName --only-upgrade

How can I install a specific version of a software package?

Simply append an = to the packagename followed by the desired version:

sudo apt-get install packageName=version

How can I remove software packages, without removing the configuration files (for example in case I want to reinstall the software later and re-use the same configs)?

Use the remove sub command:

sudo apt-get remove packageName

How can I completely remove software packages including the configuration files?

Use the purge sub command:

sudo apt-get purge packageName

or alternatively, use the --purge option with remove:

sudo apt-get remove --purge packageName

How can I free up disk space by clearing away software package install related files (.deb files)?

Use the clean sub command

sudo apt-get clean

How can I download the source code for a particular software package?

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 (that is packageName=version).

How can I just download a package (.deb file) but not install it?

Use the download sub command:

sudo apt-get download packageName

How can I take a look at the change log for a software package?

Use the changelog sub command:

sudo apt-get changelog packageName

How can I simulate a command without actually running it for real?

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

How can I tell apt-get to always assume a "Yes" answer for each prompt?

Use one of these options: -y, --yes, or --assume-yes; for example:

sudo apt-get -y command

How can I tell apt-get to always assume a "No" answer for each prompt?

Use the --assume-no option:

sudo apt-get --assume-no command

How can I add and remove software packages with the same command?

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.

Thank you for reading this article.
Please share if you liked it.