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.
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:
Option | Description | Example |
---|---|---|
-y | Automatically answer yes to all prompts. | sudo apt-get --reinstall install -y package-name |
--no-install-recommends | Install recommended but not non-essential packages. | sudo apt-get --reinstall install --no-install-recommends package-name |
--only-upgrade | Only upgrade installed packages. | sudo apt-get --reinstall install --only-upgrade package-name |
--dry-run | Simulate the reinstallation process without making changes. | sudo apt-get --reinstall install --dry-run package-name |
--download-only | Download packages without installing them. | sudo apt-get --reinstall install --download-only package-name |
--allow-downgrades | Reinstall an older version of the package. | sudo apt-get --reinstall install --allow-downgrades package-name |
--verbose-versions | Display 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
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
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
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
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
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:
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.
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.