APT Command Tutorial: Install, Remove, Update Packages (Ubuntu & Debian)
The apt
command is the main tool used to manage packages in Ubuntu and Debian-based Linux systems. In this tutorial, we’ll go over the basic commands you can use to search, install, remove, and update packages.
Listing Installed Packages
To list all available packages (both installed and not installed), run:
apt list
To only see installed packages:
apt list --installed
To check if a specific package is installed, you can filter the list using the grep command. For example, this command finds packages with 'ssh' in their name:
apt list --installed | grep ssh
Another way to list installed packages is by using the dpkg -l
command:
dpkg -l
Searching for Packages to Install
You can use apt search
to look for packages by keyword. For example, apt search apache
will list all packages related to "apache."
apt search apache
NOTE: Before searching, it's good practice to update your local package index using the apt update
command.
Sometimes, apt search
can provide a very long output, and its multi-line format can make filtering with grep
challenging. For more precise searches, especially when knowing specific terms, apt-cache search
is often preferred as it presents results in a more grep-friendly single-line format.
Examples
To find Apache-related packages, you can run:
apt-cache search apache
Filter the output of the above command to find the lines that have 'server' in the package name or description:
apt-cache search apache | grep -i server
Installing Packages
Once you know the name of the package you want to install, use the apt install
command to install it:
sudo apt install package-name
By default it will ask for a confirmation before installing a package. To skip the confirmation add -y
option:
sudo apt install package-name -y
Example: Installing the Apache HTTP Server
First, it's always a good idea to update your local package index to ensure you have the latest information about available packages:
sudo apt update
Then install the apache2
package, which provides the Apache web server on Ubuntu:
sudo apt install apache2
Uninstalling Packages
To uninstall a package you can use either apt remove
or apt purge
commands.
apt remove
uninstalls the package but keeps its configuration files.apt purge
removes the package along with its configuration files.
If you want to completely remove a package, use apt purge
:
sudo apt purge package-name
After uninstalling a package, also run the apt autoremove
command to remove any dependency packages that are no longer needed:
sudo apt autoremove
Example: Uninstalling the Apache web server:
sudo apt purge apache2
sudo apt autoremove
Updating Packages
To see which packages can be upgraded:
apt list --upgradable
To update all packages on your system, first refresh your package list, then run the apt upgrade
command:
sudo apt update
sudo apt upgrade
The apt upgrade
command updates all installed packages to their latest versions. If you only want to upgrade a single package, you can do so using the apt install
command with the --only-upgrade
option:
sudo apt install --only-upgrade package-name
Hold a package from being upgraded
To prevent a package from being upgraded, use the apt-mark hold
command:
sudo apt-mark hold package-name
To allow the package to be upgraded again, use the apt-mark unhold
command:
sudo apt-mark unhold package-name
Conclusion
That’s a quick overview of using the apt
command to manage packages on Debian and Ubuntu systems. As a final note, you can find the configuration files for apt repositories in the /etc/apt/sources.list.d/
directory.