The apt command is an effective command line tool for managing packages in Ubuntu and other Linux based systems. In this article, we will discuss the basic apt commands that you can use to install, upgrade, update or remove Ubuntu packages, search installed packages, find dependencies, view information about installed packages, and more.
Basically, the apt command is which administrators can use to perform some of the functions available in the older apt-get and apt-cache commands. However, apt is a simpler command line that delivers several benefits over the other package management tools. It is easy to use, and admins can also include in system administration scripts.
In order to run the apt commands, you need to access the Ubuntu server remotely using a secure shell terminal (SSH).
Managing packages in Ubuntu
Once you access your Ubuntu server through SSH, you can now start managing the packages. The commonly used commands include install, remove, update, upgrade, search, list, show, and others.
Some of these are used to perform one task at a time, while others have options to do multiple functions. For example, you can use apt to install or remove with multiple packages instead of just one.
The general syntax for apt is this:
$ sudo apt [option] [pkg1] [pkg2] ... ,
In the above expression, the option is the action, such as install, remove, search or show, while the pkg represents the package to install, remove, upgrade, etc.
To learn how to use the apt command, below are the common practical applications.
Installing a Package
The apt command tool allows you to install one or more packages in the Ubuntu system. Type and run the install command in the following syntax;
$ sudo apt install [package name]
For instance, if installing apache2:
$ sudo apt install apache2
By default, the system will prompt you to confirm if you want to continue installing the specified package or not. Type Y and enter to continue or N if you do not want to install the package.
However, you can override this by adding a –y option after the apt. Any install and even remove will happen without requiring any confirmation.
$ sudo apt -y install apache2
Install multiple applications
The apt command can be used to install multiple applications at a go. To do these separate the different package names with a space between them.
Syntax sudo apt install
<package1> <package2> <package3>
$ sudo apt -y install apache2 vim
This command will automatically install apache2 and Mplayer without prompting for any additional prompts, such as a confirmation.
Remove One or More Packages
If you want to remove a package, use the apt in the format; sudo apt remove [package name].
If you want to remove the apache2 package:
$ sudo apt remove apache2 vim
This will only remove one application. To remove multiple packages, use the same command while separating the package names with a space:
sudo apt remove apache2 vim
If you want to remove a package without confirming add –y between apt and remove words.
$ sudo apt -y remove apache2
Remove files and configuration
You can use purge to remove the package files and configurations from the system. Use use apt purge followed by the package name:
$ sudo apt purge apache2
Autoclean to Remove Old Downloaded Archive Files
$ sudo apt autoclean
Search For a package
You can search for a package with the "search" option:
$ sudo apt search apache2
Updating the Repository Index or package database
This updates or refreshes the Repository Index file or database of all installed applications as defined in/etc/apt/sources.list or /etc/apt/preferences. It checks if there are new updates and then compares with installed packages. It then updates the repository with newer releases.
$ sudo apt update
After running the command, the system may show results in three categories.
- Hit: if there is no need for an update, such as when the packages are of the same version.
- Ign: package version is more recent hence ignored.
- Get: when there is a new version. The system then downloads the information about the new version. However, it does not download the package itself.
Perform this to upgrade installed software with the latest versions available in the package repositories. However, it requires that you first update the package index as above.
To upgrade the packages, use the command
$ sudo apt upgrade
Upgrade just one program
When you do not want to upgrade all the programs, simply specify the particular package.
$ sudo apt upgrade apache2
Install a package without upgrading
Most often, when you install a newer version, it upgrades the package automatically. However, you can choose not to upgrade by adding –no-upgrade option.
$ sudo apt install vim --no-upgrade
Use the apt command to display all the packages, installed packages, upgradable packages etc.
To list installed packages:
$ sudo apt list --installed
To list upgradable packages
$ sudo apt list --upgradable
View package information
To view package information such as version, size, origin, and a description of what it does, use the command;
$ sudo apt show apache2
This shows raw dependency information:
$ sudo apt depends apache2
Display policy settings:
$ sudo apt policy
Getting Help with the apt Command
To get help about the apt command usage, use,
$ sudo apt help
The system keeps a log file of the apt activities such as the installs, removals, updates and others that you have performed.
The log file is available on the path /var/log/dpkg.log .
The apt a command line interface is a simple, yet powerful tool that helps administrators in managing packages in Ubuntu and other Linux based systems (click here for the top Linux hosting companies). The package management tool allows the admins to easily and quickly perform tasks such as installing, upgrading, updating or removing software as well as keeping track of the installed packages.
To use the tool, you need to access your server, usually through SSH which is available via the inbuilt terminal applications in Linux and MAC systems, or through a client tool such as puTTY on a Windows client (click here for the best Windows hosting services).
Check out these top 3 Linux hosting services