apt command in Linux with examples

‘apt’ is a command line utility for installing, updating, and removing deb packages on Ubuntu, Debian, and their derivatives.

This is because the apt command integrates the functions of apt-get, apt-cache & apt-config along with essential commands in a well organized manner.

‘apt’ command is suitable for end users and does not contain some of the additional features of apt-get command. apt was designed to fix some of the fundamental dependency flaws in apt-get.

After Ubuntu 16.04 release, the ‘apt’ command grabbed attention of the users, than the traditional ‘apt-get’ command.

In this tutorial, we will show you how to use apt commands to manage packages in Debian and Ubuntu based Linux distributions.

Refer the following article to know the difference between apt vs apt-get command.

Common syntax & file location of apt command

General syntax of ‘apt’ command is as follows:

apt [options] [package 1] [package 2]...[package n]

Important ‘apt’ configuration files are as follows:

File NameDescription
/etc/apt/apt.confMain apt configuration file
/etc/apt/sources.listMain apt source repositories configuration file
/etc/apt/sources.list.d/apt additional software repositories configuration location
/var/cache/apt/archives/apt’s cached files are located
/var/log/apt/history.logMost recent apt history logs

1) Updating package index

APT package index is a database that contains information of available packages from various repositories enabled in the system.

Run the following command to update the package database, which pulls the updated package records from the APT repositories:

$ sudo apt update

2) Installing packages with apt

If you already know the name of the package that needs to be installed, use the following command and replace the [package_name] with the desired package:

sudo apt install [package_name]

For example, to install apache2, run the below command. Add ‘-y‘ with the ‘apt’ command to bypass the confirmation needed from you while installing the package:

$ sudo apt install apache2
or
$ sudo apt -y install apache2

To install multiple packages at once, specify each of the names separated by a space:

$ sudo apt install [package_1] [package_2]

To install local deb files, run the following command:

$ sudo apt install /path/to/file.deb

3) Removing packages using apt

Run the following command to remove an installed package from the system. At the same time multiple packages can be removed by specifying their names separated by a space:

$ sudo apt remove package_name

The ‘remove’ command will uninstall the given package, but it may leave some configuration files in the system. To remove a package and its associated configuration files, use the following command instead:

$ sudo apt purge package_name

4) Removing unused packages

Whenever a new package is installed on the system that installs related dependencies.

Unused dependency packages remain on the system whenever any package is removed using the ‘remove’ or ‘purge’ command, that are no longer needed by the system.

The ‘autoremove’ command removes all unwanted dependencies left on the system:

$ sudo apt autoremove

5) Upgrading packages

Available updates must be installed regularly to keep the system up-to-date and secure.

Run the following command to upgrade all the installed packages to their latest versions, but existing package won’t be removed:

$ sudo apt upgrade

To upgrade a single package, run the below command instead:

$ sudo apt install --only-upgrade package_name

6) How to use full-upgrade

‘full-upgrade’ works the same way as upgrade, but it also removes the installed packages if needed to perform system upgrades:

$ sudo apt full-upgrade

7) Searching packages

If you do not know the exact package name you want to install, use the search command that returns the matching string. For example, run the following command to search the list of packages related to ftp:

$ sudo apt search ftpd
p   atftpd                          - advanced TFTP server                      
p   atftpd:i386                     - advanced TFTP server                      
p   ftpd                            - File Transfer Protocol (FTP) server       
p   ftpd:i386                       - File Transfer Protocol (FTP) server       
p   ftpd-ssl                        - FTP server with SSL encryption support    
p   ftpd-ssl:i386                   - FTP server with SSL encryption support    
p   gadmin-proftpd                  - GTK+ configuration tool for proftpd       
p   gadmin-proftpd:i386             - GTK+ configuration tool for proftpd       
p   gadmin-proftpd-dbg              - GTK+ configuration tool for proftpd debug 
p   gadmin-proftpd-dbg:i386         - GTK+ configuration tool for proftpd debug

8) Check package information

To know the detailed information about a package before proceeding with the installation. Use the below command which gives package dependencies, version, installation size, packages source, etc,.

$ sudo apt show nano
Package: nano
State: installed
Automatically installed: no
Version: 2.2.6-1ubuntu1
Priority: standard
Section: editors
Maintainer: Ubuntu Developers <[email protected]>
Architecture: amd64
Uncompressed Size: 614 k
Depends: libc6 (>= 2.14), libncursesw5 (>= 5.6+20070908), libtinfo5, dpkg (>=
         1.15.4) | install-info
