How to use the command apt (with examples)
- Linux
- December 25, 2023
The apt
command is a package management utility for Debian-based distributions. It is the recommended replacement for apt-get
when used interactively in Ubuntu versions 16.04 and later. It provides a way to search, install, update, and remove packages in the system.
Use case 1: Update the list of available packages and versions
Code:
sudo apt update
Motivation: Running sudo apt update
is recommended before executing other apt
commands. It updates the package lists for upgrades and new package installations, ensuring that the latest versions are available.
Explanation:
sudo
: The command is run with superuser privileges.apt
: The package management command.update
: The argument to update the package lists.
Example output:
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
Fetched 316 kB in 1s (423 kB/s)
Reading package lists... Done
Use case 2: Search for a given package
Code:
apt search package
Motivation: When you need to find a package related to a specific software or library, you can use the apt search
command. It searches for the package name, description, and other related information.
Explanation:
apt
: The package management command.search
: The argument to search for a specific package.package
: The name or keyword used to search for a package.
Example output:
Sorting... Done
Full Text Search... Done
example-package/focal 1.0.0-1 amd64
Example package description
Use case 3: Show information for a package
Code:
apt show package
Motivation: Sometimes, you need detailed information about a package, such as its version, maintainer, dependencies, and description. The apt show
command provides this information.
Explanation:
apt
: The package management command.show
: The argument to display information for a package.package
: The name of the package to show information about.
Example output:
Package: example-package
Version: 1.0.0-1
Priority: optional
Section: utils
Maintainer: John Doe <johndoe@example.com>
Installed-Size: 1234
Depends: libc6 (>= 2.31)
Homepage: https://example.com
Description: Example package description
Use case 4: Install a package, or update it to the latest available version
Code:
sudo apt install package
Motivation: Installing packages is a common task in Linux systems. The apt install
command allows you to install a specific package. It also updates the package to the latest available version if it is already installed.
Explanation:
sudo
: The command is run with superuser privileges.apt
: The package management command.install
: The argument to install a package.package
: The name of the package to install or update.
Example output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libexample-dev libexample1
The following NEW packages will be installed:
libexample-dev libexample1 package
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1234 kB of archives.
After this operation, 5678 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Use case 5: Remove a package
Code:
sudo apt remove package
Motivation: When you no longer need a package installed in your system, you can use the apt remove
command to remove it. This keeps the system clean and frees up disk space. Adding purge
instead of remove
removes configuration files associated with the package.
Explanation:
sudo
: The command is run with superuser privileges.apt
: The package management command.remove
: The argument to remove a package.package
: The name of the package to remove.
Example output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
package
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 1234 kB disk space will be freed.
Do you want to continue? [Y/n]
Use case 6: Upgrade all installed packages to their newest available versions
Code:
sudo apt upgrade
Motivation: Regularly upgrading installed packages ensures that you have the latest bug fixes and security updates. The apt upgrade
command upgrades all installed packages to their newest available versions.
Explanation:
sudo
: The command is run with superuser privileges.apt
: The package management command.upgrade
: The argument to upgrade installed packages.
Example output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
package1 package2 package3
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 5678 kB/12345 kB of archives.
After this operation, 12.3 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Use case 7: List all packages
Code:
apt list
Motivation: The apt list
command provides a list of all available packages in the system. It can be useful when you want to browse the complete package repository and search for specific packages.
Explanation:
apt
: The package management command.list
: The argument to list all packages.
Example output:
Listing...
package1/focal 1.0.0-1 amd64
package2/focal 2.0.0-1 amd64
package3/focal 3.0.0-1 amd64
...
Use case 8: List installed packages
Code:
apt list --installed
Motivation: Sometimes you need to check which packages are currently installed on your system. The apt list --installed
command lists all installed packages, making it easy to review or verify the installed software.
Explanation:
apt
: The package management command.list
: The argument to list packages.--installed
: The option to filter for installed packages only.
Example output:
Listing...
package1/focal 1.0.0-1 amd64
package2/focal 2.0.0-1 amd64
package3/focal 3.0.0-1 amd64
...
Conclusion:
The apt
command is a powerful package management utility for Debian-based distributions. By learning how to use the different use cases of apt
, you can effectively search, install, update, and remove packages in your system. Remember to always run sudo apt update
before executing other apt
commands to ensure you have the latest package information.