May 2, 2024 10 min read

apt-get command in Linux with Examples

Explore apt-get command's significance in Linux. The apt-get command is a vital tool in Linux which manages software packages effectively.

apt-get command in Linux
apt-get command in Linux
Table of Contents

Introduction

Before we discuss apt-get command in Linux, let's briefly understand - What is apt-get command ?

The apt-get command is a vital tool in Linux for installing, removing, and managing software packages efficiently. It streamlines the process of software management, ensuring quick updates, upgrades, and removal of applications.

By typing straightforward commands in the terminal, users can easily handle software packages, dependencies, and repositories.

In this tutorial, you will understand apt-get command in Linux. We will also address a few FAQs on  apt-get command in Linux.

How Do I Use the Apt-Get Command in Linux?

Linux users can manage packages with the help of the apt-get command, which has the following basic syntax: [sudo] apt-get [command] [package_name]. It makes it simple for you to manage, update, and upgrade your packages.

Here’s a simple example of how you can use it:

sudo apt-get update
sudo apt-get install package-name

In this example, sudo apt-get update is used to update the package list first. Next, we use sudo apt-get install package-name to install a new package (replace package-name with the name of the package you want to install).

Basic Use of Apt-Get Command

Updating the Package List

The Linux package manager, apt-get, is a valuable tool. To update your package list is one of the simplest uses. To make sure you have the most recent information about the packages available, this is crucial. Here's how to go:

sudo apt-get update
Output

Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Fetched 88.7 kB in 2s (44.3 kB/s)
Reading package lists... Done

To update the package list in this example, use the sudo apt-get update command. The output displays the updates and fetches of the packages from the repository.

Upgrading Packages

You may want to upgrade the installed packages after your package list has been updated. The command sudo apt-get upgrade can be used to accomplish this. Here's how it works:

sudo apt-get upgrade
Output

Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

The sudo apt-get upgrade command in this instance looks for and applies any available upgrades for the installed packages. The process of reading package lists, creating a dependency tree, and figuring out the upgrade is displayed in the output.

Installing New Packages

Installing new packages is another typical application for the apt-get command. The sudo apt-get install package-name command is used to accomplish this. Assume for the moment that we wish to install the popular Linux text editor package nano:

sudo apt-get install nano
Output

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
nano-tiny
Suggested packages:
spell
The following NEW packages will be installed:
nano nano-tiny
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 231 kB of archives.
After this operation, 778 kB of additional disk space will be used.
Do you want to continue? [Y/n]

The nano package is installed in this case using the sudo apt-get install nano command. The output displays the reading of package lists, the building of the dependency tree, and the reading of state data. Additionally, it demonstrates the installation of the nano package and the nano-tiny add-on package.

Advanced Use of the Apt-Get Command

You can begin investigating the apt-get command's more sophisticated features as you grow more accustomed to using it. These consist of updating to a new distribution, clearing out unnecessary packages, and uninstalling packages. Let's first become acquainted with a few of the command-line options and flags that can change how the apt-get command behaves before delving into these subjects.

The following are a few of the most often used apt-get arguments:

  • install: Installs a package

Example:

sudo apt-get install package-name
  • remove: Removes a package

Example:

sudo apt-get remove package-name
  • upgrade: Upgrades all upgradable packages.

Example:

sudo apt-get upgrade
  • update: Updates package list

Example:

sudo apt-get update
  • autoremove: Removes unwanted packages

Example:

sudo apt-get autoremove
  • dist-upgrade: Upgrades the distribution

Example:

sudo apt-get dist-upgrade
  • purge: Removes a package along with its configuration files

Example:

sudo apt-get purge package-name
  • clean: Clears out the local repository of downloaded package files

Example:

sudo apt-get clean
  • -f: Fixes broken dependencies

Example:

sudo apt-get -f install
  • check: Updates the package cache and checks for broken dependencies

Example:

sudo apt-get check

Removing Packages

Removing packages you don't need is one of the most frequent tasks you'll undertake. Here's how to accomplish it:

sudo apt-get remove nano
Output

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
nano
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

