10 Ubuntu / Debian Linux apt Command Examples

Posted on in Categories , , last updated April 2, 2016

I am a new Linux system admin user. How do I use apt command line utility for the package management on Ubuntu Linux LTS and Debian Linux server? How can I use the apt command for the package management?

apt command details
DescriptionAPT command
Root privilegesYes
Estimated completion time10m
APT stands for Advanced Package Tool. It is a set of core tools inside Debian and Ubuntu system. You can use the apt command to install apps, remove apps, keep your system up to date and more. Apt work with dpkg to install and update system. I suggest you use the apt command to save typing at the CLI. apt-get is the first front end tool. apt is a second tool which overcomes some design mistakes of apt-get. Hence you should use apt command. The apt command provides nicer interface including progress bars and colors at the CLI. 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.


Use apt for installing, upgrading, configuring, and removing apps/programs for a Debian or Ubuntu operating system in a consistent manner.


The basic syntax is as follows:
apt [options] command
apt [options] command pkg1
apt [options] command pkg1 pkg2

apt command examples

Let us see how to use the apt command to install security updates or new set of packages on Ubuntu or Debian Linux server.

How to fetch updates

To download package information from all configured sources, enter:
$ sudo apt update
Sample outputs:

Fig.01: Ubuntu Linux 16.04 LTS use update to download package information
Fig.01: Ubuntu Linux 16.04 LTS use update to download package information

How to apply package and security updates

To upgrade all packages currently installed on the system, run:
$ sudo apt upgrade
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed:
  libboost-random1.58.0 liblvm2app2.2
The following packages will be upgraded:
  apport bsdutils dbus dmeventd dmsetup ethtool
  gcc-5-base geoip-database grub-legacy-ec2
  libblkid1 libdbus-1-3 libdevmapper-event1.02.1
  libdevmapper1.02.1 libfdisk1 libglib2.0-0
  libglib2.0-data liblvm2cmd2.02 liblxc1 libmount1
  libpam-cgfs libpam-systemd libpython3.5-minimal
  libpython3.5-stdlib librados2 librbd1
  libsmartcols1 libstdc++6 libsystemd0 libudev1
  libuuid1 linux-generic linux-headers-generic
  linux-image-generic lvm2 lxc lxc-common
  lxc-templates lxc1 lxcfs lxd lxd-client mount
  openssh-client openssh-server openssh-sftp-server
  python3-apport python3-lxc python3-problem-report
  python3.5 python3.5-minimal sudo systemd
  systemd-sysv tzdata ubuntu-minimal ubuntu-standard
  udev util-linux uuid-runtime
59 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 96.4 MB of archives.
After this operation, 297 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 bsdutils amd64 1:2.27.1-6ubuntu2 [51.6 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 util-linux amd64 2.27.1-6ubuntu2 [847 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 mount amd64 2.27.1-6ubuntu2 [121 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 dbus amd64 1.10.6-1ubuntu3 [141 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libdbus-1-3 amd64 1.10.6-1ubuntu3 [161 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 systemd-sysv amd64 229-3ubuntu2 [17.3 kB]
Setting up lxd (2.0.0~rc8-0ubuntu2) ...
Setting up liblvm2cmd2.02:amd64 (2.02.133-1ubuntu8) ...
Setting up dmeventd (2:1.02.110-1ubuntu8) ...
Setting up lvm2 (2.02.133-1ubuntu8) ...
update-initramfs: deferring update (trigger activated)
Processing triggers for shared-mime-info (1.5-2) ...
Processing triggers for libc-bin (2.23-0ubuntu2) ...
Processing triggers for initramfs-tools (0.122ubuntu6) ...
update-initramfs: Generating /boot/initrd.img-4.4.0-16-generic

Please note that new packages will be installed if required to satisfy dependencies, but existing packages will never be removed.

A note about applying package and security updates

Simply type the following two commands to apply all security and package updates:
$ sudo apt update && sudo apt upgrade
Sample outputs:

Gif 01: Updating my system using apt
Gif 01: Updating my system using apt

To see the list of packages that can be upgraded on the system, enter:
$ apt list --upgradable
Sample outputs:
Fig.02: Ubuntu Linux 16.04 LTS list all upgradable packages apt command
Fig.02: Ubuntu Linux 16.04 LTS list all upgradable packages apt command

How to perform full system upgrade

The full-upgrade command performs the function of upgrade but will remove currently installed packages if this is needed to upgrade the system as a whole. This is useful when packages are kept back from updates or you want to install from Ubuntu version 16.04 to 16.04.1:
$ sudo apt full-upgrade

How to install a new packages

To install a new package called nginx, enter:
$ sudo apt install {pkgNameHere}
$ sudo apt install nginx

How to remove a packages

To delete or remove a package called nginx, enter:
$ sudo apt remove {pkgNameHere}
$ sudo apt remove nginx

The purge option to remove both package and config files

Removing a package removes all packaged data, but leaves usually small (modified) user configuration files behind, in case the remove was an accident. Just issuing an installation request for the accidentally removed package will restore its function as before in that case. On the other hand you can get rid of these leftovers by calling purge even on already removed packages:
$ sudo apt purge {pkgNameHere}
$ sudo apt purge nginx

The autoremove option

The autoremove option is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed as dependencies changed or the package(s) needing them were removed in the meantime. For example, when you upgrade Linux kernel to 4.1.5, you may not need Linux kernel version 3.8.5. The syntax is:
$ sudo apt autoremove
$ sudo apt --purge autoremove

How to search packages

The search option can be used to search for the given regex. To search for php packages, enter:
$ apt search php
$ apt search mysql-5.?
$ apt search mysql-server-5.?
$ apt search httpd*
$ apt search ^apache
$apt search ^nginx
$ apt search ^nginx$

Sample outputs:

Fig.01: Ubuntu Linux 16.04 LTS apt search package command
Fig.01: Ubuntu Linux 16.04 LTS apt search package command

How to find info about packages

To show or see information about the given package(s) including its dependencies, installation and download size, sources the package is available from, the description of the packages content and much more:
$ apt show {pkgNamehere}
$ apt show nginx

How to List packages

To list all packages, enter:
$ apt list
$ apt list | more
$ apt list | grep foo
$ apt list | grep php7-

To display a list of packages satisfying certain criteria, enter:
$ apt list nginx
$ apt list 'php7*'

Sample outputs:

Fig.03: Ubuntu Linux 16.04 LTS display a list of packages satisfy
Fig.03: Ubuntu Linux 16.04 LTS display a list of packages satisfying certain criteria

apt command options

From the apt(8) command man page:

  list - list packages based on package names
  search - search in package descriptions
  show - show package details
  install - install 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

A note about differences from apt-get tool

The apt command is designed as an end-user tool and it may change behavior between versions. While it tries not to break backward compatibility this is not guaranteed either if a change seems beneficial for interactive use. All features of apt are available in dedicated APT tools like apt-get and apt-cache command as well. apt just changes the default value of some options. So you should prefer using these commands (potentially with some additional options enabled) in your scripts as they keep backward compatibility as much as possible.

See also
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Package Managerapt
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
User Informationgroupsidlastlastcommlognameuserswwhowhoamilidmembers

4 comment

  1. The stats sub command will display overall statistics about the cache. For example, the following command will display Total package names is the number of packages have found in the cache.

  2. It was an eye-opener. Didn’t know apt is as close as apt-get and apt-cache. Less typing and more productive from now on. Just read its man page/s. Confirmed what you wrote. A big thanks for sharing this info!

Leave a Comment