How to install a specific version of a package

I wanted to install cncjs on Raspberry, but a specific version of Node.js has to be installed. By default, the latest version is installed whatever if there is many sources.

Check package availability

Before installation, you can type apt-cache madison to see if there is many version available.

apt-cache madison packagename

Here is the output:

pi@raspberrypi:~ $ apt-cache madison nodejs
    nodejs | 10.19.0~dfsg1-1 | http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages
    nodejs | 7.10.1-2nodesource1~buster1 | https://deb.nodesource.com/node_7.x buster/main armhf Packages
    nodejs | 7.10.1-2nodesource1~buster1 | https://deb.nodesource.com/node_7.x buster/main Sources

Or with another display with apt-cache policy:

pi@raspberrypi:~ $ apt-cache policy nodejs
nodejs:
  Installed: (none)
  Candidate: 10.19.0~dfsg1-1
  Version table:
     10.19.0~dfsg1-1 500
        500 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages
     7.10.1-2nodesource1~buster1 500
        500 https://deb.nodesource.com/node_7.x buster/main armhf Packages

Install the specific version

We can use apt-get to proceed with the installation:

apt-get install package=version -V

The -V parameter will give more information.

Simulate installation

We can simulate the installation to check if everything will go well.

apt-get install -s package
apt-get install -s nodejs=7.10.1-2nodesource1~buster1

Check version installed of a package

dpkg -l | grep nodejs

Avoid further updates

Finally, in order to avoid the update of the version installed to a more recent one, we need to freeze the package. To do so:

apt-mark hold nodejs # Freeze

Or to unlock it afterwards:

apt-mark unhold nodejs # Unfreeze

If apt update is done, we will have the recent package in the list –upgradable. Nevertheless, it will be kept back when performing apt upgrade.

pi@raspberrypi:~ $ sudo apt upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  nodejs
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.