apt Command in Linux

Introduction to APT Package Management

This lab provides a comprehensive guide on utilizing the apt command, a cornerstone tool for package management within Ubuntu and Debian-based Linux environments. You will delve into the anatomy and core functionalities of the apt command, reinforced by practical scenarios covering software package installation, updates, searches, and removal. By the lab's conclusion, you'll possess a firm grasp on managing software packages on your Linux system through apt.

Understanding the Structure of the apt Command

This segment dissects the structure and fundamental usage of the apt command in Linux. The apt command stands as a robust package management solution, enabling the installation, updating, searching, and removal of software packages across Ubuntu and Debian-based Linux distributions.

Let's begin by deconstructing the generic structure of the apt command:

sudo apt [command] [options] [package]

Here's a breakdown of each component:

  • sudo: Executes the command with superuser (root) privileges, essential for most package management tasks.
  • apt: The primary package management command.
  • [command]: The specific operation to execute, such as install, update, search, or remove.
  • [options]: Optional flags or parameters that tailor the command's behavior.
  • [package]: The identifier of the package to be installed, updated, searched, or removed.

Example: To install the htop package, execute:

sudo apt install htop

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  htop
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/83.0 kB of archives.
After this operation, 282 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Selecting previously unselected package htop.
(Reading database ... 115729 files and directories currently installed.)
Preparing to unpack .../htop_3.0.5-7_amd64.deb ...
Unpacking htop (3.0.5-7) ...
Setting up htop (3.0.5-7) ...

Subsequent sections will delve into more sophisticated uses of the apt command, including package updates, package searches, and package removal procedures.

Installing and Updating Software Packages using apt

This section details the process of installing fresh packages and updating existing ones with the apt command.

Initially, refresh the package index to procure the latest information regarding available packages:

sudo apt update

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done

Now, proceed to install a new package, such as the tree command, which presents directory content in a hierarchical manner:

sudo apt install tree

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 55.9 kB of archives.
After this operation, 184 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 tree amd64 1.8.0-1build1 [55.9 kB]
Fetched 55.9 kB in 0s (0 B/s)
Selecting previously unselected package tree.
(Reading database ... 116022 files and directories currently installed.)
Preparing to unpack .../tree_1.8.0-1build1_amd64.deb ...
Unpacking tree (1.8.0-1build1) ...
Setting up tree (1.8.0-1build1) ...

To update packages, utilize the apt upgrade command. This will upgrade all currently installed packages to their newest releases:

sudo apt upgrade

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  libc-bin libc6 libssl3 openssl
4 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,007 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libc-bin amd64 2.35-0ubuntu3.1 [1,114 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libc6 amd64 2.35-0ubuntu3.1 [1,693 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libssl3 amd64 3.0.2-0ubuntu1.8 [124 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 openssl amd64 3.0.2-0ubuntu1.8 [76.0 kB]
Fetched 3,007 kB in 1s (2,280 kB/s)
(Reading database ... 116027 files and directories currently installed.)
Preparing to unpack .../libc-bin_2.35-0ubuntu3.1_amd64.deb ...
Unpacking libc-bin (2.35-0ubuntu3.1) over (2.35-0ubuntu3) ...
Preparing to unpack .../libc6_2.35-0ubuntu3.1_amd64.deb ...
Unpacking libc6 (2.35-0ubuntu3.1) over (2.35-0ubuntu3) ...
Preparing to unpack .../libssl3_3.0.2-0ubuntu1.8_amd64.deb ...
Unpacking libssl3 (3.0.2-0ubuntu1.8) over (3.0.2-0ubuntu1.7) ...
Preparing to unpack .../openssl_3.0.2-0ubuntu1.8_amd64.deb ...
Unpacking openssl (3.0.2-0ubuntu1.8) over (3.0.2-0ubuntu1.7) ...
Setting up libc-bin (2.35-0ubuntu3.1) ...
Setting up libc6 (2.35-0ubuntu3.1) ...
Setting up libssl3 (3.0.2-0ubuntu1.8) ...
Setting up openssl (3.0.2-0ubuntu1.8) ...

The following section will guide you on how to search for packages and then uninstall them via the apt command.

Searching and Uninstalling Packages using apt

This section explains how to find software packages within your repositories and then uninstall those you no longer need, all utilizing the apt command.

To search for a particular package, you can invoke the apt search command. As an illustration, let's search for the "vim" package:

sudo apt search vim

Example output:

Sorting... Done
Full Text Search... Done
vim/jammy 2:8.2.3995-1ubuntu2 amd64
  Vi IMproved - enhanced vi editor
vim-common/jammy 2:8.2.3995-1ubuntu2 amd64
  Vi IMproved - Common files
vim-gtk3/jammy 2:8.2.3995-1ubuntu2 amd64
  Vi IMproved - GTK3 GUI version
vim-nox/jammy 2:8.2.3995-1ubuntu2 amd64
  Vi IMproved - enhanced vi editor (without GUI)
vim-runtime/jammy 2:8.2.3995-1ubuntu2 all
  Vi IMproved - Runtime files
vim-tiny/jammy 2:8.2.3995-1ubuntu2 amd64
  Vi IMproved - enhanced vi editor - compact version

The search results show that there are several vim packages available, including the full vim package, as well as more specialized variants like vim-gtk3 and vim-tiny.

To uninstall an existing package, you can make use of the apt remove command. As an illustration, we will remove the tree package that was installed earlier:

sudo apt remove tree

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  tree
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 184 kB of disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 116032 files and directories currently installed.)
Removing tree (1.8.0-1build1) ...

If you want to remove a package *and* all of its associated configuration files, use the apt purge command instead of apt remove:

sudo apt purge tree

This removes the package and its configuration files, leaving no traces on the system. This is useful for a clean uninstall.

In the next steps, we will explore more advanced package management tasks using the apt command, equipping you with systemadmin skills.

Conclusion

This lab commenced with an introduction to the structure and fundamental application of the apt command, elucidating its various components and its role in package installation. You subsequently learned how to update the package index, install new packages, and update those that already exist. Lastly, you learned how to search for and completely remove packages from your Linux system. Each concept was accompanied by hands-on examples designed to bolster your comprehension of the apt command's full potential. This knowledge empowers you to effectively manage software on Debian-based Linux systems.

400+ Linux Commands