apt
apt is the standard package management tool for Debian- and Ubuntu-based Linux distributions. Use apt update to refresh the package list, apt install to install software, apt upgrade to update the entire system, and apt remove / apt purge to uninstall packages. By adding third-party PPA repositories, you can also manage packages that are not available in the official repositories.
Syntax
Refreshes the repository package list. Always run this before installing or upgrading packages.
sudo apt update
Installs the specified package(s). Multiple packages can be specified separated by spaces.
sudo apt install {package} [{package} ...]
Installs a specific version of a package.
sudo apt install {package}={version}
Updates all installed packages to the latest version. Adds new dependency packages as needed, but does not remove existing packages.
sudo apt upgrade
Updates the entire system, removing existing packages if necessary. Use this for major changes such as kernel updates.
sudo apt full-upgrade
Removes the package (configuration files are kept). Use purge to remove the package along with its configuration files.
sudo apt remove {package}
sudo apt purge {package}
Removes dependency packages that are no longer needed.
sudo apt autoremove
Searches for packages by keyword. Use show to display detailed information such as version and dependencies.
apt search {keyword}
apt show {package}
Lists all installed packages, or lists packages that can be upgraded.
apt list --installed apt list --upgradable
Adds an Ubuntu PPA (Personal Package Archive). Run apt update after adding to refresh the package list.
sudo add-apt-repository ppa:{username}/{repository}
sudo apt update
Command Reference
| Operation | Command | Description |
|---|---|---|
| Update package list | apt update | Refreshes the repository package list. Always run this before installing or upgrading packages. |
| Install a package | apt install {package} | Installs the specified package. Dependencies are resolved automatically. |
| Upgrade all packages | apt upgrade | Updates all installed packages to the latest version. Does not remove existing packages. |
| Full upgrade | apt full-upgrade | Updates the entire system, allowing removal of existing packages if necessary. Used for kernel updates and similar changes. |
| Remove a package | apt remove {package} | Removes the package. Configuration files remain on the system. |
| Remove with config files | apt purge {package} | Removes the package along with its configuration files. Use this for a complete reset. |
| Remove unused dependencies | apt autoremove | Removes dependency packages that are no longer needed. Running this after remove or purge frees up disk space. |
| Search for a package | apt search {keyword} | Searches package names and descriptions by keyword. |
| Show package details | apt show {package} | Displays detailed information including version, dependencies, and description. |
| List installed packages | apt list --installed | Lists all packages currently installed on the system. |
| List upgradable packages | apt list --upgradable | Shows all packages that can be upgraded after running apt update. |
| Install a specific version | apt install {package}={version} | Installs a pinned version of a package. Useful when you need to keep a version that has been tested and verified. |
| Add a PPA repository | add-apt-repository ppa:{user}/{repo} | Adds an Ubuntu PPA. Allows you to use software versions not available in the official repositories. |
| Remove a PPA repository | add-apt-repository --remove ppa:{user}/{repo} | Removes an added PPA. Run apt update after removing to refresh the package list. |
Examples
Update the system to the latest state
Refresh the package list, then update all installed packages. You can also check which packages are upgradable beforehand.
$ sudo apt update $ sudo apt upgrade $ apt list --upgradable Listing... Done bash/jammy-updates 5.1-6ubuntu1.1 amd64 [upgradable from: 5.1-6ubuntu1] libc6/jammy-updates 2.35-0ubuntu3.8 amd64 [upgradable from: 2.35-0ubuntu3.7] openssl/jammy-updates 3.0.2-0ubuntu1.18 amd64 [upgradable from: 3.0.2-0ubuntu1.17]
Install Nginx and verify it works
Update the package list, install Nginx, then verify the installed version.
$ sudo apt update $ sudo apt install nginx $ nginx -v nginx version: nginx/1.24.0 (Ubuntu)
Add a PHP PPA and install the latest version
Adding the PPA by Ondřej Surý makes newer PHP versions available than those in the official Ubuntu repository. software-properties-common is required because it provides the add-apt-repository command.
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php8.3 php8.3-fpm php8.3-mbstring php8.3-xml php8.3-curl
$ php8.3 --version
PHP 8.3.6 (cli) (built: Apr 15 2024 18:34:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
Cleanly remove an unnecessary package
Remove apache2 along with its configuration files, then clean up dependencies and cached package files. apt clean deletes .deb files under /var/cache/apt/archives/.
$ sudo apt purge apache2 $ sudo apt autoremove $ sudo apt clean $ apt list --installed | grep apache2 (No output means the removal was successful)
Install a specific version of a package
Check available versions first, then install the package at a pinned version.
$ apt-cache policy nginx
nginx:
Installed: 1.24.0-1ubuntu1
Candidate: 1.24.0-1ubuntu1
Version table:
*** 1.24.0-1ubuntu1 500
500 http://jp.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
1.18.0-6ubuntu14 500
500 http://jp.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
$ sudo apt install nginx=1.18.0-6ubuntu14
$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
Overview
apt is a high-level package management tool for Debian- and Ubuntu-based Linux systems. It sits on top of the lower-level tool dpkg, and handles dependency resolution, downloading from repositories, and installation all in one step. Making a habit of running apt update to refresh the package list before apt install helps prevent dependency conflicts. After uninstalling, running apt autoremove and apt clean removes leftover files and keeps the disk tidy. When you need a newer version of software not available in the official repositories (such as the latest PHP), you can add a PPA — but always verify that the source is trustworthy before doing so. For package management on RHEL- and AlmaLinux-based systems, see the dnf / yum page.
If you find any errors or copyright issues, please contact us.