19 Ubuntu / Debian Linux apt Command Examples

apt command

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? Can you provide apt command examples for new developers?

APT stands for Advanced Package Tool. It is a set of core tools inside Debian and Ubuntu Linux system. You can use the apt command to install apps, delete or remove apps, keep Ubuntu/Debian server up to date and more. Apt work with dpkg command to install and update the system. I strongly suggest that you use the apt command to save typing at the command line. This page provides apt command examples for new Linux users.

The apt-get command is the first front-end tool. apt is a second tool which overcomes some design mistakes of apt-get command. Hence you should use the apt command. This 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.

apt command examples and purpose

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
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

All files are deleted except configuration files on the system.

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
$ sudo apt purge nginx nginx-core nginx-common

apt command examples purging files

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
$ apt show sudo

Sample outputs:

Package: sudo
Version: 1.8.19p1-1
Priority: optional
Section: admin
Maintainer: Bdale Garbee 
Installed-Size: 3106 kB
Depends: libaudit1 (>= 1:2.2.1), libc6 (>= 2.17), libpam0g (>=, libselinux1 (>= 1.32), libpam-modules, lsb-base
Conflicts: sudo-ldap
Replaces: sudo-ldap
Homepage: http://www.sudo.ws/
Tag: admin::login, admin::user-management, implemented-in::c,
 interface::commandline, role::program, scope::utility,
 security::authentication, use::login
Download-Size: 1054 kB
APT-Sources: http://httpredir.debian.org/debian stretch/main amd64 Packages
Description: Provide limited super user privileges to specific users
 Sudo is a program designed to allow a sysadmin to give limited root
 privileges to users and log root activity.  The basic philosophy is to give
 as few privileges as possible but still allow people to get their work done.
 This version is built with minimal shared library dependencies, use the
 sudo-ldap package instead if you need LDAP support for sudoers.

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

See policy of apt package

Type the command:
$ apt list -a {pkgNameHere}
$ apt list -a sudo

Sample outputs:

Listing... Done
sudo/zesty,now 1.8.19p1-1ubuntu1 amd64 [installed]

List all installed packages

The syntax is:
$ apt list --installed
$ apt list --installed | grep {pkgNameHere}

Sample outputs:

Fig.04: List all installed packages

Fig.04: List all installed packages

List package dependency

The depends option shows a listing of each dependency a package has and all the possible other packages that can fulfill that dependency. For example:
$ apt depends {pkgNameHere}
$ apt depends sudo

Sample outputs:

  Depends: libaudit1 (>= 1:2.2.1)
  Depends: libc6 (>= 2.17)
  Depends: libpam0g (>=
  Depends: libselinux1 (>= 1.32)
  Depends: libpam-modules
  Depends: lsb-base
  Conflicts: sudo-ldap
  Replaces: sudo-ldap

How do I performs recursive dependency listings similar to apt-cache?

The syntax is:
$ apt rdepends {pkgNames}
$ apt rdepends sudo

How do I hold a package?

Package holding means it can not be upgraded till you run unhold on it again. The syntax is:
$ sudo apt-mark hold {pkgName}
$ sudo apt-mark hold sudo

How do I unhold a package?

The syntax is as follows:
$ sudo apt-mark unhold {pkgName}
$ sudo apt-mark unhold sudo

See “apt-get hold back packages on Ubuntu / Debian Linux” for other options to hold and unhold packages.

How do I edit the source information file i.e. /etc/apt/sources.list?

The syntax is:
$ sudo apt edit-sources

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

We explained apt commands explained with plenty of practical examples for new and seasoned sysadmins/developers.

  • apt(8) Linux/Unix command man page or see this page here

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 13 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • Mark Ulmer Apr 3, 2016 @ 3:44

    I’m so glad this is not an April fools joke. Thanks for apt help.

  • Error Apr 18, 2016 @ 21:02

    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.

  • Pothi Kalimuthu May 2, 2016 @ 6:54

    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!

  • Ravi J May 26, 2016 @ 9:18

    Excellent article. Short, and to the point.

  • Francisco May 7, 2017 @ 19:31

    I didn’t know this exist! :O

    Cool thing. Thanks for sharing.

  • Marty May 8, 2017 @ 10:36

    Never knew about the dependency feature. It should work if I am trying to build a package from source and need the dependencies correct?

  • Eduardo Jorge May 9, 2017 @ 10:13

    What is the equivalent apt command to rpm -qa | grep flash-player?
    Thank you!

    • 🐧 Vivek Gite May 9, 2017 @ 13:45

      apt list | grep whatever
      OR much better without piping output
      apt list '*flash*'

  • Yash Pal Aug 16, 2017 @ 14:42

    Thanks for the lucid article. Can I ask you to explain what ‘w’ ‘ii’ and another letter (I have forgotten) mean when trying to upgrade with apt.
    These letters precede the description of packages.

  • Frank Sep 11, 2017 @ 12:23

    How can I only upgrade security updates? Like for example:
    aptitude safe-upgrade -t $(lsb_release -sc)-security

  • Pablo AB Jan 5, 2021 @ 6:10

    Isn’t apt-mark hold instead of apt hold? BTW apt hold would be great!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum