See APT manpage on Ubuntu docs.

Note that apt is a more modern version of apt-get. They are separate packages and commands are mostly the same. See also aptitude command for a GUI.

See also my os-genesis repo’s script, which deal with APT.

Help

$ sudo apt -h
Usage: apt [options] command

apt is a commandline package manager and provides commands for
searching and managing as well as querying information about packages.
It provides the same functionality as the specialized APT tools,
like apt-get and apt-cache, but enables options more suitable for
interactive use by default.

Most used commands:
  list - list packages based on package names
  search - search in package descriptions
  show - show package details
  install - install packages
  reinstall - reinstall packages
  remove - remove packages
  autoremove - Remove automatically all unused packages
  update - update list of available packages
  upgrade - upgrade the system by installing/upgrading packages
  full-upgrade - upgrade the system by removing/installing/upgrading packages
  edit-sources - edit the source information file
  satisfy - satisfy dependency strings
...

Manage packages

Specify one or more packages by name (separated by spaces).

Install or upgrade package

Use the same command to install a package or upgrade an existing package.

$ sudo apt install PACKAGE

e.g.

$ sudo apt install bar

Install a target version:

$ sudo apt install PACKAGE=VERSION

e.g.

$ sudo apt install git=1:2.38.1-0ppa1~ubuntu20.04.1

Remove package

$ sudo apt remove PACKAGE

Or use the more aggressive command to also remove configurations - purge.

Upgrade all packages

$ sudo apt upgrade

Add -y for yes to accept immediately.

Note that apt upgrade PACKAGE will not target a single package.

Autoremove

Uninstall packages no longer needed.

$ sudo apt autoremove

Full flow

Upgrade, upgrade, install, and clean-up.

This is useful for a Dockerfile such as this:

apt update 
  && apt -y upgrade

apt install -y foo

apt autoremove \
  && apt clean

Fixed packages

If you get an error when running apt because of locked or broken packages, then the output will suggest this which solves it for me:

$ sudo dpkg --configure -a

List

Available:

$ sudo apt list

Installed only:

$ sudo apt list --installed

Upgradeable:

$ sudo apt list --upgradeable

You can filter the list if you know the exact package name. The prefix of the slash names will be use.

$ sudo apt list python3
Listing... Done
python3/focal,now 3.8.2-0ubuntu2 amd64 [installed]
python3/focal 3.8.2-0ubuntu2 i386

Or use a glob pattern.

$ sudo apt list 'python*'
Listing...
python-acme-doc/focal,focal 1.1.0-1 all
python-agate-doc/focal,focal 1.6.0-3 all
python-agatedbf-doc/focal,focal 0.2.0-2 all
..

There might be version available from different sources:

$ sudo apt list git -a
Listing... Done
git/focal,now 1:2.38.1-0ppa1~ubuntu20.04.1 amd64 [installed]
git/focal-updates,focal-security 1:2.25.1-1ubuntu3.6 amd64
git/focal 1:2.25.1-1ubuntu3 amd64

Sources

Help

$ man sources.list

See sources.list(5) on the Debian manpages site.

Add

Copy instructions from the relevant documentation. Some examples below.

  • Based on the PGAdmin instructions:
      $ sudo sh -c 'echo "deb URL PACKAGE_NAME main" > /etc/apt/sources.list.d/PACKAGE_NAME.list && apt update'
    
  • Based on the Node instructions.
      $ curl -sL "URL/VERSION" | sudo -E bash -
    

Edit

You might want to remove or update a source if you get errors or no longer need something.

$ cd /etc/apt/sources.d

Files:

nodesource.list
official-package-repositories.list
pgadmin4.list
slack.list
vscode.list