On Ubuntu and Debian, apt-get install (or the newer apt install) installs the latest version available in the repositories by default. This is usually the right thing to do, as the latest version contains security patches. Sometimes, however, a specific version is required: an application may only be compatible with a particular version, an update may have caused an error and you need to revert to the previous version, or you may need to run the same version on multiple servers.
The following commands require root or sudo privileges, so they can be used on your own KVM VPS or dedicated server.
1. Check which versions are available
Update the package list, then check the package versions:
sudo apt-get update
apt-cache policy nginx
In the output, the Installed line shows the version currently installed, the Candidate line shows the version installed by default, and below that are listed all the versions available in the repositories. On a Debian 13 test machine, for the bind9-host package (part of the dig and host commands):
bind9-host:
Installed: 1:9.20.30-1~deb13u1
Candidate: 1:9.20.30-1~deb13u1
Version table:
*** 1:9.20.30-1~deb13u1 500
500 mirror+file:/etc/apt/mirrors/debian-security.list trixie-security/main arm64 Packages
100 /var/lib/dpkg/status
1:9.20.26-1~deb13u1 500
500 mirror+file:/etc/apt/mirrors/debian.list trixie/main arm64 Packages
The installed version came from the security repository (trixie-security); the release version is still available in the main repository (trixie/main). The same information is displayed in a shorter form by the commands apt list -a nginx or apt-cache madison nginx.
2. Install the desired version
After the package name, enter the version number separated by an equals sign, exactly as shown in the previous command, including the revision number and, if present, the leading 1: (epoch) part:
sudo apt-get install nginx=1.24.0-2ubuntu7
sudo apt-get install bind9-host=1:9.20.26-1~deb13u1
The version number given here is just an example; copy your own from the output of apt-cache policy. If the requested version is older than the one currently installed, apt will prompt you to confirm the downgrade; when running in non-interactive mode, you must use the --allow-downgrades option for this.
3. Record the version
If you do not pin the package, the next apt upgrade or automatic update will revert it to the latest version. To pin it:
sudo apt-mark hold nginx
apt-mark showhold
sudo apt-mark unhold nginx
The first command holds back the package, the second lists the held-back packages, and the third releases the hold. During the test, the update listed the held-back packages in the line The following packages have been kept back: and did not update them. A held-back package does not receive security updates either, so please use this only as a temporary solution. You can read about updating the system in our system update guide.
Common errors
E: Version '...' for '...' was not found: the specified version is not available in the repositories. Under Ubuntu and Debian, repositories generally only retain the release version and the most recent updated version; intermediate versions are removed. Please check the exact version number, or use the software vendor’s own repository, which often offers several older versions.Depends: ... but ... is to be installed: the package requires the same version of other packages. In the test, the downgrade ofbind9-hostwas halted as follows:bind9-host : Depends: bind9-libs (= 1:9.20.26-1~deb13u1) but 1:9.20.30-1~deb13u1 is to be installed. In such cases, specify the dependency in the same command:sudo apt-get install --allow-downgrades bind9-host=1:9.20.26-1~deb13u1 bind9-libs=1:9.20.26-1~deb13u1; this downgraded both packages in the test.- Installing a downloaded
.debfile: if you have the.debpackage for the old version (for example, in the/var/cache/apt/archives/directory), you can install it using the commandsudo apt install ./csomag.deb, which will allow apt to resolve the dependencies.
Verification
After installation, the Installed line in the output of the apt-cache policy csomagnev command shows the actual version, whilst most programs also display this using the --version switch (for example, nginx -v).