15 apt command practical examples in Linux [Cheat Sheet]


Rohan Timalsina

CheatSheet

Introduction to apt command

apt (Advanced Package Tool) is a package management tool in Debian-based Linux systems. You can use apt command to install, update, remove, or query packages and their dependencies. APT was ported and became APT-RPM to manage the rpm package format. APT-RPM can be used by any RPM-based distribution, such as Conectiva, Red Hat, SUSE, ALT-Linux, etc.

 

Comparison between apt and apt-get

Both apt and apt-get are command-line tools for handling packages.

  • Most of the commands of apt are similar to apt-get but you have to use apt-cache for search and show command.
  • apt is designed as an end user interface and its output may be changed between versions.
  • apt contains the most used commands and options from more specialized APT tools like apt-get and apt-cache.
  • Besides, apt includes the progress bar and colour texts in the output for an interactive use.

 

How apt command works

apt command uses repositories to manage the packages. Often, the repositories that come pre-installed with Ubuntu will suffice for the majority of the Debian packages you'll install via APT. Software repositories are essentially URLs in a text file, stored in one of two places.

  • The main Ubuntu repository list is stored in /etc/apt/sources.list. Inside that file, you'll find a multitude of repositories for Ubuntu's package manager to pull packages from.
  • In addition, files with an extension of .list are read from the /etc/apt/sources.list.d/ directory and are also used whenever you use apt.

A typical repository line in either of these two files will look similar to the following:

deb http://us.archive.ubuntu.com/ubuntu/ bionic main restricted

Here,

  • The first section of each line will be either deb or deb-src, which references whether the apt command will find binary packages (deb) or source packages (deb-src), there.
  • In the second section, we have the actual URL which apt will use in order to reach the repository.
  • In the third section, we have the code-name of the release; in this case, it's bionic (which refers to the code-name for Ubuntu 18.04, Bionic Beaver).
  • The fourth section of each repository line refers to the Component, which references whether or not the repository contains software that is free and open source
  • The component can be main, restricted, universe, or multiverse.
  • Repositories with a main component include officially supported software. This generally means that the software packages have source code available, so Ubuntu developers are able to fix bugs.
  • Software marked restricted is still supported but may have a questionable license.
  • Universe packages are supported by the community, not Canonical themselves.
  • Finally, multiverse packages contain software that is neither free nor supported, which you would be using at your own risk.

 

Different examples to use apt command

Only root users or sudo users have permission to execute apt command. The basic syntax of apt command would be:

apt [options] command [package name]

In this article, we will teach you to use apt commands to manage the packages and their dependencies.

 

1. Install a package with apt command

You can use this command to install a package in your system. You need to specify the package name for installing that package.

# apt install package_name

Sample Output:

apt command to install package

You can specify multiple packages to install all of them.

# apt install package1 package2 package3

 

2. Remove a package with apt command

To uninstall a package from the system, you can use the remove command followed by the package name.

# apt remove package_name

Sample Output:

apt command to remove package

Since remove command does not delete configuration files, you can use purge command to remove packages along with their configuration files.

# apt purge package_name

 

3. apt command to show package details

This command prints the detailed information of a specified package.

# apt show package_name

Sample Output:

root@golinux:~# apt show gcc
Package: gcc
Version: 4:5.3.1-1ubuntu1
Priority: optional
Section: devel
Source: gcc-defaults (1.150ubuntu1)
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian GCC Maintainers <debian-gcc@lists.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 45.1 kB
Provides: c-compiler
Depends: cpp (>= 4:5.3.1-1ubuntu1), gcc-5 (>= 5.3.1-3~)
Recommends: libc6-dev | libc-dev
Suggests: gcc-multilib, make, manpages-dev, autoconf, automake, libtool, flex, bison, gdb, gcc-doc
Conflicts: gcc-doc (<< 1:2.95.3)
Task: ubuntu-desktop, ubuntu-usb, kubuntu-desktop, kubuntu-full, edubuntu-desktop, edubuntu-usb, xubuntu-core, xubuntu-desktop, mythbuntu-frontend, mythbuntu-desktop, mythbuntu-backend-slave, mythbuntu-backend-master, ubuntustudio-desktop-core, ubuntustudio-desktop, ubuntu-gnome-desktop, ubuntukylin-desktop
Build-Essential: yes
Supported: 5y
Download-Size: 5,250 B
APT-Manual-Installed: yes
APT-Sources: http://np.archive.ubuntu.com/ubuntu xenial/main i386 Packages
Description: GNU C compiler
This is the GNU C compiler, a fairly portable optimizing compiler for C.
.
This is a dependency package providing the default GNU C compiler.

 

