Every Linux user knows how critical apt-get commands are. From the very first time you install Ubuntu (or a similar Linux distribution), you come across this powerful tool.

This tutorial serves as a good introduction for beginners on the apt-get command. It can also help more experienced users refresh their memory and improve their knowledge of terminal commands.

guide to using apt-get commands in linux

What is apt-get?

Advanced Package Tool (APT) is a package management system for Debian, Ubuntu, and other Linux distributions. It is quite powerful, allowing you to search for, install, manage, update, and remove software. This secure tool is easy to use since it does not build software from the source code. Instead, it compiles the source code or uses precompiled files to install software packages.

Different command-line programs interact with the APT library. The most popular by far is apt-get, along with apt-cache, apt-config and apt.


Note: All these commands, except the apt-cache (search) command, only work within root or superuser (sudo) privileges.


What’s the difference between apt-get and apt?

First, let’s start by distinguishing between apt-get and apt-cache. The difference is merely functional and you would use one or the other depending on what you want to do. You use apt-get for installing, upgrading and removing software packages. On the other hand, use apt-cache in instances when you need to search for new packages.

However, many users are still confused when it comes to the difference between apt-get and apt. Linux introduced apt in 2014 as a friendlier command-line for front-end users, that combines the most commonly used apt-get and apt-cache commands. The goal was to solve the problem of having too many functionalities that the average user had no need for.

The apt command line program introduced a couple of other innovations:

  • The progress bar, so you can follow the installation and removal of software packages
  • The number of packages that can be upgraded after updating the package repository

Using apt-get commands

How to update the package repository with apt-get

Before you install a package, it is important to resynchronize the package index files and update the package repository to the latest version. This ensures that the packages you install are up-to-date.

Update the package repository by running the command:

sudo apt-get update

What is the difference between update and upgrade?

There is a big difference between updating and upgrading. When you update your package repository, it only makes the newer versions of packages available in the repository.

However, it does not update the packages you already have installed. In order to do that, you have to upgrade your packages.

How to upgrade packages

To upgrade all installed packages, use the apt-get command:

sudo apt-get upgrade

To upgrade a specific package:

sudo apt-get upgrade <package_name>

What is the difference between upgrade and dist-upgrade?

The difference is that dist-upgrade searches for dependencies with newer versions, installs new packages and removes old ones on its own.

On the other hand, upgrade never installs or removes packages on its own. It only installs the latest versions of the installed packages on your system.

You should be careful when using dist-upgrade as it may remove some packages you still want and need on your system. The apt-get manual explains the function and underlying difference of both commands as follows:

difference between upgrade and dist-upgrade

How to install a package with apt-get

To install a package using apt-get, simply type in the following command:

sudo apt-get install <package_name>

Note: Replace <package_name> with the name of the software package you intend to install. If you do not know the exact name of the package you want to install, type in the first few letters and press TAB. It will suggest all the packages available, starting with those letters.


How to install a specific application version

If you do not want the latest version of a software, you can install an older one by specifying the exact version number.

sudo apt-get install *package number*=version number

Before it installs the package, you will receive a terminal output showing:

  • The additional packages required for the installation to be successful
  • The package you intend to install
  • The packages that must be upgraded
  • How much disk space will the package take up

For example, if you want to install curl, you will receive the output as in the image below:

Finally, it will ask you whether you wish to continue with the installation. To answer, you must type in y for yes or n for no.

How to install multiple packages

If you need to install more than one package, you can speed up the installation process by installing multiple packages at the same time with a single command line:

sudo apt-get install <package_name1> <package_name2> <package_name3>

How to remove installed packages with apt-get

You can remove a package with the remove command:

sudo apt-get remove <package_name>

However, this command only removes the package, leaving its configuration files on the system. If you want to remove the package along with its configuration files, use the command:

sudo apt-get purge <package_name>

How to clean up disk space

When you install a package, you have to download a file with a .deb extension. You can find this file in the /var/cache/apt/packages folder. After installing, you can delete the .deb files from your local repository so they do not take up disk space.

To clean the local repository of retrieved package files, use the command:

sudo apt-get clean

The clean command will remove everything except the local file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/

Another way to empty the local repository of retrieved files is to use autoclean to clear out packages that are no longer available, use the command:

sudo apt-get autoclean

To remove the unneeded packages that apt-get automatically installs to satisfy dependencies for other packages, run the command:

sudo apt-get autoremove

Using apt-cache

How to list all available packages

You can browse through all the available packages in the repository with the command:

apt-cache pkgnames

How to search for package using apt-cache

If you want to read a description of a package before you install it, you can do so by running the command:

apt-cache search <package_name>


Note: Don't worry if you do not know the exact name of a package. It will display results on the searched term by going through the names and the descriptions of all available packages.


How to find information about a package

If you know the exact name of the package you wish to install, but want to see all the information regarding that package, use the following command:

apt-cache showpkg <package_name>

How to view dependencies for specific packages

You can search through a list of dependencies a package has, along with all other possible packages that can fulfill that dependency with the command:

apt-cache depends pkg <package_name>

How to list all installed packages

In case you want to inspect which packages you already have on your system, you can do so with:

apt-cache pkgnames <package_name>

Conclusion

In this article, you learned the most common apt-get and apt-cache commands. They are simple to use and easy to remember. Once you have mastered the use of each command, you can manage all your packages with ease and confidence.