
Understanding the apt-get package management tool
Ubuntu supports an Advanced Packaging Tool (APT) for package management: apt-get is one of the command-line tools to aid the user in installing, removing, upgrading packages, updating package index, and also upgrading the entire Ubuntu OS. Its simplicity is the reason behind its powerful features and why it is better than other GUI package management tools. Server administrators can use it over SSH connections and also they can use it in scripts for administrative purposes. Automation of scripts using cron scheduling is also an added advantage for server administrators when it comes to dealing with package management using apt-get.
Let's first discuss about the apt-get package and its various uses. Later, we will look at other utilities under APT, such as apt-cache, apt-file, apt-ftparchive, and so on.
Updating the repository list with the apt-get update command
It is recommended to always update the repository list in the local Ubuntu system before installing and/or removing the packages. It is possible that the package could have been upgraded along with changes in the dependencies. To be on the safe side, update the repository list with the following command:
sudo apt-get update
The preceding command will update the repository list and you will see an output similar to the following screenshot. The repositories are defined in the /etc/apt/sources.list
file and the /etc/apt/sources.list.d
directory. It is a database of all the packages available in the repositories. When you run the update
command from the apt-get tool, it updates the list in the files and directories discussed earlier.
The local package index is updated to the latest list available in the remote repository.

Installing a package with the apt-get install command
It is quite easy to install a package with the apt-get tool. To install the apache2 package, type the following command:
sudo apt-get install apache2
The output is shown in the following screenshot:

The install
command will read the repository list and try downloading the package from the URL listed. Once downloaded, the apt-get install tool will unpack and install the software. If there are additional packages required for the proper functioning of this software, then apt-get will also download the additional dependencies and install them too. This is one of the features that makes apt-get one of the desired package management tools for server administrators working on Debian-based systems.
You will see the preceding output after you install apache2 using the apt-get install tool. The user will be prompted if he approves the downloading of the package. Hit Y and Enter to proceed with download and install.
After successful installation, you will see an output similar to the following screenshot:

Now, let's try to remove this package using the apt-get tool. We will use the remove
option. You need to specify the package name that you wish to remove from the machine. If you wish to remove or install multiple packages at once, you can mention them one after the other with a space as the separator. The following command will remove multiple packages apache2, apache2-bin, and ssl-cert:
sudo apt-get remove apache2 apache2-bin ssl-cert
After running the preceding command, apt-get removes the packages. The following screenshot shows those packages were removed. apt-get handles the dependencies, unlike the dpkg package management tool.

Upgrading a package with the apt-get upgrade command
Before we upgrade, we need to run the update
command. The update
command will update all packages to the latest version, and the upgrade
command actually downloads and installs the updated packages. The command for updating a package is as follows:
sudo apt-get update
apt-get allows the software packages to be upgraded to newer versions with the upgrade
option. Newer versions of software become available with bug fixes or with additional functionalities on the remote repositories. Running the following command will upgrade the installed packages already installed on your machine with newer versions. Be sure to run the update
command before you do an upgrade:
sudo apt-get upgrade
You will see an output similar to the following screenshot:

Here, we see that around 98 packages are ready to be upgraded. The server will get 49 MB of data downloaded from the repositories and install them.
Cleaning with the apt-get clean command
Over a period of time, your system will accumulate loads of downloaded packages, copies of which are stored in /var/cache/apt/archives
. If you want to remove only the packages that are obsolete, then use the autoclean
option instead of clean
with the apt-get tool. If you want to remove the .deb
packages from the directory, run the following command:
sudo apt-get clean
The content of the folder is shown in the following screenshot:

Purging a package with the apt-get purge command
If you wish to remove a package along with all its configuration files, use the following command:
sudo apt-get purge ssl-cert
You can remove and purge multiple packages too, as we did for the remove
package command. Once you execute the preceding command, you will be able to see the output similar to the following screenshot. Once you purge a package, the dkpg tool will have no information about the package. All it will know is that the package was removed.

Fixing unsuccessful installations with the apt-get –f command
There are times when your package is not getting installed in spite of multiple tries. Sometimes, you might face a dependency issue whilst you are installing a package using the apt-get install
command. Even though the apt-get package management tool is built to handle dependencies, this issue can pop up. To solve this issue, use the following command:
sudo apt-get –f install
This command will try and repair the broken dependencies in your system and the installation of the package will be successful. You might need to run this command when you are using the apt-get install
command to install anything for the first time on a new server.
The output is shown in the following screenshot:

Checking for broken dependencies with the apt-get check command
The apt-get check
command is a diagnostic tool for package management. There can be instances where your system has broken dependencies. To verify the broken dependency issues, you can use the following command:
sudo apt-get check
This will update the package lists and check for the broken dependencies. It also updates the package cache.
This brings us to the end of the apt-get tool. However, there are additional tools from the apt-* family. We will look at apt-ftparchive in the Creating a repository mirror section where we will set up a repository mirror. Let's first look at the apt-cache tool.