Suggests: spell
Conflicts: pico, pico, nano
Breaks: alpine-pico (<= 2.00+dfsg-5), alpine-pico (<= 2.00+dfsg-5)
Replaces: pico, pico
Provides: editor
Description: small, friendly text editor inspired by Pico
 GNU nano is an easy-to-use text editor originally designed as a replacement for
 Pico, the ncurses-based editor from the non-free mailer package Pine (itself
 now available under the Apache License as Alpine). 
 
 However, nano also implements many features missing in pico, including: 
 * feature toggles; 
 * interactive search and replace (with regular expression support); 
 * go to line (and column) command; 
 * auto-indentation and color syntax-highlighting; 
 * filename tab-completion and support for multiple buffers; 
 * full internationalization support.
Homepage: http://www.nano-editor.org/
</[email protected]>

9) How to clear apt cache

This command removes ‘.deb’ files for packages that are no longer installed on your system. Removing these files from ‘/var/cache/apt/archives’ directory can free up a significant amount of disk space.

$ sudo apt clean
$ sudo apt autoclean

10) Downloading ‘.deb’ package

To download a ‘.deb’ package without installing it, run the following command

$ sudo apt download package_name
$ sudo apt download nano

11) Check broken dependencies

“check” is a diagnostic command to update the package list and check for any broken dependencies:

$ sudo apt check

12) Fix broken packages

When you encounter “unmet dependencies” while installing the package, run the below command to repair the broken package.

$ sudo apt -f install

13) Listing packages

The ‘list’ command will display the available, installed, and upgradable packages.

To list all available packages use the following command:

$ sudo apt list

To list available package updates:

$ sudo apt list --upgradeable

To list only the installed packages:

$ sudo apt list --installed

14) Finding files belonging to a particular package

Use the below command to find the files and their location which belongs to a particular package:

$ sudo apt content package_name

$ sudo apt content nano
/.
/bin
/bin/nano
/bin/rnano
/etc
/etc/nanorc
/usr
/usr/bin
/usr/bin/nano
/usr/share
/usr/share/doc
/usr/share/doc-base

Use the command below to find a list of files in a given package:

$ sudo apt contains package_name

15) Checking package dependencies

Run the following command to check all the dependencies of a particular package:

$ sudo apt depends package_name

$ sudo apt depends nano
nano
  Depends: libc6
  Depends: libncursesw5
  Depends: libtinfo5
 |Depends: dpkg
    dpkg:i386
  Depends: install-info
    install-info:i386
  Suggests: spell
  Conflicts: 
  Conflicts: 
  Breaks: alpine-pico
  Breaks: alpine-pico:i386
  Replaces: 
  Replaces: 
  Conflicts: nano:i386

To verify missing dependencies of a package, use the below command:

$ sudo apt recommends nano
No missing recommended packages were found for nano

16) How to check installed package version

Run the below command to verify the version of a package installed in the system:

$ sudo apt version nano
2.2.6-1ubuntu1

17) Checking ‘changelog’ of the package

The ‘changelog’ command is used to check the list of changes that were made on the package by the development team. Often, it is used to verify if a particular ‘CVE’ issue has been fixed.

$ sudo apt changelog package_name

$ sudo apt changelog curl
curl (7.35.0-1ubuntu2.6) trusty-security; urgency=medium

  * SECURITY UPDATE: NTLM credentials not-checked for proxy connection
    re-use
    - debian/patches/CVE-2016-0755.patch: fix ConnectionExists to compare
      Proxy credentials in lib/url.c.
    - CVE-2016-0755

 -- Marc Deslauriers <[email protected]>  Tue, 26 Jan 2016 12:10:58 -0500

curl (7.35.0-1ubuntu2.5) trusty-security; urgency=medium
</[email protected]>

18) Adding PPAs

Use the below command to add third party PPAs in the system:

$ sudo apt add-repository ppa:PPA_name

19) apt man page

If you want to explore any other commands not listed here, go to the man page of the ‘apt’ command:

$ man apt
or
$ apt --help

Conclusion

This tutorial explains how to manage packages using ‘apt’ command in Debian/Ubuntu systems.

If you have any questions or feedback, feel free to comment below and we shall get back to you as soon as possible. Happy learning!

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

3 Comments on “apt command in Linux with examples”

  1. Thanks for the really helpful post. I think you have accidentally used apt-get in 2 and 12. You may want to correct this

Leave a Reply

Your email address will not be published. Required fields are marked *