19 Ubuntu / Debian Linux apt Command Examples

Posted on in Categories , , last updated May 7, 2017
ip-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?

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

Purpose

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

Syntax

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
  linux-headers-4.4.0-16
  linux-headers-4.4.0-16-generic
  linux-image-4.4.0-16-generic
  linux-image-extra-4.4.0-16-generic
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
$ 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 (>= 0.99.7.1), 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:

sudo
  Depends: libaudit1 (>= 1:2.2.1)
  Depends: libc6 (>= 2.17)
  Depends: libpam0g (>= 0.99.7.1)
  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:
$ apt hold {pkgName}
$ apt hold sudo

How do I unhold a package?

The syntax is
$ apt unhold {pkgName}
$ apt unhold sudo

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
See also
  • apt(8)
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Package Managerapkapt
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationgroupsidlastlastcommlognameuserswwhowhoamilidmembers

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

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

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

Leave a Comment