Ubuntu

How to Use APT Package Manager in Ubuntu 20.04 LTS

APT is the default package manager on Ubuntu/Debian based Linux distributions. In this article, I am going to show you how to use the APT package manager on Ubuntu 20.04 LTS. So, let’s get started.

Ubuntu Package Repositories:

Ubuntu package repositories are basically file servers where all the deb packages are kept.

Ubuntu has 4 main official package repositories: main, restricted, universe, and multiverse.

In main repository, Ubuntu supported free and open source softwares are kept.

In restricted repository, proprietary drivers (i.e. NVIDIA) are kept.

In universe repository, free and open-source softwares that are maintained by the community are kept.

In multiverse repository, softwares that have copyright restriction or legal issues are kept.

Ubuntu also has an extra package repository called partner. In partner repository, some proprietary and closed-source softwares are kept.

There are other third-party package repositories and PPAs which you can add on Ubuntu to install your desired software packages as well.

Ubuntu Package Repository Configuration Files:

Ubuntu package repository information is stored in the /etc/apt/sources.list file. Third-party PPAs and other repositories are stored as .list files in the /etc/apt/sources.list.d/ directory. There configuration files are plain text files. So, you can easily use a text editor to manage them.

The contents of the /etc/apt/sources.list file is shown in the screenshot below.

$ cat /etc/apt/sources.list

Here, each line starting with deb or deb-src is an APT package repository line. The lines starting with # (hash) are comments. They describe what the repository is for.

The APT package repository line has the following format:

<type> <url> <release-name> <repository-name>

Here, <type> can be deb or deb-src.

deb is used to define repositories that contain compiled packages.

deb-src is used to define repositories that contain source codes of the packages which must be compiled before use.

deb-src is mainly used for development purposes. If you are not a software developer or don’t have any idea what this is, then just use deb.

