Linux apt Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the apt command, a powerful package management tool for Ubuntu and Debian-based Linux distributions. The lab covers the structure and basic usage of the apt command, as well as practical examples for installing, updating, searching, and removing software packages. By the end of this lab, you will have a solid understanding of how to manage packages on your Linux system using the apt command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") subgraph Lab Skills linux/apt -.-> lab-422549{{"`Linux apt Command with Practical Examples`"}} linux/rm -.-> lab-422549{{"`Linux apt Command with Practical Examples`"}} linux/sudo -.-> lab-422549{{"`Linux apt Command with Practical Examples`"}} end

Understand the apt Command Structure

In this step, we will explore the structure and basic usage of the apt command in Linux. The apt command is a powerful package management tool used to install, update, search, and remove software packages on Ubuntu and Debian-based Linux distributions.

Let's start by understanding the general structure of the apt command:

sudo apt [command] [options] [package]

Here's what each part of the command means:

  • sudo: Runs the command with superuser (root) privileges, which is required for most package management operations.
  • apt: The main package management command.
  • [command]: The specific action to perform, such as install, update, search, or remove.
  • [options]: Additional flags or parameters to customize the command's behavior.
  • [package]: The name of the package you want to install, update, search, or remove.

Example: To install the htop package, you would run:

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

In the next steps, we will explore more advanced usage of the apt command, such as updating packages, searching for packages, and removing packages.

Install and Update Packages Using apt

In this step, we will learn how to install new packages and update existing packages using the apt command.

First, let's update the package index to ensure we have the latest information about 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, let's install a new package, for example, the tree command, which displays the contents of a directory in a tree-like format:

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 a package, you can use the apt upgrade command. This will upgrade all installed packages to their latest versions:

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

In the next step, we will learn how to search for packages and remove installed packages using the apt command.

In this step, we will learn how to search for packages and remove installed packages using the apt command.

To search for a package, you can use the apt search command. For example, 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 remove an installed package, you can use the apt remove command. For example, let's remove the tree package that we installed in the previous step:

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 its associated configuration files, you can use the apt purge command instead of apt remove:

sudo apt purge tree

This will remove the package and its configuration files, leaving no trace of the package on the system.

In the next steps, we will explore more advanced package management tasks using the apt command.

Summary

In this lab, you first learned the structure and basic usage of the apt command, including the different parts of the command and how to use it to install packages. You then explored how to update the package index, install new packages, and update existing packages using apt. Finally, you learned how to search for and remove packages with apt. Throughout the lab, you were provided with practical examples to reinforce your understanding of the apt command's capabilities.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like