In this case, the nano package is being removed using the sudo apt-get remove nano command. The output displays the building of the dependency tree, the reading of package lists, and the removal of the nano package.

Cleaning Up Unused Packages

You may gather unnecessary packages over time that require storage space. You can use the sudo apt-get autoremove command to remove these packages. As an example, consider this:

sudo apt-get autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
nano-tiny
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 191 kB disk space will be freed.

Here, we're removing unnecessary packages with the sudo apt-get autoremove command. The output demonstrates the removal of the nano-tiny package, which was installed as a dependency when we installed nano.

Upgrading to a New Distribution

The sudo apt-get dist-upgrade command can be used to upgrade to a new distribution when one becomes available. Take this as an example:

sudo apt-get dist-upgrade
Output

Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Here, the sudo apt-get dist-upgrade command looks for and installs any updated distributions. The process of reading package lists, creating a dependency tree, and figuring out the upgrade is displayed in the output.

Exploring Alternative Package Management Commands

There are other commands available for managing packages in Linux besides the apt-get command, which is a very useful one. Other package management commands are available, each with a unique combination of features, advantages, and disadvantages. Let's see a few of these options.

The apt Command

The apt command comes first. It's a more recent tool that makes managing Linux packages easier. It condenses the features of multiple commands, such as apt-get, into a single command. An example of how to install a package using the apt command is provided here:

sudo apt install nano
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
nano-tiny
Suggested packages:
spell
The following NEW packages will be installed:
nano nano-tiny
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 231 kB of archives.
After this operation, 778 kB of additional disk space will be used.
Do you want to continue? [Y/n]

In this example, the nano package is installed using the sudo apt install nano command. The apt command performs the same functions as the apt-get command, reading package lists, creating dependency trees, and installing the nano and nano-tiny packages.

The dpkg Command

The dpkg command is the next in line. It is a lower-level tool for managing Debian (.deb) packages directly. This is an illustration of how to install a Debian package using the dpkg command:

sudo dpkg -i package-name.deb
Output

Selecting previously unselected package package-name.
(Reading database ... 178474 files and directories currently installed.)
Preparing to unpack package-name.deb ...
Unpacking package-name (1.0-1) ...
Setting up package-name (1.0-1) ...

To install a Debian package, use the command sudo dpkg -i package-name.deb in this example. Install is what the -i flag represents. The package is seen being ready, unpacked, and set up in the output.

The yum Command

The yum command is the last command. It is the standard package manager for Linux distributions based on Red Hat. Here's an illustration of how to install a package using the yum command:

sudo yum install nano
Output

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package nano.x86_64 0:2.3.1-10.el7 will be installed
--> Finished Dependency Resolution
Installed:
nano.x86_64 0:2.3.1-10.el7
Complete!

In this example, the nano package is installed using the sudo yum install nano command. The output displays the installation of the nano package along with the resolution of package dependencies.

How to Use apt-get Options

Options can be added to the syntax of the apt-get command to modify its behavior.

apt-get [options] [command]

Here is a compilation of frequently utilized apt-get options for your reference.

  • --no-install-recommends – install only the essential dependencies, excluding recommended packages.
  • --install-suggests – include suggested packages as dependencies during the installation process.
  • -d/--download-only – retrieve the packages without installing or extracting them.
  • -f/--fix-broken – try to resolve a system with broken dependencies.
  • -m/--ignore-missing/--fix-missing – proceed with the operation, disregarding any packages that cannot be retrieved.
  • --ignore-hold – disregard any packages that have been placed on hold.
  • --no-download – prevent the downloading of packages.
  • -q/--quiet – suppress progress indicators and generate output suitable for logging purposes.
  • -s/--simulate/--just-print/--dry-run,--recon,--no-act - simulate system changes without actually modifying the system.
  • -y/--yes/--assume-yes – automatically respond with "yes" to prompts.
  • --assume-no - automatically respond with "no" to all prompts.
  • --no-upgrade – prevent the upgrading of packages (typically used with the install command to ensure apt-get does not upgrade if the package is already installed).
  • --only-upgrade – upgrade only the packages that are currently installed.
  • -V/--verbose-versions – displays complete versions of upgraded and installed packages.
  • --show-progress – display a progress bar during the installation, removal, or upgrade of packages.
  • >-b/--compile/--build – compile source packages immediately after downloading them.
  • --reinstall – reinstall currently installed packages to the latest version available.
  • --auto-remove/--autoremove - remove unnecessary packages during the installation of a package.
  • -h/--help – display a concise usage summary.
  • -v/--version – display the version of the apt-get package manager.
  • -c/--config-file – provide an additional configuration file to be read after the default one.
  • -o/--option – set a custom configuration option.
  • --no-show-upgraded – ssuppress the list of upgraded packages that is typically displayed at the end of the upgrade process. (This option only works with the upgrade command, not with install, remove, or purge commands.)
  • --show-progress – display a progress bar for the update command. This option has no effect on other commands like upgrade, install, remove, or purge.