4. Search for packages with apt command

search command allows you to search a pattern in the name and description of packages.

# apt search pattern

Sample Output:

root@golinux:~# apt search vim
Sorting... Done
Full Text Search... Done
clang-format-3.9/xenial-security 1:3.9.1-4ubuntu3~16.04.2 i386
Tool to format C/C++/Obj-C code
clang-format-4.0/xenial-security 1:4.0-1ubuntu1~16.04.2 i386
Tool to format C/C++/Obj-C code

clang-format-8/xenial-security 1:8-3~ubuntu16.04.1 i386
Tool to format C/C++/Obj-C code

exuberant-ctags/xenial 1:5.9~svn20110310-11 i386
build tag file indexes of source code definitions

grilo-plugins-0.2-base/xenial,now 0.2.17-0ubuntu2 i386 [installed]
Framework for discovering and browsing media - Base Plugins

tmux/xenial 2.1-3build1 i386
terminal multiplexer

vim/xenial-security 2:7.4.1689-3ubuntu1.5 i386
Vi IMproved - enhanced vi editor

vim-athena/xenial-security 2:7.4.1689-3ubuntu1.5 i386
Vi IMproved - enhanced vi editor - with Athena GUI

...

 

5. Update the list of available packages with apt command

You can use this command to update the list of available packages from the configured repositories. The update argument is used to download package information from all configured sources.

# apt update

Sample Output:

root@golinux:~# apt update
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
Hit:2 http://np.archive.ubuntu.com/ubuntu xenial InRelease 
Get:3 http://np.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB] 
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB] 
Get:4 http://security.ubuntu.com/ubuntu xenial-security/main i386 DEP-11 Metadata [93.7 kB] 
Get:5 http://security.ubuntu.com/ubuntu xenial-security/universe i386 DEP-11 Metadata [130 kB] 
Get:6 http://security.ubuntu.com/ubuntu xenial-security/multiverse i386 DEP-11 Metadata [2,468 B] 
Get:3 http://np.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB] 
Get:7 http://np.archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB] 
Get:8 http://np.archive.ubuntu.com/ubuntu xenial/universe i386 Packages [7,512 kB] 

...

Get:38 http://np.archive.ubuntu.com/ubuntu xenial-backports/universe i386 DEP-11 Metadata [6,608 B] 
Get:39 http://np.archive.ubuntu.com/ubuntu xenial-backports/universe DEP-11 64x64 Icons [5,608 B] 
Get:40 http://np.archive.ubuntu.com/ubuntu xenial-backports/multiverse i386 DEP-11 Metadata [216 B] 
Get:41 http://np.archive.ubuntu.com/ubuntu xenial-backports/multiverse DEP-11 64x64 Icons [29 B] 
Fetched 28.2 MB in 11min 30s (40.8 kB/s) 
Reading package lists... 77%
Reading package lists... Done
Building dependency tree 
Reading state information... Done
498 packages can be upgraded. Run 'apt list --upgradable' to see them.

 

6. apt command to upgrade packages

This command allows you to upgrade all the installed packages and their dependencies to the latest version via sources.list . New packages will be installed if required to satisfy dependencies, but existing packages will never be removed. If an upgrade for a package requires the remove of an installed package the upgrade for this package isn't performed.

# apt upgrade

Sample Output:

root@golinux:~# apt upgrade
Reading package lists... Done
Building dependency tree 
Reading state information... Done
Calculating upgrade... Done
The following package was automatically installed and is no longer required:
snapd-login-service
Use 'apt autoremove' to remove it.
The following NEW packages will be installed:
distro-info libzstd1 linux-headers-4.15.0-142
linux-headers-4.15.0-142-generic linux-image-4.15.0-142-generic
linux-modules-4.15.0-142-generic linux-modules-extra-4.15.0-142-generic
python3-distro-info python3-yaml ubuntu-advantage-tools xdg-desktop-portal

...
Do you want to continue? [Y/n] y
Get:1 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 libc6-dev i386 2.23-0ubuntu11.3 [1,677 kB]
Get:2 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 libc-dev-bin i386 2.23-0ubuntu11.3 [66.5 kB]
Get:3 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 linux-libc-dev i386 4.4.0-210.242 [832 kB]
Get:4 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 libc6 i386 2.23-0ubuntu11.3 [2,272 kB]

To upgrade a particular package, specify the package name.

# apt upgrade package_name

 

7. View the list of upgradable packages using apt command

If you need to view the list of upgradable packages in your system, you can use the following command.

# apt list --upgradable

Sample Output:

