Reinstall Packages in Ubuntu and Debian Using apt-get --reinstall

October 22, 2020

Introduction

Files in packages sometimes get corrupted or accidentally modified. Trying to reinstall an already installed package with the apt-get install command may result in an error.

You can force the reinstallation of the latest package version with the --reinstall flag. This option is especially useful for packages with multiple software dependencies.

Find out how to use apt-get --reinstall to reinstall packages on Debian and Ubuntu.

Reinstalling packages on Ubuntu and Debian.

Prerequisites

  • A Debian/Ubuntu system.
  • Access to the command line/terminal.
  • An account with sudo privileges.

Note: Ensure your package lists are up-to-date by running sudo apt update before using --reinstall.

Reinstall Packages Using apt-get --reinstall

The --reinstall command has a straightforward syntax:

sudo apt-get --reinstall install [package_name]

To reinstall more than one package, list them all in one line:

sudo apt-get --reinstall install [package_name1] [package_name2]

Note: The --reinstall flag does not remove user configuration files or system-wide settings. To reset configurations, you may need to purge the package (including the configuration files) and then install it from scratch.

apt-get --reinstall Options

The table lists common --reinstall options used to control and customize the reinstallation process:

OptionDescriptionExample
-yAutomatically answer yes to all prompts.sudo apt-get --reinstall install -y package-name
--no-install-recommendsInstall recommended but not non-essential packages.sudo apt-get --reinstall install --no-install-recommends package-name
--only-upgradeOnly upgrade installed packages.sudo apt-get --reinstall install --only-upgrade package-name
--dry-runSimulate the reinstallation process without making changes.sudo apt-get --reinstall install --dry-run package-name
--download-onlyDownload packages without installing them.sudo apt-get --reinstall install --download-only package-name
--allow-downgradesReinstall an older version of the package.sudo apt-get --reinstall install --allow-downgrades package-name
--verbose-versionsDisplay detailed package version information during the reinstallation.sudo apt-get --reinstall install --verbose-versions package-name

apt-get --reinstall Examples

The following section provides practical examples of how to use the --reinstall flag.

Force Reinstall Package

To force the reinstallation of a specific package, for example, the curl command, enter:

sudo apt-get --reinstall install curl
Reinstalling curl on Ubuntu using the apt-get command.

The APT package manager downloads and reinstalls the latest curl version from the repository.

Reinstall Multiple Packages

The --reinstall flag allows you to reinstall multiple packages using a single command. For example, enter the following command to reinstall curl and wget:

sudo apt-get --reinstall install curl wget
Reinstalling multiple packages in Ubuntu.

The system downloads and installs the specified packages sequentially. You can list as many packages as needed in the command.

Reinstall Package from a Specific Repository

To reinstall a specific package version, you must specify the version number in the command. For example, to display the available Python versions in your repositories, enter:

apt-cache policy python3
Available Python versions in repository to reinstall.

Choose a version number from the Version table and use it to reinstall a specific package:

sudo apt-get --reinstall install python3=3.12.3-0ubuntu2

The command reinstalls Python version 3.12.3-0ubuntu2 on your system.

Note: To avoid switching between apt-get and apt-cache, consider using the apt command to manage packages on Debian-based Linux distributions. Learn the differences between apt and apt-get and why apt is more user-friendly than traditional APT tools.

Reinstall All Installed Packages

In case of a general package integrity issue, you can use --reinstall to restore all files to their original state:

sudo apt-get --reinstall install -y $(dpkg-query -W -f='${binary:Package}\n')

The dpkg-query command generates a list of installed packages on the system. The apt-get command with the -y option automatically reinstalls each package from the list.

Note: This option is resource-intensive and can take a long time to complete. It should only be used as a last resort when troubleshooting.

Reinstall Packages Using the aptitude Command and Interface

Aptitude is a text-based interface for the APT package manager. It can also be used from the command line to reinstall packages using the following syntax:

sudo aptitude reinstall [package_name]

For example, to reinstall the htop tool, which lists running processes in Linux, enter:

sudo aptitude reinstall htop
Using the aptitude tool to reinstall packages on Ubuntu and Debian.

How to Reinstall apt Package Manager

If you accidentally removed apt from your system, reinstall it manually using the following steps:

1. (Optional) Enter the following command to check your system architecture:

uname -m
Checking the architecture designation of a Linux system.

The x86_64 output indicates a 64-bit (amd64) architecture.

2. Use a browser to access the Ubuntu archive or the Debian archive. Select the apt package for your system's release:

Manually download apt package from Ubuntu archive.

3. Download the latest stable package for your architecture. In this example, the latest apt version for Ubuntu 24.04 (noble) is apt_2.7.14build2_amd64.deb.

4. Scroll down and select the mirror link closest to your location for a faster download.

Download the latest stable apt version for Ubuntu 24.04.

5. Open the terminal, navigate to the directory the package was downloaded to, and enter the following command to install it:

sudo dpkg -i apt_2.7.14build2_amd64.deb

Replace the version and architecture in the example to match the settings on your system.

Conclusion

After reading this article, you can reinstall packages using apt-get and aptitude commands, and restore the apt tool if it is removed accidentally.

Also, learn how to manage packages using Ubuntu's most prominent package managers.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Snap vs APT: What's the Difference?
January 21, 2021

This article compares the Snap packaging system and the APT package manager.
Read more
How to Fix "add-apt-repository command not found" on Ubuntu & Debian
March 21, 2024

This guide shows you how to fix the 'add-apt-repository command not found' error in Ubuntu. This specific Ubuntu error appears when trying to add a new software repository when the command is not installed on your system.
Read more
Fix "sub-process /usr/bin/dpkg returned an error code (1)"
March 5, 2024

Ddpkg is a tool used to install packages in Debian-based distributions. The error message “Sub-process /usr/bin/dpkg returned an error code (1)” indicates a problem with the package installer.
Read more
Linux Commands Cheat Sheet {with Free Downloadable PDF}
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or download our Linux Commands Cheat Sheet and save it for future reference.
Read more