19 Ubuntu / Debian Linux apt Command Examples

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?





ADVERTISEMENTS



APT stands for Advanced Package Tool. It is a set of core tools inside Debian and Ubuntu Linux system. You can use theto 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.

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:



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:



$ apt list --upgradable

How to perform full system upgrade

To see the list of packages that can be upgraded on the system, enter:

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



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:



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:



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:



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

Conclusion

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

Category List of Unix and Linux commands File Management cat Network Utilities dig • host • ip • Package Manager apk • apt Processes Management bg • chroot • disown • fg • jobs • kill • killall • pwdx • time • pidof • pstree Searching whereis • which User Information id • groups • last • lastcomm • logname • users • w • who • whoami • lid/libuser-lid • members