1. Overview

The Advanced Packaging Tool (APT) is a high-level package management tool for the Debian Linux distribution and its derivatives. It carries out metadata searching and dependency resolution for packages.

In this article, we’ll show the contrast between apt-get update and apt-get upgrade commands.

2. Updating Packages

The apt suite downloads packages from a repository and installs them with full dependency resolution. Also, we can use apt to keep packages up-to-date with changes from the source repository.

Next, let’s see the apt-get update command and its unique functions.

2.1. Sources

The apt-get update command fetches the package information from the configured sources on the system:

  • /etc/apt/sources.list file
  • /etc/apt/sources.list.d directory

We can view the content of the sources.list file using the cat command:

$ cat /etc/apt/sources.list
#deb cdrom:[Ubuntu 20.04.3 LTS _Focal Fossa_ - Release amd64 (20210819)]/ focal main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ng.archive.ubuntu.com/ubuntu/ focal main restricted
# deb-src http://ng.archive.ubuntu.com/ubuntu/ focal main restricted
...
deb http://security.ubuntu.com/ubuntu focal-security main restricted
# deb-src http://security.ubuntu.com/ubuntu focal-security main restricted
deb http://security.ubuntu.com/ubuntu focal-security universe
# deb-src http://security.ubuntu.com/ubuntu focal-security universe
deb http://security.ubuntu.com/ubuntu focal-security multiverse
# deb-src http://security.ubuntu.com/ubuntu focal-security multiverse

The sources set on the system are defined via deb-src. Inactive sources are commented out using a # at the beginning of the line.

2.2. Updating Available Packages

To fetch the data about new updates, we use the apt-get update command. Naturally, we need admin access (sudo) to run the command:

$ sudo apt-get update
[sudo] password for user1: 
Hit:1 http://ng.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://ng.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
...
Get:61 http://ng.archive.ubuntu.com/ubuntu focal-backports/universe Translation-en [16.0 kB]
Get:62 http://ng.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [30.5 kB]
Get:63 http://ng.archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [860 B]
Fetched 15.2 MB in 1min 26s (176 kB/s)
Reading package lists... Done

Here, the output contains the repositories in the /etc/apt/sources.list file.

Importantly, apt-get update doesn’t install or upgrade any package. Instead, it only gets metadata for the latest versions of the packages on the system:

  • version
  • repository
  • dependencies
  • other relevant package details

Usually, if there are no newer versions in the repository, we’ll get a pretty basic output:

$ sudo apt-get update
Hit:1 http://ng.archive.ubuntu.com/ubuntu focal InRelease 
Hit:2 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:3 http://ng.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:4 http://ng.archive.ubuntu.com/ubuntu focal-backports InRelease
Reading package lists... Done

In addition, the apt-get update command checks for updates on any third-party repository on the system.

2.3. Checking Available Updates

While the update command refreshes the package cache, it doesn’t list all the possible updates. To list all the packages available for updating, assuming we’ve already run apt-get update, we can use the apt list –upgradable command:

$ apt list --upgradable
Listing... Done
dbus-user-session/focal-updates,focal-security 1.12.16-2ubuntu2.3 amd64 [upgradable from: 1.12.16-2ubuntu2.2]
dbus-x11/focal-updates,focal-security 1.12.16-2ubuntu2.3 amd64 [upgradable from: 1.12.16-2ubuntu2.2]
dbus/focal-updates,focal-security 1.12.16-2ubuntu2.3 amd64 [upgradable from: 1.12.16-2ubuntu2.2]
distro-info-data/focal-updates,focal-updates 0.43ubuntu1.11 all [upgradable from: 0.43ubuntu1.10]
firefox-locale-en/focal-updates,focal-security 107.0+build2-0ubuntu0.20.04.1 amd64 [upgradable from: 105.0+build2-0ubuntu0.20.04.1]
...

Now, the output of this command is a list of all upgradable packages. In addition, it shows the new upgrade version and gives a hint on the version change like in the example:

[upgradable from: 2.36.8-0ubuntu0.20.04.1]

Note that it’s important to run apt-get update first before running apt list –upgradable. Otherwise, the data for the sources in the /etc/apt/sources.list file remains the same, in which case apt list –upgradable might not show any available updates:

$ apt list --upgradable
Listing... Done

Here, the command doesn’t list any package as being eligible for an upgrade, thus highlighting the role of the apt-get update command in providing data regarding available updates.

Next, let’s look at the apt-get upgrade command and how it differs from the apt-get update command.

3. Upgrading Packages

The command apt-get upgrade installs the newest versions of all packages on the system. Of course, it gets the data from the sources in the /etc/apt/sources.list file.

Usually, the update command precedes the upgrade command. Without any available update on the system, the apt-get command doesn’t upgrade any package:

$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
   rsync
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded

However, the upgrade command can proceed if there are available upgrades already on the system. These upgrades can come from previous info on system updates that weren’t upgraded.

After using apt-update, we can run apt-get upgrade to download and install available upgrades in one go:

$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
...
25 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.
20 standard LTS security updates
Need to get 257 MB of archives.
After this operation, 41.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

Again, the output shows that the command first reads the package lists, which contain the metadata from the update command. Next, the upgrade command waits for confirmation to download and install.

Optionally, we can modify the upgrade command to download and install the packages without prompts:

$ sudo apt-get upgrade -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
...
25 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.
20 standard LTS security updates
Need to get 257 MB of archives.
After this operation, 41.5 MB of additional disk space will be used.
Get:1 http://ng.archive.ubuntu.com/ubuntu focal-updates/main amd64 ntfs-3g amd64 1:2017.3.23AR.3-3ubuntu1.3 [388 kB]
Get:2 http://ng.archive.ubuntu.com/ubuntu focal-updates/main amd64 libntfs-3g883 amd64 1:2017.3.23AR.3-3ubuntu1.3 [150 kB]
...
Get:25 http://ng.archive.ubuntu.com/ubuntu focal-updates/main amd64 ubuntu-report amd64 1.6.1ubuntu0.1 [2,374 kB]
Fetched 257 MB in 10min 59s (390 kB/s)
Preconfiguring packages ...
(Reading database ... 183943 files and directories currently installed.)
Preparing to unpack .../00-ntfs-3g_1%3a2017.3.23AR.3-3ubuntu1.3_amd64.deb ...
Unpacking ntfs-3g (1:2017.3.23AR.3-3ubuntu1.3) over (1:2017.3.23AR.3-3ubuntu1.2) ...
...
Setting up libexpat1:amd64 (2.2.9-1ubuntu0.5) ...
Setting up libpixman-1-0:amd64 (0.38.4-0ubuntu2.1) ...
...

Here, the -y flag adds the validation apt needs to carry out the process.

4. Combining Update and Upgrade

Of course, we can combine update and upgrade in one command:

$ sudo apt-get update && sudo apt-get upgrade -y

To explain, this combination carries out the following tasks in one go:

  • refreshes the package metadata
  • downloads the updates
  • installs the updates

Also, the && operator chains the two commands together, expecting a zero exit code after the first. However, we can use a semicolon to join them without any dependency on the exit code:

$ sudo apt-get update; sudo apt-get upgrade -y

Again, the -y flag acts as a preset to confirm the upgrade of our packages.

5. Conclusion

In summary, we saw the major difference between apt-get update and apt-get upgrade. In addition, we illustrated how to use the commands and the information they provide.

Comments are closed on this article!