How to use apt Command in Linux

Photo of author

By rasho

APT also known as Advanced Packaging Tool is the command-line tool for managing packages in Debian-based distributions like Ubuntu 16.04, Ubuntu 18.04, Debian 8, Debian 9 and much more. It combines the most frequently used commands from the apt-get and apt-cache tools with different default values of some options. APT simplifies the process of installing, removing, upgrading packages and even used to upgrade the entire operating system through the Command Line Interface.
In this tutorial, we will explain how to manage packages using APT command line tool on Ubuntu 18.04 LTS, server.

Requirements

A server running Ubuntu 18.04 LTS.
A root or sudo access on the server.
All commands below are run as root user. Either log in as root user on the shell or run:

sudo -s

to become the root user. As an alternative, you can prepend ‘sudo‘ to all commands.

1. Updating package index

The APT package index is basically a database that holds records of available packages from the repositories enabled in your system.
To update the package index run the command below. This will pull the latest changes from the APT repositories:

$ apt update -y

After running the above command, your database should be up-to-date.

2. Upgrading packages

The upgrade command is used to upgrade all the currently installed software packages to the newer version.

$ apt upgrade -y

You can also use dist-upgrade command to upgrade the packages, but it changes package dependencies with a smart conflict resolution method.

$ apt dist-upgrade

The difference between upgrade and full-upgrade is that the later will remove the installed packages if that is needed to upgrade the whole system.

$ apt full-upgrade -y

3. Installing packages

Once your database is updated, you can install any packages by running the following command:

$ apt instal package_name -y

If you want to install multiple packages specify them as a space-separated list:

$ apt install package_name1 package_name2

[ads]
To install local deb files provide the full path to file. Otherwise, the command will try to retrieve and install the package from the APT repositories.

$ apt install /full/path/file.deb

If you want to download only package file but not install it, you can run the following command:
The above command will download the package file in /var/cache/apt/archives directory.
To reinstall any package with the newer version run the following command:

$ apt install package_name1 --reinstall

4. Removing Packages

To remove an installed package type the following:

$ apt remove package_name

You can also specify multiple packages, separated by spaces:

$ apt remove package_name1 package_name2

The remove command will uninstall the given packages but it may leave some configuration files behind. If you want to remove the package including all configuration files use purge instead of remove :

$ apt purge package_name

You can also remove all unwanted packages and clean the database with the following command:

$ apt autoremove
$ apt clean

5. Listing Packages

The list command allows you to list the available, installed and upgradeable packages.
To list all available packages use the following command:

$ apt list

To list only the installed packages type:

$ apt list --installed

Getting a list of the upgradeable packages may be useful before actually upgrading the packages:

$ apt list --upgradeable

6. Searching Packages

This command allows you to search for a given package in the list of the available packages:

$ apt search package_name

If found the command will return the packages which name matches the search term.
[ads]

7. Package Information

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 package_name

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

$ apt depends package_name

Example:

$ apt depends sudo
sudo
  Depends: libaudit1
  Depends: libc6
  Depends: libpam0g
  Depends: libselinux1
  Depends: libpam-modules
  Depends: lsb-base
  Conflicts: sudo-ldap
  Replaces: sudo-ldap

Conclusion

Knowing how to manage packages is an essential part of Linux system administration.
To learn more about the apt command open your terminal and type man apt.
Feel free to leave a comment if you have any questions.

Leave a Comment