Linux apt-get Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the apt-get command, a powerful package management tool in Linux. You will understand the basics of the apt-get command, including how to install, update, and remove packages on your system. The lab covers practical examples of using apt-get commands to manage packages efficiently. The apt-get command provides a straightforward way to manage packages and keep your system up-to-date.

Linux Commands Cheat Sheet


Skills Graph

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

Understand the Basics of apt-get Command

In this step, you will learn the basics of the apt-get command, which is a powerful package management tool in Linux. The apt-get command allows you to install, update, remove, and manage packages on your system.

First, let's understand the general syntax of the apt-get command:

sudo apt-get [operation] [package_name]

The available operations include:

  • install: Install one or more packages
  • update: Update the package index
  • upgrade: Upgrade installed packages to their latest version
  • remove: Remove one or more packages
  • purge: Remove a package and its configuration files
  • clean: Remove downloaded package files
  • autoremove: Remove automatically installed packages that are no longer needed

Now, let's try some basic apt-get commands:

## Update the package index
sudo apt-get update
Example output:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
...

## Install a package
sudo apt-get 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/86.0 kB of archives.
After this operation, 296 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...

## Remove a package
sudo apt-get remove htop
Example output:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  htop
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 296 kB disk space will be freed.
Do you want to continue? [Y/n] Y

In the examples above, we've demonstrated how to update the package index, install a package (htop), and remove a package (htop). The apt-get command provides a straightforward way to manage packages on your Ubuntu system.

Install and Update Packages Using apt-get

In this step, you will learn how to install and update packages using the apt-get command.

First, let's install a new package, tree, which is a command-line tool that displays the directory structure in a tree-like format.

sudo apt-get 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 0 B/56.0 kB of archives.
After this operation, 152 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

Now, let's update all the installed packages on your system to their latest versions.

sudo apt-get upgrade
Example output:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  base-files base-passwd bash bsdutils coreutils dash dbus dbus-user-session dbus-x11 ...
57 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 29.1 MB of archives.
After this operation, 3,772 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

In the example above, we first installed the tree package, and then we upgraded all the installed packages on the system to their latest versions.

Remove Packages and Clean the System with apt-get

In this step, you will learn how to remove packages and clean up your system using the apt-get command.

First, let's remove the tree package that we installed in the previous step:

sudo apt-get 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, 152 kB of disk space will be freed.
Do you want to continue? [Y/n] Y

In the example above, we used the remove operation to uninstall the tree package.

Next, let's clean up the downloaded package files to free up disk space:

sudo apt-get clean
Example output:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

The clean operation removes the downloaded package files from the local repository, freeing up disk space on your system.

Finally, let's remove any packages that are no longer needed, using the autoremove operation:

sudo apt-get autoremove
Example output:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-5.15.0-46 linux-headers-5.15.0-46-generic linux-image-5.15.0-46-generic
  linux-modules-5.15.0-46-generic linux-modules-extra-5.15.0-46-generic
0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded.
After this operation, 321 MB of disk space will be freed.
Do you want to continue? [Y/n] Y

The autoremove operation identifies and removes any packages that were installed automatically as dependencies and are no longer needed.

Summary

In this lab, you learned the basics of the apt-get command, a powerful package management tool in Linux. You explored the general syntax of the apt-get command and the available operations, such as installing, updating, removing, and cleaning packages. You also practiced running common apt-get commands, including updating the package index, installing a package (htop), and removing a package (htop). The apt-get command provides a straightforward way to manage packages on your Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like