Ubuntu ‘apt-get’ list of commands (list, update, upgrade, cheatsheet)

I have a couple of Debian and Ubuntu Linux systems that use the APT package management system, including test servers, production servers, and even Raspberry Pi systems. It seems like every time I have to use an apt-get or other apt command, I always have to search for the command I need. To put an end to that, I’m creating this “apt-get reference page.” This page is terse, as I’ve just written it for myself, but I hope it’s also helpful for others.

One note: I log into my servers as the “root” user, so I run the commands as shown. If you don’t log in as root, you’ll need to use the sudo command before most of the commands shown below.

With no further delay, here’s my Ubuntu package management cheat sheet ...

List installed packages

On Ubuntu 14.04 and newer, get a list of packages installed locally:

apt list --installed
dpkg -l

Older versions:

dpkg --get-selections | grep -v deinstall

Search for packages

apt-cache is used to search the apt package cache.

Search for a package by its name or description:

apt-cache search <search_term>
apt-cache search foo

From help.ubuntu.com, “ Similar to apt-cache search, but also shows whether a package is installed on your system by marking it with ii (installed) and un (not installed)”:

dpkg -l *<search_term>*

List all available packages:

apt-cache search .

This may also work to list all available packages (“provides a listing of every package in the system”):

apt-cache pkgnames

Can also do this, haven’t tried it yet:

apt-get update && apt-cache dump

Get package information

Get information about a package:

apt-cache show <package_name>
apt-cache show nginx

Get package dependencies

List dependencies for a package:

apt-cache showpkg <package_name>
apt-cache showpkg nginx

Update the Ubuntu package management system

Update the package database on the system:

apt-get update

From tecmint.com, “The update command is used to resynchronize the package index files from the their sources specified in /etc/apt/sources.list.”

Upgrade

Upgrade all installed packages:

apt-get upgrade

Check for upgrades, but don’t install them automatically:

apt-get -u upgrade

Upgrade one specific package:

apt-get upgrade <package_name>
apt-get upgrade nginx

Often see update and upgrade together like this:

apt-get update && sudo apt-get upgrade -y

upgrade vs dist-upgrade

Possible to do this, but not recommended on production systems:

apt-get dist-upgrade

Quote from itsfoss.com:

apt-get upgrade is very obedient. It never tries to remove any packages or tries to install a new package on its own. apt-get dist-upgrade, on the other hand, is proactive. It looks for dependencies with the newer version of the package being installed and it tries to install a new package or remove an existing one on its own ... dist-upgrade should be avoided on production machines”

Install new packages

Install one package:

apt-get install <package_name>
apt-get install streamripper
apt-get install xdaliclock

Install multiple packages:

apt-get install <package_1> <package_2>
apt-get install nginx ssl-cert

Install without upgrading:

sudo apt-get install <package_name> --no-upgrade

Remove installed packages

Remove the binary files of a package:

apt-get remove <package_name>

Remove everything related to a package, including its configuration files (purge everything):

apt-get purge <package_name>

It looks like I used this command once:

apt-get remove --purge openjdk*

Ways to “clean” your Linux system

These commands are available, but I haven’t used them:

apt-get clean
apt-get autoclean
apt-get autoremove

Quotes from tecmint.com:

  • “The clean command is used to free up the disk space by cleaning retrieved (downloaded) .deb files (packages) from the local repository”
  • “The autoclean command deletes all .deb files from /var/cache/apt/archives to free-up significant volume of disk space”

apt-get diagnostics

From help.ubuntu.com, “does an update of the package lists and checks for broken dependencies”:

apt-get check

Other useful Debian/Ubuntu commands

List all services on a Linux system:

service --status-all

Show Ubuntu version information:

lsb_release -a

See also