I had problems with this as well. So there are many tutorials how to install different version of nodejs but they did not work in my case. However the last with nvm did.
Method 1: Install nodejs from specific source
cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
by changing setup_16.x to your version, you change the version
sudo bash nodesource_setup.sh
then you test sources with
# sample on Ubuntu 22.10
$ cat /etc/apt/sources.list.d/nodesource.list
deb https://deb.nodesource.com/node_16.x focal main
deb-src https://deb.nodesource.com/node_16.x focal main
Then you install nodejs from that source (did not work in my case)
sudo apt -y install nodejs
And check installed version
node -v
This should return
v16.19.0
but in my case (Ubuntu 22.10) I got the new version
v18.7.0
Method 2: Install Node Vesrion Manager or nvm
This worked in my case:
sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.bashrc
Now to install an older version of nodejs with nvm
you can first check available versions by
nvm list-remote
and then install specific version by (ex. version v16.19.0)
nvm install 16.19.0
and check with:
$ node -v
v16.19.0
To install latest version of node with nvm
nvm install node
$ node -v
v18.7.0
Hope this helps someone to