What sudo apt update and upgrade Do on Ubuntu / Debian
In this guide, we aim to explain What sudo apt update and upgrade Do on Ubuntu / Debian. As you likely know, APT is the default package manager for Ubuntu and Debian distributions. It’s used for managing software packages, including installing, updating, and removing them. Two of the most useful commands in Debian-based distributions are sudo apt update and sudo apt upgrade. Before starting any significant task on your server, these commands are crucial for ensuring your system is running the latest versions of software. Understanding What sudo apt update and upgrade Do on Ubuntu / Debian is essential for maintaining a stable and secure system.
Now, let’s delve into the steps below to understand What sudo apt update and upgrade Do on Ubuntu / Debian more thoroughly.
Note: You might also consider using apt-get
instead of apt
. For a detailed comparison, refer to this guide: apt vs apt-get – Which One Should We Use, to grasp the nuances between the two.
Many users mistakenly believe that sudo apt update
and sudo apt upgrade
perform the same function. However, they serve distinct purposes. Let’s explore these differences in detail to understand What sudo apt update and upgrade Do on Ubuntu / Debian.
Step 1 – What Is the Difference Between sudo apt update and sudo apt upgrade?
Firstly, remember that sudo
is used to execute commands with root privileges. Users with sudo
privileges, though not the root user themselves, can perform administrative tasks.
Now, let’s highlight the core difference between sudo apt update
and sudo apt upgrade
.
The sudo apt update
command refreshes the package lists from the repositories defined in your system’s configuration. It essentially checks for the latest versions of packages available in the default Debian and Ubuntu repositories. It identifies which packages have updates available and their dependencies, but it does not download or install anything. This is the first step in ensuring your system has access to the most current package information.
On the other hand, the sudo apt upgrade
command downloads and installs the latest versions of the packages identified by sudo apt update
. It resolves dependencies and ensures that your installed software is updated to the newest available versions.
Step 2 – Usage of sudo apt update and sudo apt upgrade Commands on Ubuntu / Debian
Now, let’s demonstrate how to use these commands in practice. For example, we can run the sudo apt update
command on Ubuntu 22.04 like this:
sudo apt update
This will produce an output similar to the following:
**Output**
Hit:1 http://de.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://de.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:3 http://de.archive.ubuntu.com/ubuntu jammy-backports InRelease [109 kB]
Get:4 http://de.archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:5 http://de.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [970 kB]
Get:6 http://de.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [979 kB]
Fetched 2,286 kB in 1s (1,913 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
193 packages can be upgraded. Run 'apt list --upgradable' to see them.
As the output indicates, the command successfully fetched updated package lists, and there are 193 packages available for upgrading.
To see a list of these upgradable packages, execute the following command:
sudo apt list --upgradable
This will display a list similar to:
**Output**
Listing... Done
amd64-microcode/jammy-updates,jammy-security 3.20191218.1ubuntu2.2 amd64 [upgradable from: 3.20191218.1ubuntu2]
apparmor/jammy-updates 3.0.4-2ubuntu2.2 amd64 [upgradable from: 3.0.4-2ubuntu2.1]
apport/jammy-updates 2.20.11-0ubuntu82.5 all [upgradable from: 2.20.11-0ubuntu82.1]
apt-utils/jammy-updates 2.4.10 amd64 [upgradable from: 2.4.6]
apt/jammy-updates 2.4.10 amd64 [upgradable from: 2.4.6]
base-files/jammy-updates 12ubuntu4.4 amd64 [upgradable from: 12ubuntu4.2]
bind9-dnsutils/jammy-updates,jammy-security 1:9.18.12-0ubuntu0.22.04.2 amd64 [upgradable from: 1:9.18.1-1ubuntu1.1]
....
Now, you can use the sudo apt upgrade
command to download and install these updated packages and their dependencies on your Ubuntu / Debian system:
sudo apt upgrade
The output will look something like this:
**Output**
...
193 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
96 standard security updates
Need to get 484 MB of archives.
After this operation, 732 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Enter Y
to proceed with the upgrade process.
This will download and install the latest packages available on your Debian and Ubuntu distributions. The process may take some time, depending on the number of packages being upgraded and your internet connection speed.
Upon completion, you’ll see an output similar to:
**Output**
done
Scanning processes...
Scanning candidates...
Scanning linux images...
...
The sudo apt upgrade
command doesn’t automatically remove old, unnecessary dependencies. To clean up these obsolete packages and dependencies, you can use the following command:
sudo apt autoremove
The output will resemble:
**Output**
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
libflashrom1 libftdi1-2 linux-headers-5.15.0-43
linux-headers-5.15.0-43-generic linux-image-5.15.0-43-generic
linux-modules-5.15.0-43-generic linux-modules-extra-5.15.0-43-generic
0 upgraded, 0 newly installed, 7 to remove and 0 not upgraded.
After this operation, 562 MB disk space will be freed.
Do you want to continue? [Y/n] Y
Press Y
to confirm and free up disk space by removing the unneeded packages.
Conclusion
At this point, you should understand What sudo apt update and upgrade Do on Ubuntu / Debian. These two commands are fundamental for maintaining your Debian-based distribution, enabling you to fetch and install the latest package versions from the default repositories. Understanding What sudo apt update and upgrade Do on Ubuntu / Debian is crucial for system administrators and regular users alike.
Hopefully, you found this guide helpful. You might also be interested in these articles:
Disable NTP Network Time Synchronization on Ubuntu
Alternative Solutions for Package Management in Ubuntu/Debian
While sudo apt update
and sudo apt upgrade
are the standard methods for managing packages in Ubuntu and Debian, there are alternative approaches that offer different functionalities or are better suited for specific situations. Here are two such alternatives:
1. Using unattended-upgrades
for Automatic Security Updates
The unattended-upgrades
package provides a way to automatically install security updates without requiring manual intervention. This is particularly useful for servers or systems where maintaining security is paramount, and manual updates might be overlooked. This doesn’t replace the need to understand What sudo apt update and upgrade Do on Ubuntu / Debian, but it automates a crucial aspect.
Explanation:
unattended-upgrades
works by periodically checking for security updates and installing them automatically. It is configured through files in the /etc/apt/apt.conf.d/
directory, specifically 50unattended-upgrades
. You can customize the types of updates that are automatically installed and the repositories from which they are fetched.
Installation and Configuration:
-
Install
unattended-upgrades
:sudo apt install unattended-upgrades
-
Configure Automatic Updates:
Edit the
/etc/apt/apt.conf.d/50unattended-upgrades
file to specify which updates should be automatically installed. You can enable or disable specific origins (repositories) and update types (e.g., Security, Updates).For example, to ensure only security updates are automatically installed from the Ubuntu security repository, you can configure the
Unattended-Upgrade::Allowed-Origins
section like this:Unattended-Upgrade::Allowed-Origins { "${distro_id}:${distro_codename}-security"; // "${distro_id}:${distro_codename}-updates"; // "${distro_id}:${distro_codename}-proposed"; // "${distro_id}:${distro_codename}-backports"; };
Uncommenting the lines for
-updates
,-proposed
, or-backports
would enable automatic upgrades from those repositories as well. -
Enable Automatic Upgrades:
Create or edit the
/etc/apt/apt.conf.d/20auto-upgrades
file and set the following options:APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::Update-Package-Lists "1";
enables automatic updating of the package lists.APT::Periodic::Unattended-Upgrade "1";
enables the unattended upgrades themselves.
-
Test the Configuration:
You can test the configuration by running
unattended-upgrades
in debug mode:sudo unattended-upgrades -d
This will simulate an upgrade and show you what packages would be installed.
unattended-upgrades
offers a hands-off approach to security, ensuring your system remains protected against vulnerabilities without constant manual intervention.
2. Using apt-get
with Specific Options for More Granular Control
While apt
is the recommended tool, apt-get
provides more fine-grained control over the upgrade process through various options. For instance, you might want to upgrade only a specific package or simulate an upgrade to see what changes will be made. Knowing What sudo apt update and upgrade Do on Ubuntu / Debian gives context, but apt-get
allows specific targeting.
Explanation:
apt-get
commands, combined with specific options, offer a level of control that apt
sometimes lacks. For example, you can use apt-get upgrade
to upgrade all packages, similar to apt upgrade
, but you can also use apt-get install --only-upgrade <package_name>
to upgrade a single package without affecting others.
Code Examples and Usage:
-
Simulating an Upgrade:
Before performing an actual upgrade, you can simulate it to see what changes will be made. This is done using the
-s
or--simulate
option:sudo apt-get upgrade -s
This will print a list of packages that would be upgraded, installed, or removed, without actually making any changes to the system.
-
Upgrading a Specific Package:
To upgrade only a specific package, use the
install
command with the--only-upgrade
option:sudo apt-get install --only-upgrade <package_name>
Replace
<package_name>
with the actual name of the package you want to upgrade. -
Dist-Upgrade for Handling Complex Dependencies:
Sometimes, a simple
upgrade
might not be sufficient if new dependencies are required or existing ones need to be removed. In such cases,dist-upgrade
is used, which handles dependencies more intelligently:sudo apt-get dist-upgrade
This command is similar to
apt full-upgrade
and is useful for major system upgrades. -
Forcibly Reinstalling a Package:
If a package is corrupted or not functioning correctly, you can forcibly reinstall it using the
--reinstall
option:sudo apt-get install --reinstall <package_name>
This will download and reinstall the specified package, potentially resolving any issues.
These apt-get
options provide more control over the upgrade process, allowing you to manage packages selectively and address specific issues effectively. While understanding What sudo apt update and upgrade Do on Ubuntu / Debian is important, these more granular options offer flexibility for experienced users.