deepak@golinux:~$ apt list --upgradable
Listing... Done
accountsservice/xenial-updates,xenial-security 0.6.40-2ubuntu11.6 i386 [upgradable from: 0.6.40-2ubuntu11.3]
amd64-microcode/xenial-updates,xenial-security 3.20191021.1+really3.20180524.1~ubuntu0.16.04.2 i386 [upgradable from: 3.20180524.1~ubuntu0.16.04.2]
apparmor/xenial-updates,xenial-security 2.10.95-0ubuntu2.11 i386 [upgradable from: 2.10.95-0ubuntu2.9]
apport/xenial-updates,xenial-security 2.20.1-0ubuntu2.30 all [upgradable from: 2.20.1-0ubuntu2.18]
apport-gtk/xenial-updates,xenial-security 2.20.1-0ubuntu2.30 all [upgradable from: 2.20.1-0ubuntu2.18]
apt/xenial-updates 1.2.35 i386 [upgradable from: 1.2.27]
apt-transport-https/xenial-updates 1.2.35 i386 [upgradable from: 1.2.27]
apt-utils/xenial-updates 1.2.35 i386 [upgradable from: 1.2.27]
aptdaemon/xenial-updates,xenial-security 1.1.1+bzr982-0ubuntu14.5 all [upgradable from: 1.1.1+bzr982-0ubuntu14]
aptdaemon-data/xenial-updates,xenial-security 1.1.1+bzr982-0ubuntu14.5 all [upgradable from: 1.1.1+bzr982-0ubuntu14]
apturl/xenial-updates 0.5.2ubuntu11.2 i386 [upgradable from: 0.5.2ubuntu11.1]
apturl-common/xenial-updates 0.5.2ubuntu11.2 i386 [upgradable from: 0.5.2ubuntu11.1]
aspell/xenial-updates,xenial-security 0.60.7~20110707-3ubuntu0.1 i386 [upgradable from: 0.60.7~20110707-3build1]

...
cpp-5/xenial-updates,xenial-security 5.4.0-6ubuntu1~16.04.12 i386 [upgradable from: 5.4.0-6ubuntu1~16.04.10]
cups/xenial-updates,xenial-security 2.1.3-4ubuntu0.11 i386 [upgradable from: 2.1.3-4ubuntu0.5]
cups-browsed/xenial-updates,xenial-security 1.8.3-2ubuntu3.5 i386 [upgradable from: 1.8.3-2ubuntu3.4]
cups-bsd/xenial-updates,xenial-security 2.1.3-4ubuntu0.11 i386 [upgradable from: 2.1.3-4ubuntu0.5]
cups-client/xenial-updates,xenial-security 2.1.3-4ubuntu0.11 i386 [upgradable from: 2.1.3-4ubuntu0.5]

 

8. Upgrade the full system with apt command

full-upgrade command allows you to upgrade the whole system. It will install/remove/upgrade packages if that is required to upgrade the whole system.

# apt full-upgrade

Sample Output:

root@golinux:~# apt full-upgrade
Reading package lists... Done
Building dependency tree 
Reading state information... Done
Calculating upgrade... Done
The following package was automatically installed and is no longer required:
snapd-login-service
Use 'apt autoremove' to remove it.
The following NEW packages will be installed:
distro-info libzstd1 linux-headers-4.15.0-142
linux-headers-4.15.0-142-generic linux-image-4.15.0-142-generic
linux-modules-4.15.0-142-generic linux-modules-extra-4.15.0-142-generic
python3-distro-info python3-yaml ubuntu-advantage-tools xdg-desktop-portal
xdg-desktop-portal-gtk
The following packages will be upgraded:
accountsservice amd64-microcode apparmor apport apport-gtk apt
apt-transport-https apt-utils aptdaemon aptdaemon-data apturl apturl-common
aspell avahi-autoipd avahi-daemon avahi-utils base-files bash bind9-host
binutils bluez-cups bluez-obexd bsdutils busybox-initramfs busybox-static

...

debconf-i18n dh-python distro-info-data dmidecode dnsmasq-base dnsutils dpkg
dpkg-dev e2fslibs e2fsprogs evince evince-common evolution-data-server
evolution-data-server-common evolution-data-server-online-accounts file
file-roller firefox firefox-locale-en fonts-opensymbol friendly-recovery
fwupd g++-5 gcc-5 gcc-5-base geoclue-ubuntu-geoip gettext gettext-base
wireless-regdb wpasupplicant x11-common xdg-utils xorg xserver-common
xserver-xorg-core-hwe-16.04 xserver-xorg-legacy-hwe-16.04 xterm zlib1g
498 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
Need to get 520 MB of archives.
After this operation, 440 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 base-files i386 9.4ubuntu4.13 [61.8 kB]
Get:2 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 bash i386 4.3-14ubuntu1.4 [593 kB]
Get:3 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 bsdutils i386 1:2.27.1-6ubuntu3.10 [53.0 kB]
Get:4 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 libc6-dbg i386 2.23-0ubuntu11.3 [3,124 kB]
Get:5 http://np.archive.ubuntu.com/ubuntu xenial-updates/main i386 libc6-dev i386 2.23-0ubuntu11.3 [1,677 kB]

 

