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 cat Command: Display and Concatenate File Contents

Linux cat Command: Display and Concatenate File Contents

Learn how to use the Linux cat command to display text files, concatenate multiple files, number lines, reveal hidden characters, and avoid unsafe redirection mistakes.

Linux pgrep Command: Find Process IDs by Name or Criteria

Linux pgrep Command: Find Process IDs by Name or Criteria

Learn how to find Process IDs by Name or Criteria with the Linux pgrep command, including practical examples, key options, and important precautions.

Linux su Command: Switch User Accounts and Login Shells

Linux su Command: Switch User Accounts and Login Shells

Learn how to use the Linux su command to switch user accounts and start login or non-login shells, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux file Command: Identify File Types and Formats

Linux file Command: Identify File Types and Formats

Learn how to use the Linux file command to identify file formats from their contents instead of filename extensions, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Learn how to inspect and Configure Network Interfaces and Routes with the Linux ip command, including practical examples, key options, and important precautions.

Linux Tutorial / Hard Links vs. Symbolic Links in Linux: Differences and Examples

Linux Tutorial / Hard Links vs. Symbolic Links in Linux: Differences and Examples

Compare hard and symbolic links through inode and path behavior, then create both kinds of link and observe what happens when the original name changes.

Linux rm Command: Remove Files and Directories Safely

Linux rm Command: Remove Files and Directories Safely

Learn how to remove files and directory trees with Linux rm, use interactive safeguards, limit recursive deletion, and avoid destructive path mistakes.

Linux ping Command: Test Host Reachability and Round-Trip Time

Linux ping Command: Test Host Reachability and Round-Trip Time

Learn how to test Host Reachability and Round-Trip Time with the Linux ping command, including practical examples, key options, and important precautions.

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Practice creating, copying, renaming, moving, and deleting files and directories in a dedicated folder while checking each result safely.

Linux apt Command: Manage Packages on Debian and Ubuntu

Linux apt Command: Manage Packages on Debian and Ubuntu

Learn how to manage Packages on Debian and Ubuntu with the Linux apt command, including practical examples, key options, and important precautions.