Troubleshooting Common Apt-Get Command Errors

Using the apt-get command can occasionally cause errors, even for the most experienced Linux users. There are a number of potential causes for these errors, including incorrect package names, broken dependencies, and network problems. Let's examine a few of these typical mistakes and how to fix them.

Network Issues

When utilizing the apt-get command, network-related problems are among the most frequent ones. For example, if the package list cannot be retrieved from the repository using the apt-get command, an error message may appear. As an example, consider this:

sudo apt-get update
Output

Err:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Temporary failure resolving 'archive.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.

A network problem is indicated by the error message Temporary failure resolving 'archive.ubuntu.com'. We are unable to resolve the domain name 'archive.ubuntu.com' with the apt-get command. This might be the result of a DNS server issue or a network connectivity problem. You can try adjusting your DNS server or verifying your network connection to fix this issue.

Broken Dependencies

Broken dependencies are another frequent problem. This occurs when an installed or malfunctioning package is required by another package that you are attempting to install. Observe this instance of a mistake:

sudo apt-get install package-name
Output

Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
package-name : Depends: other-package but it is not going to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

The Unmet dependencies error message appears. Try running apt --fix-broken install without any packages, or provide a fix. Shows that although other-package is either broken or not installed, package-name depends on it. You can use the recommended command, apt --fix-broken install, to resolve this issue by fixing the broken dependencies.

Incorrect Package Names

Occasionally, you may receive an error message stating that the package name you have entered is invalid. As an example, consider this:

sudo apt-get install wrong-package-name
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package wrong-package-name

The package wrong-package-name does not exist, as indicated by the error message E: Unable to locate package wrong-package-name. You can look up the correct package name or double-check the spelling of the package name to find the solution to this issue.

FAQs on apt-get command in Linux

How can apt-get help with software management on Linux systems? 

Apt-get simplifies software management by assisting in the installation, upgrade, and removal of packages. It streamlines the process, ensuring quick and easy handling of software.

Is it possible to install a specific version of a package using apt-get? 

Users can install a particular package version with apt-get by specifying the desired version number alongside the package name, ensuring precise installation.

Why is apt-get considered a crucial tool for software management on Linux? 

Apt-get is essential on Linux for its ability to simplify and streamline software package installation, upgrade, and removal processes, ensuring efficient management.

How does apt-get interact with software repositories on Linux distributions?

Apt-get interacts with software repositories by fetching updated package information, enabling users to access the latest versions of software packages available for installation or upgrade.

What are the advantages of using apt-get over manual software installation methods on Linux?

 Apt-get offers advantages such as automated dependency resolution, centralized package management, and simplified software updates, making it more efficient than manual installation methods.

Can apt-get be used to downgrade software packages on Linux?

With apt-get, users can downgrade software packages to previous versions by specifying the desired version number during the installation or upgrade process.

What are some common troubleshooting steps when encountering errors with apt-get on Linux? 

Common troubleshooting steps when facing errors with apt-get include updating package lists, checking network connectivity, verifying repository configurations, and resolving package conflicts.

Conclusion

We hope this tutorial helped you understand how to use apt-get command on Linux.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.