9. apt command to remove unused packages

When installing a new package on the system, the package dependencies are automatically installed. When you remove a package, those dependencies remain on the system and are no longer used. To remove all unused packages and dependencies, you can use the following command.

# apt autoremove

Sample Output:

root@golinux:~# apt autoremove
Reading package lists... Done
Building dependency tree 
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 498 not upgraded.

 

10. List all packages with apt command

You can use the following command to view the list of all packages that are available for your system.

# apt list

Sample Output:

root@golinux:~# apt list 
Listing... Done
qtpositioning5-doc-html/xenial 5.5.1-3ubuntu1 all
qtpositioning5-examples/xenial 5.5.1-3ubuntu1 all
qtpurchasing5-dbg/xenial 5.6.0~git20151023.2f454143-0ubuntu4 i386
qtpurchasing5-dev/xenial 5.6.0~git20151023.2f454143-0ubuntu4 i386
qtqr/xenial 1.4~bzr21-1 all
qtquick1-5-dbg/xenial 5.5.1-2build1 i386
...
qtscript5-dbg/xenial 5.5.1+dfsg-2build1 i386
qtscript5-dev/xenial 5.5.1+dfsg-2build1 i386
qtscript5-doc/xenial-updates 5.5.1-3ubuntu0.1 all
qtscript5-doc-html/xenial 5.5.1+dfsg-2build1 all
qtscript5-examples/xenial 5.5.1+dfsg-2build1 i386
qtscript5-private-dev/xenial 5.5.1+dfsg-2build1 i386
qtscrob/xenial 0.11+git-2 i386

 

11. List all installed packages with apt command

This command can help you to get the list of all installed packages on your system.

# apt list --installed

Sample Output:

root@golinux:~# apt list 
Listing... Done
qtpositioning5-doc-html/xenial 5.5.1-3ubuntu1 all
qtpositioning5-examples/xenial 5.5.1-3ubuntu1 all
qtpurchasing5-dbg/xenial 5.6.0~git20151023.2f454143-0ubuntu4 i386
qtpurchasing5-dev/xenial 5.6.0~git20151023.2f454143-0ubuntu4 i386
qtqr/xenial 1.4~bzr21-1 all

...
qtscript5-dbg/xenial 5.5.1+dfsg-2build1 i386
qtscript5-dev/xenial 5.5.1+dfsg-2build1 i386
qtscript5-doc/xenial-updates 5.5.1-3ubuntu0.1 all
qtscript5-doc-html/xenial 5.5.1+dfsg-2build1 all
qtscript5-examples/xenial 5.5.1+dfsg-2build1 i386
qtscript5-private-dev/xenial 5.5.1+dfsg-2build1 i386
qtscrob/xenial 0.11+git-2 i386

To list the specific package, you can use:

# apt list --installed | grep package_name

 

12. Check dependencies of package using apt

You can use this command to print the dependencies information of a specified package.

# apt depends package_name

Sample Output:

apt command to check dependencies

 

13. Avoid Yes/No prompt with apt command

To avoid Yes/No prompt and assume Yes, you can use -y option.

apt command to avoid yes

 

14. List the policy of a package using apt command

You can use -a option with list command to list the policy of a specific package.

# apt -a list package_name

Sample Output:

root@golinux:~# apt -a list python
Listing... Done
python/xenial-updates,now 2.7.12-1~16.04 i386 [installed]
python/xenial 2.7.11-1 i386

 

15. apt command to edit the source information file

With the following command, you can edit the source information file /etc/apt/sources.list.

# apt edit-sources

 

Conclusion

This article explains the different examples of apt commands for handling packages. It is an essential tool to install, remove, update, or query packages and their dependencies in Debian-based Linux systems. We hope we have helped you to learn apt command and feel free to ask questions in a comment section.

 

What's Next

25+ DNF command practical examples in Linux

 

Further Reading

man page for apt command

 

Views: 254

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can reach out to him on LinkedIn or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

GoLinuxCloud Logo


We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages

JavaScript

Python

Golang

Node.js

Java

Laravel