<url> is the location (i.e. http://us.archive.ubuntu.com/ubuntu/) where the packages are hosted. The APT package manager will download package information and package files from this location.

<release-name> is the code name of the Ubuntu version you’re running.

You can find the code name of your Ubuntu machine with the following command:

$ lsb_release -cs

In case of Ubuntu 20.04 LTS, the <release-name> is focal.

There are also updates <release-name> (i.e. focal-updates) for the Ubuntu official package repositories which keeps all the incremental updates of packages after the release of an Ubuntu version.

The <repository-name> is the name of the package repository. In case of the official Ubuntu package repositories, the <repository-name> is main, restricted, universe, and multiverse.

You can put one or more <repository-name> in the same APT package repository line separated by spaces (i.e. main restricted universe).

Here is an example of an APT package repository line:

deb http://us.archive.ubuntu.com/ubuntu/ focal main restricted

Listing Enabled Package Repositories of Ubuntu:

You can list all the enabled package repositories of your Ubuntu operating system with the following command:

$ sudo egrep -h -v '(^#)|(^$)' /etc/apt/sources.list $(ls
/etc/apt/sources.list.d/*.list 2> /dev/null)

As you can see, all the enabled package repositories are displayed.

Managing Official Package Repositories:

By default, the Ubuntu official main, restricted, universe and multiverse package repositories are enabled. The partner repository is not enabled.

You can enable an Ubuntu official package repository (let’s say, restricted) with the following command:

$ sudo apt-add-repository restricted

As you can see, the restricted repository is already enabled. If it is disabled for some reason, it will be enabled.

You can disable or remove the official restricted repository as follows:

$ sudo apt-add-repository --remove restricted

As you can see, the restricted repository is disabled.

Adding and Removing Ubuntu partner Repository:

You can add the Ubuntu partner repository with the following command:

$ sudo apt-add-repository "deb http://archive.canonical.com/ubuntu
 $(lsb_release -cs) partner"

The Ubuntu partner repository should be enabled.

As you can see, Ubuntu partner repository is enabled.

$ sudo egrep -h -v '(^#)|(^$)' /etc/apt/sources.list $(ls /etc/apt/sources.list.d/*.list
 2> /dev/null)

You can also remove the Ubuntu partner repository with the following command:

$ sudo apt-add-repository --remove "deb http://archive.canonical.com/ubuntu
$(lsb_release -cs) partner"

Managing Third-Party Package Repositories:

If the Ubuntu official package repositories do not have the software/tool you’re looking for, you can always add third-party package repositories and PPAs on Ubuntu and install your desired software/tool from there.

A good source of third-party Ubuntu PPAs is launchpad. Visit the Launchpad Ubuntu PPA page and search for the software/tool (i.e. codeblocks) you’re looking for there.

From the search results, you can find the third-party PPA repository of your desired software/tool. Click on the PPA link that you like. I have picked Codeblocks Daily Builds PPA for demonstration.

The PPA should have instructions on how to install things from the PPA as you can see in the screenshot below. Just follow the PPA specific installation instructions.

For example, Codeblocks Daily Builds PPA tells you to run 3 commands to install Codeblocks Daily build.

$ sudo add-apt-repository ppa:codeblocks-devs/daily
$ sudo apt-get update
$ sudo apt-get install codeblocks codeblocks-contrib

Here, the following command is used to add the PPA to your Ubuntu operating system:

$ sudo add-apt-repository ppa:codeblocks-devs/daily

To confirm, press <Enter>.

The PPA should be added and also the APT package repository cache should be updated.

In my case, the PPA won’t work as it does not support Ubuntu 20.04 LTS yet. But it’s added.

As you can see, the PPA repository was added to my Ubuntu machine.

$ sudo egrep -h -v '(^#)|(^$)' /etc/apt/sources.list $(ls
/etc/apt/sources.list.d/*.list 2> /dev/null)

If you want, you can remove the PPA with the following command:

$ sudo add-apt-repository --remove ppa:codeblocks-devs/daily

To confirm the remove operation, press <Enter>.

The PPA should be removed. As you can see, the PPA is no more in the /etc/apt/sources.list file or in /etc/apt/sources.list.d/ directory.

$ sudo egrep -h -v '(^#)|(^$)' /etc/apt/sources.list $(ls
/etc/apt/sources.list.d/*.list 2> /dev/null)

Some third-party software (i.e. Docker, MongoDB) may provide the APT package repository line which you can add on Ubuntu to install it.

For example, Docker’s official APT package repository line may look as follows:

deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable

Here, focal is the <release-name> of Ubuntu 20.04 LTS.

To add this repository, run the following command:

$ sudo apt-add-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu
 $(lsb_release -cs) stable"

Here, $(lsb_release -cs) will be replaced with the <release-name> of your Ubuntu version (i.e. focal for Ubuntu 20.04 LTS).

The Docker official package repository should be added.

Ignore the errors. Docker does not support Ubuntu 20.04 LTS yet. I have only used it for the demonstration on how to add/remove third-party package repositories on Ubuntu.

As you can see, the Docker package repository is added.

$ sudo egrep -h -v '(^#)|(^$)' /etc/apt/sources.list $(ls
/etc/apt/sources.list.d/*.list 2> /dev/null)

To remove a third-party package repository, list all the enabled package repositories with the following command:

$ sudo egrep -h -v '(^#)|(^$)' /etc/apt/sources.list $(ls
/etc/apt/sources.list.d/*.list 2> /dev/null)

Then, copy the APT package repository line which you want to remove from the list.

Then, remove the third-part package repository as follows:

$ sudo apt-add-repository --remove "deb [arch=amd64] https://download.docker.com
/linux/ubuntu focal stable"

As you can see, the third-party package repository is not in the enabled package repository list anymore.

$ sudo egrep -h -v '(^#)|(^$)' /etc/apt/sources.list $(ls
/etc/apt/sources.list.d/*.list 2> /dev/null)

Updating Package Repository Cache:

Once you have added new APT package repositories, run the following command to update the APT package repository cache:

$ sudo apt update

The APT package repository cache should be updated.

List Installed Packages:

You can list all the packages that are installed on your Ubuntu machine with the following command:

$ sudo apt list --installed

All the installed packages (i.e. acl, acpi-support), the package version (i.e. 2.2.53-6, 0.143), the package architecture (i.e. amd64) should be listed as you can see in the screenshot below. The list is very long.

Listing Upgradable Packages:

If you want to list the packages which are installed on your Ubuntu machine but a newer version (update) is available in the package repository, run the following command:

$ sudo apt list --upgradable

As you can see, all the packages that have updates (newer version) available are listed. I will show you how to install these updates in a later section of this article.

Searching for Packages:

You can search for packages by the package name, the package description and so on. You can use Regular Expressions for searching packages as well.

For example, you can search for all the available text editor packages as follows:

$ sudo apt search 'text editor'

As you can see, the packages that matched search query text editor are listed. The list is very long. I have shown only a small portion of the list here.

Here, the green text represents the package name which you can use to install the package on your Ubuntu machine.

You can also use Regular Expressions to search for packages.

For example, you can search for packages that start with node- as follows:

$ sudo apt search '^node-'

If you’re interested to know more about any specific package (i.e. nodejs), you can run the following command:

$ sudo apt show nodejs

As you can see, a lot of information on the nodejs package is displayed.

Here,

The Package name is nodejs.

The package Version is 10.19.0~dfsg-3ubuntu1.

The package is from web Section of the universe repository.

The package will use 158 KB of disk space once installed (Installed-Size).

It Depends on libc6 and libnode64 packages.

The Homepage of the project is http://nodejs.org.

The package Download-Size is 61.1 KB.

The package is from the http://us.archive.ubuntu.com/ubuntu focal/universe amd64 Packages repository (APT-Sources) etc.

What the package is for is described in the Description section.

There are a lot more information which you can read if you want

Installing Packages from the Enabled Package Repositories:

Once you have found the package or packages you want to install, you can easily install them on your Ubuntu machine.

You can install a single package (i.e. nodejs) as follows:

$ sudo apt install nodejs

As you can see, installing this package will install 3 more dependency packages. In total 4 new packages will be installed. You will have to download about 6,807 KB of package files from the internet. Once the packages are installed, it will consume about 30.7 MB of additional disk space.

To confirm the installation, press Y and then press <Enter>.

As you can see, the required packages are being downloaded from the internet.

At this point, the packages should be installed.

You can also install more than one packages (i.e. filezilla, apache2, vsftpd) at the same time as follows:

$ sudo apt install filezilla apache2 vsftpd

To confirm the installation, press Y and then press <Enter>.

The required packages are being downloaded from the internet.

At this point, all the packages should be installed.

Installing Third-Party DEB Packages:

You can install a third-party DEB package (.deb) file on your Ubuntu machine using the APT package manager. If the third-party DEB package has any dependencies, the APT package manager will try to resolve them as well.

For example, I have downloaded Visual Studio Code DEB package file from the official website of Visual Studio Code. The DEB package file code_1.44.2-1587059832_amd64.deb is in the ~/Downloads directory as you can see in the screenshot below.

Now, you can install the DEB package file code_1.44.2-1587059832_amd64.deb using the APT package manager as follows:

$ sudo apt install ./Downloads/code_1.44.2-1587059832_amd64.deb

The DEB package file should be installed.

Installing Specific Package Version:

A package may have many versions in the package repository. You can install a specific version of that package if you want.

You can find all the available versions of a package (i.e. netplan.io) with the following command:

$ sudo apt show -a <strong>netplan.io</strong> 2>/dev/null | grep Version

As you can see, 2 versions (0.99-0ubuntu2 and 0.99-0ubuntu1) of netplan.io package is available.

Now, you can install netplan.io version 0.99-0ubuntu2 as follows:

$ sudo apt install netplan.io=0.99-0ubuntu2

To confirm the installation, press Y and then press <Enter>.

netplan.io version 0.99-0ubuntu2 should be installed.

The same way, you can install netplan.io version 0.99-0ubuntu1 as follows:

$ sudo apt install netplan.io=0.99-0ubuntu1

Uninstalling Packages:

You can uninstall a package very easily with the APT package manager.

You can uninstall a package (i.e. nodejs) as follows:

$ sudo apt remove nodejs

To confirm to remove operation, press Y and then press <Enter>.

The package should be removed.

Even after you have removed a package (i.e. nodejs), the package may have left some configuration files and other files. You can remove these as well as follows:

$ sudo apt purge nodejs

The leftover files of the package (if it has any) should be removed.

Removing Unnecessary Packages:

Once you have removed a package, the dependency packages that were installed along with the package are no longer needed.

You can remove these unnecessary packages with the following command:

$ sudo apt autoremove

As you can see, 3 packages are no longer needed. Once they are removed, about 30.5 MB of disk space will be freed.

To confirm the remove operation, press Y and then press <Enter>.

All the unnecessary packages should be removed.

Keeping Packages Up to Date:

I have covered this topic in a separate article. To learn how to keep all the packages of your Ubuntu machine up to date read my article How to Update Ubuntu 20.04 from the Command Line Interface.

Cleaning Up:

All the packages that the APT package manager downloads are cached in the /var/cache/apt/archives/ directory as you can see in the screenshot below.

$ ls -lh /var/cache/apt/archives/

To remove the cached packages, run the following command:

$ sudo apt clean

As you can see, the cached packages are removed.

$ ls -lh /var/cache/apt/archives/

So, this is how you use APT package manager in Ubuntu 20.04 LTS. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.