How to use apt Package Manager on Ubuntu Command Line

When you start using the Ubuntu operating system, the graphical Ubuntu application manager is your first choice for installing software. Although this collection contains many applications, you will quickly notice that it lacks many powerful tools, especially those for the command line. Then it's time to switch to apt-get, a more robust software installation tool. With this tool, you can not only install software from the command line but also:

  • Update the package repositories
  • Update installed software
  • Search for available packages
  • Get the source code for an installed package
  • Reinstall a software package
  • Remove software from your system

In this tutorial, I'll show you how to use the apt-get tool to complete the abovementioned tasks.

This article is compatible with all recent Ubuntu versions incl. Ubuntu 20.04 and Ubuntu 22.04. We'll use the Ubuntu Terminal to run apt-get because it's a command-line utility. The Terminal may be accessed using the system Dash or the Ctrl+alt+T shortcut.

Update the Package Repositories with apt

A repository is a catalog of software programs available at a particular time. Because the packages in this list are added, deleted, and changed regularly, you should keep your system repositories up to date. This gives you an up-to-date list of all software packages available in the repository.

Before you install new software, you should update the repositories by running the following command as root:

$ sudo apt update

Ubuntu package list update

Now your system repositories are in-line with those on the Internet.

Update Installed Software with apt

Although you can use the update manager to update installed software on your system, the apt-get utility also provides a way to do the same. Use the following command as root to upgrade software on your system:

$ sudo apt upgrade

Doing an Ubuntu package upgrade

Now the software on your system is up to date.

Search for Available Packages with apt

You can use the apt-cache utility to search for available packages from the Internet repositories. Use the following syntax to do so:

$ sudo apt-cache search [package name keyword]

Example:

Let us search for the stable version of the Firefox browser through the following apt-cache command:

$ sudo apt-cache search "firefox"

The following output shows the availability of the Firefox package.

Ubuntu search for packages

You can get detailed information about the package through the following apt-cache command:

Syntax:

$ sudo apt-cache show "package-name"

Example:

$ sudo apt-cache show "firefox"

The following output shows details about the ‘firefox’ package:

Show details about a .deb package

Install a Package with apt

Here comes the most famous use of the apt-get command; installing software from an updated repository.

Use the following command as root to install a software package:

$ sudo apt install “package-name”

Example:

As an example, you can install the PHP programming language by using this apt command:

$ sudo apt install php

Install Ubuntu software with apt

The system prompts you with a y/n solution before installing a program through the apt-get utility. Always enter Y to proceed with the installation.

Get the Source Code for a Ubuntu Software Package with apt

If you want the source code for a package, you can use the following command:

Syntax:

$ sudo apt source “package-name”

Example:

For example, if I want to get the source code of the firefox package, I will use the following command:

$ sudo apt source firefox

If you get this error after running the above command:

E: You must put some 'source' URIs in your sources.list

E: You must put some 'source' URIs in your sources.list

You need to remove the commenting from the deb-src line for the respective package from the sources.list file. This file is located in /etc/apt/ folder.

You can open the sources file in the nano editor through the following command:

$ sudo nano /etc/apt/sources.list

sources.list

Press Ctrl+X and then Y to exit and then save the changes. Then run the "apt update" command.

Reinstall a Software Package with apt

While running an application, it might stop working or go corrupt. In that case, you can easily reinstall that application through the apt command as follows:

$ sudo apt install “package-name” --reinstall

Example:

$ sudo apt-get install php --reinstall

This command will re-install the PHP package already installed on my system.

Reinstall packages using apt

Find which packages are installed

You can use the apt list command to list installed packages:

apt list --installed

A more in-depth guide on this topic can be found here.

Remove a Software from Your System

When you want to remove software from your system, you can use the following apt-get command:

$ sudo apt remove “package-name”

Example:

$ sudo apt remove php

This command will remove the PHP software from my system

Remove software with apt

Remove Complete Software Configurations

The apt remove command removes the software from your system but does not remove the configuration files that might have been installed with it. The following command will completely remove those configuration files for software:

$ sudo apt-get purge “package-name”

Example:

$ sudo apt purge php

This command will completely remove PHP, including its config files, from my system.

Apt purge

When uninstalling or deleting software, the system gives you the option to answer Y/N to the question. Always select Y to proceed with the program removal.

We have shown in this article how you can use the apt-get command not only to install software on your system but also to perform all installation-related activities. You no longer have to rely solely on the Ubuntu Software Manager to install apps on your computer.