How to Use the 'apt' Command for Package Management (with examples)
- Linux
- December 17, 2024
The ‘apt’ command is a powerful package management tool for Debian-based Linux distributions, most notably Ubuntu. This command serves as an efficient, user-friendly way of managing software on your system. It simplifies the installation, updating, and removal of software packages. While earlier versions of Debian and Ubuntu utilized apt-get
for these actions, apt
has become the recommended utility for most interactive use cases starting from Ubuntu 16.04.
This article will explore the primary uses of the apt
command, demonstrating key functionalities with examples.
Update the list of available packages and versions
Code:
sudo apt update
Motivation:
Before installing, upgrading, or removing any software package, it is essential to have the latest information about available software. Running sudo apt update
ensures that your package list is current, which helps in preventing incompatibility issues, security vulnerabilities, and outdated packages.
Explanation:
sudo
: Stands for “superuser do” and allows users to run commands as the superuser or another user. Usingsudo
is necessary here because updating package information requires administrative privileges.apt
: The package management command.update
: Downloads the latest package lists from the repositories to ensure that your package database is up to date.
Example Output:
Hit:1 http://us.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [354 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
...
Fetched 1,234 kB in 2s (615 kB/s)
Reading package lists... Done
Search for a given package
Code:
apt search package
Motivation:
Sometimes, you may need to find out whether a specific software package is available in your repositories. The apt search package
command enables you to search the package database for specific software, along with a brief description.
Explanation:
apt
: Initiates the package manager.search
: Instructsapt
to search through the package lists.package
: Placeholder for the actual package name or search term you want to look up.
Example Output:
Sorting... Done
Full Text Search... Done
example-package/focal 1.0 amd64
Example package for demonstration purposes.
Show information for a package
Code:
apt show package
Motivation:
To make informed decisions about software installation, it’s often useful to have detailed information about a package, including its description, version, dependencies, and repository information. apt show package
offers comprehensive insight into specific packages.
Explanation:
apt
: The command-line utility for package management.show
: Retrieves detailed information about the requested package.package
: The package name you want to inquire about.
Example Output:
Package: example-package
Version: 1.0
Priority: optional
Section: utils
Maintainer: Package Maintainers <maintainer@example.com>
Description: Example package for demonstration purposes
Install a package, or update it to the latest available version
Code:
sudo apt install package
Motivation:
Installing new software or updating existing programs is a frequent requirement for system administrators and users who want to ensure their software remains current. The sudo apt install package
command facilitates easy installation and upgrading of packages.
Explanation:
sudo
: Grants necessary permissions for making system changes.apt
: The package management interface.install
: Directsapt
to add or update a package.package
: Represents the specific software package you wish to install or upgrade.
Example Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
example-package
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 123 kB of archives.
After this operation, 456 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Remove a package
Code:
sudo apt remove package
Motivation:
Over time, unused or obsolete programs clutter your system, consuming valuable disk space and, potentially, resources. Removing such packages using sudo apt remove package
is necessary for keeping your system streamlined and optimized.
Explanation:
sudo
: Essential for executing privileged commands.apt
: Engages the package management system.remove
: Indicates thatapt
should delete the specified package.package
: Refers to the package you wish to eliminate from your system.
Example Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
example-package
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 456 kB disk space will be freed.
Do you want to continue? [Y/n]
Upgrade all installed packages to their newest available versions
Code:
sudo apt upgrade
Motivation:
A crucial aspect of system maintenance is keeping all installed packages up to date to ensure access to the latest features, improvements, and security updates. The sudo apt upgrade
command updates all packages on your system to their latest available versions.
Explanation:
sudo
: Provides the authorization to execute commands requiring root permissions.apt
: The package management tool in action.upgrade
: Executes the process of updating all installed packages to their latest versions based on the updated package lists.
Example Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
example-package
1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 123 kB of archives.
After this operation, 12 kB disk space will be freed.
Do you want to continue? [Y/n]
List all packages
Code:
apt list
Motivation:
Sometimes, it’s valuable to take an inventory of all available packages in your configured repositories. The apt list
command provides a comprehensive list of every package that can be installed, along with their current versions in the repository.
Explanation:
apt
: Engages the package management utility.list
: Lists available packages from the repositories known to the system.
Example Output:
Listing... Done
example-package/focal 1.0 amd64
package2/focal 2.0 amd64
package3/focal,now 3.1 amd64 [installed]
List installed packages
Code:
apt list --installed
Motivation: When managing your system, it’s often necessary to identify the software already installed. This is particularly useful for performing audits, troubleshooting, or simply understanding the software set you currently maintain.
Explanation:
apt
: Leverages the package management tool.list
: Initiates the listing function.--installed
: Filters the list to display only those packages that are currently installed on the system.
Example Output:
Listing... Done
example-package/focal,now 1.0 amd64 [installed]
package3/focal,now 3.1 amd64 [installed]
Conclusion:
The apt
command serves as an essential tool for managing software packages on Debian-based systems. It’s intuitive, flexible, and robust for tasks such as searching, installing, updating, and removing packages. By incorporating these command examples into daily or periodic maintenance routines, users can effectively manage their Linux environments, ensuring systems remain updated, secure, and fully functional.