apt-get Command in Linux: Understanding with Examples

Deepsandhya Shukla Last Updated : 14 Jan, 2024
5 min read

Introduction

The apt-get command is a powerful tool in Linux that allows users to manage software packages. Whether you want to install, update, remove, or search for packages, apt-get provides a simple and efficient way to handle these tasks. In this article, we will explore the various functionalities of apt-get and provide examples to help you understand its usage.

apt-get Command in Linux

Understanding apt-get Command in Linux

What is the apt-get Command?

The apt-get command is a package management command-line tool used in Debian-based Linux distributions, such as Ubuntu. It handles software packages, including installation, updating, removal, and searching. With apt-get, you can easily manage your system’s software packages and dependencies.

Benefits of Using apt-get Command

Using apt-get offers several advantages.

Firstly, it simplifies installing, updating, and removing packages, making it user-friendly.

Additionally, apt-get automatically resolves dependencies, ensuring all required packages are installed correctly. It also provides a centralized repository system, allowing you to search for and install packages from a vast collection easily.

Basic Syntax of apt-get Command

The basic syntax of apt-get command is as follows:

sudo apt-get [options] [command] [package_name]

The `sudo` command is used to run apt-get with administrative privileges. The `options` are additional flags that modify the command’s behavior. The `command` specifies the action to perform, such as installing, updating, removing, or searching. Finally, the `package_name` refers to the name of the package you want to act on.

apt-get Command

Installing Packages with apt-get

Installing a Single Package

To install a single package using apt-get, use the following command:

sudo apt-get install package_name

For example, to install the package “firefox,” you would run:

sudo apt-get install firefox

Installing Multiple Packages

To install multiple packages simultaneously, you can list them after the `install` command, separated by spaces. For instance:

sudo apt-get install package1 package2 package3

Installing Specific Versions of Packages

If you need to install a specific version of a package, you can specify it using the following syntax:

sudo apt-get install package_name=version

For example, to install version 2.4.0 of the package “apache2,” you would run:

sudo apt-get install apache2=2.4.0

Installing Packages from a Specific Repository

To install packages from a specific repository, you can use the `-t` option followed by the repository name. For example:

sudo apt-get install -t repository_name package_name

Installing Packages with Dependencies

The apt-get automatically resolves and installs dependencies for packages. You don’t need to worry about manually installing each dependency. Run the install command, and apt-get will handle the rest.

Updating Packages with apt-get

Updating All Installed Packages

To update all installed packages on your system, use the following command:

sudo apt-get update
sudo apt-get upgrade

The first command updates the package lists, while the second upgrades the installed packages to their latest versions.

Updating a Specific Package

If you want to update a specific package, use the following command:

sudo apt-get install --only-upgrade package_name

Replace `package_name` with the package name you want to update.

Updating Packages with Dependencies

When you update packages, apt-get automatically handles dependencies. It ensures that all required packages are updated along with the main package.

Removing Packages with apt-get

Removing a Single Package

To remove a single package, use the following command:

sudo apt-get remove package_name

For example, to remove the package “apache2,” you would run:

sudo apt-get remove apache2

Removing Multiple Packages

To remove multiple packages simultaneously, list them after the `remove` command, separated by spaces.

sudo apt-get remove package1 package2 package3

Removing Packages with Dependencies

apt-get automatically handles dependencies when removing packages. It ensures that any packages are removed depending on the one being removed.

Searching for Packages with apt-get

Searching for a Specific Package

To search for a specific package, use the following command:

apt-cache search package_name

Replace `package_name` with the package name you want to search for.

Searching for Packages by Keyword

To search for packages using a keyword, use the following command:

apt-cache search keyword

Replace `keyword` with the term you want to search for.

Searching for Packages by Description

To search for packages based on their description, use the following command:

apt-cache search --names-only description_keyword

Replace `description_keyword` with the keyword present in the package description.

Managing Repositories with apt-get

Adding a Repository

To add a repository, you need to edit the `/etc/apt/sources.list` file or create a new file in the `/etc/apt/sources.list.d/` directory. After adding the repository, run the following command to update the package lists:

sudo apt-get update

Removing a Repository

Delete the corresponding file from the `/etc/apt/sources.list.d/` directory to remove a repository. Then, update the package lists using the `update` command.

Updating Repository Information

To update the information about available packages from all enabled repositories, use the following command:

sudo apt-get update

Upgrading the System with apt-get

Upgrading to the Latest Version of Ubuntu

To upgrade your Ubuntu system to the latest version, use the following command:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo do-release-upgrade

The `dist-upgrade` command performs a distribution upgrade, which handles changes in dependencies between packages.

The do-release-upgrade command is designed to upgrade the Ubuntu distribution to a new version. It will prompt you for confirmation before proceeding with the upgrade. 

Upgrading to a Specific Ubuntu Version

To upgrade to a specific version of Ubuntu, use the following command:

sudo apt-get install update-manager-core
sudo do-release-upgrade

Replace `update-manager-core` with the appropriate package name for your Ubuntu version.

Cleaning Up with apt-get

Removing Unused Packages

To remove unused packages from your system, use the following command:

sudo apt-get autoremove

This command removes packages automatically installed as dependencies but no longer needed.

Clearing the Package Cache

To clear the package cache and free up disk space, use the following command:

sudo apt-get clean

This command removes all downloaded package files from the cache.

Conclusion

The apt-get command is a versatile tool for managing software packages in Linux. Whether you need to install, update, remove, or search for packages, apt-get provides a straightforward and efficient way to handle these tasks. By understanding the basic syntax and various options available, you can easily navigate the world of package management in Linux. So go ahead, explore apt-get, and take full control of your system’s software packages.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details