Linux Tutorial / Install and Update Packages with APT and DNF

A Linux package manager obtains programs and their dependencies from configured repositories and handles installation, updates, and removal. Debian and Ubuntu commonly use APT through apt; Rocky Linux uses DNF through dnf. For a safe update, distinguish refreshing information about available packages from actually changing installed software.

Understand packages, repositories, and dependencies

A package bundles software with metadata such as version and dependencies. A repository is a source of packages supplied by a distribution or added by an administrator. When an application needs a library, the package manager can propose installing that dependency as part of a transaction. Before confirming, inspect the source and every package that would be installed, replaced, or removed.

The same command name can yield different versions and results depending on the distribution, release, and enabled repositories. Do not mix the Debian/Ubuntu and Rocky Linux commands on one machine. Check your distribution first:

cat /etc/os-release
command -v apt
command -v dnf

ID and ID_LIKE in /etc/os-release help identify the distribution family. command -v only checks whether an executable is available; it does not fully describe that system's package policy. A minimal image or container may omit or restrict package-management tools.

Compare common APT and DNF tasks

Purpose Debian/Ubuntu: APT Rocky Linux: DNF
Search apt search tree dnf search tree
Package details apt show tree dnf info tree
Check for updates sudo apt update, then apt list --upgradable sudo dnf check-update
Install sudo apt install tree sudo dnf install tree
Upgrade installed packages sudo apt upgrade sudo dnf upgrade
Remove sudo apt remove tree sudo dnf remove tree

tree is just an example package; verify availability in your repositories. dnf check-update can return exit status 100 when updates are available, which an automation should not automatically classify as failure. The install, upgrade, and remove commands change system state. Run them only after reviewing the target on a suitable practice system.

Use APT on Debian or Ubuntu

APT manages Debian-format .deb packages. sudo apt update downloads current package-list information from configured repositories; by itself it generally does not upgrade installed applications. Inspect candidates with apt list --upgradable. Only sudo apt upgrade proceeds to update installed packages after confirmation.

sudo apt update
apt list --upgradable
sudo apt upgrade

Before an upgrade, review the packages to be added, upgraded, or removed and the proposed download. If fetching repository metadata fails, the list may be stale or incomplete; investigate warnings rather than assuming everything is current. apt is convenient for interactive use. For scripts that require a stable command-line interface, consult the distribution's guidance on tools such as apt-get.

Search and inspect a new program before installing it. Only the last command below performs the installation.

apt search tree
apt show tree
sudo apt install tree
tree --version

If installation succeeds, tree --version reports the installed version. The number varies by distribution and repository. If the package cannot be found, check the distribution, configured repositories, and spelling. Do not add an arbitrary repository or run an installation script found online without establishing its provenance.

Use DNF on Rocky Linux

Rocky Linux manages RPM packages with DNF. Use dnf search and dnf info to inspect candidates, then sudo dnf install to install one. DNF displays the proposed transaction, including dependencies and removals, so read it instead of automatically accepting prompts.

dnf search tree
dnf info tree
sudo dnf install tree
tree --version

Checking for available updates and upgrading installed packages are separate actions. Below, check-update lists candidates; upgrade changes installed packages after approval.

sudo dnf check-update
sudo dnf upgrade

Routine Rocky Linux package updates are not the same as a major-version migration. Running dnf upgrade does not guarantee a safe switch to the next major release. For that, consult the release-specific official guidance and plan backups, compatibility checks, and a migration path separately.

What to review before installation

  1. Identify the distribution family and release and inspect the enabled repositories. Prefer an official or otherwise trusted source for the required package.
  2. Read the package name, version, description, and dependencies. Similar names do not necessarily mean the same function.
  3. Review proposed additions, removals, and disk use. For a production server, plan for service interruption and recovery.
  4. After running the command, read errors and warnings, verify the program version, and check affected services where appropriate.
  5. Do not assume every update can simply be rolled back. Recovery depends on backups, available package versions, and distribution guidance.

Whether a security update needs a reboot depends on the changed packages and operating policy. Installing a new kernel does not immediately change the running kernel. Check the current version with uname -r, then schedule a reboot if required and acceptable for the service.

Be careful with removal and cleanup

apt remove and dnf remove remove the named package. APT's purge also removes package-managed configuration files, making it more consequential than a simple remove. Before using autoremove to clean up automatically installed dependencies, inspect its proposed list for software still needed. Removal behavior for data directories and manually created files varies; it is not a substitute for a backup.

DNF history can help investigate earlier transactions, but a recorded transaction does not guarantee a complete rollback of every change. Configuration, data, and third-party repository changes may need separate backups.

Official references

The Ubuntu Server package-management guide explains APT index refreshes, installation, and upgrades. The DNF command reference documents install, check-update, upgrade, and their exit behavior. See the Rocky Linux update guide for the scope of release updates.

More in This Category
Linux reboot Command: Restart the System Safely

Linux reboot Command: Restart the System Safely

Learn how to restart the System Safely with the Linux reboot command, including practical examples, key options, and important precautions.

Linux who Command: List Logged-In Users

Linux who Command: List Logged-In Users

Learn how to list Logged-In Users with the Linux who command, including practical examples, key options, and important precautions.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.

Linux dirname Command: Extract the Directory Part of a Path

Linux dirname Command: Extract the Directory Part of a Path

Learn how to use the Linux dirname command to remove the final component of a path and return its directory portion, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.

Linux journalctl Command: Read systemd Logs

Linux journalctl Command: Read systemd Logs

Learn how to read systemd Logs with the Linux journalctl command, including practical examples, key options, and important precautions.

Linux whatis Command: Show One-Line Command Descriptions

Linux whatis Command: Show One-Line Command Descriptions

Learn how to use the Linux whatis command to display concise descriptions of commands and manual pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux scp Command: Copy Files over SSH

Linux scp Command: Copy Files over SSH

Learn how to copy Files over SSH with the Linux scp command, including practical examples, key options, and important precautions.

Linux rpm Command: Inspect RPM Packages and the Installed Database

Linux rpm Command: Inspect RPM Packages and the Installed Database

Learn how to inspect RPM Packages and the Installed Database with the Linux rpm command, including practical examples, key options, and important precautions.