How to install a specific version of Node on Ubuntu?

Solution 1:

The n module worked for me.

Run this code to clear npm’s cache, install n, and install the latest stable version of Node:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

See: http://www.hostingadvice.com/how-to/update-node-js-latest-version/
And: https://www.npmjs.com/package/n

To install a specific version of node:

sudo n 6.11.2

To check what version:

node -v

You might need to restart

Solution 2:

Chris Lea has 0.8.23 in his ppa repo.

This package let you add a repository to apt-get: (You can also do this manually)

sudo apt-get install software-properties-common

Add Chris Lea's repository:

sudo apt-add-repository ppa:chris-lea/node.js-legacy

Update apt-get:

sudo apt-get update

Install Node.js:

sudo apt-get install nodejs=0.8.23-1chl1~precise1

I think (feel free to edit) the version number is optional if you only add node.js-legacy. If you add both legacy and ppa/chris-lea/node.js you most likely need to add the version.

Solution 3:

It is possible to install specific version of nodejs from nodejs official distribution with using dpkg.

  • Check the version of you ubuntu distribution, cat /etc/lsb-release.
  • Check architecture of your os, uname -m.
  • Download preferred version of debian package from nodejs official site.
    • For 4.x, https://deb.nodesource.com/node_4.x/pool/main/n/nodejs/
    • For 5.x, https://deb.nodesource.com/node_5.x/pool/main/n/nodejs/
    • For 0.12.x, https://deb.nodesource.com/node_0.12/pool/main/n/nodejs/
  • Be careful to check nodejs-dbg or nodejs in filename.

For example, currently recent 4.x version is 4.2.4, but you can install previous 4.2.3 version.

curl -s -O https://deb.nodesource.com/node_4.x/pool/main/n/nodejs/nodejs_4.2.3-1nodesource1~trusty1_amd64.deb
sudo apt-get install rlwrap
sudo dpkg -i nodejs_4.2.3-1nodesource1~trusty1_amd64.deb

Solution 4:

NVM (Node Version manager)

https://github.com/nvm-sh/nvm

Advantages:

  • allows you to use multiple versions of Node and without sudo

  • is analogous to Ruby RVM and Python Virtualenv, widely considered best practice in Ruby and Python communities

  • downloads a pre-compiled binary where possible, and if not it downloads the source and compiles one for you

Tested in Ubuntu 17.10:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh
source ~/.nvm/nvm.sh
nvm install 0.9.0
nvm install 0.9.9
nvm use 0.9.0
node --version
#v0.9.0
nvm use 0.9.9
node --version
#v0.9.9

For the particular case of the most recent long term support version (recommended if you can choose):

nvm install --lts
nvm use --lts
npm --version
npm install --global vaca
vaca

Since the sourcing has to be done for every new shell, the install script hacks adds some auto sourcing to the end of your .barshrc. That works, but I prefer to remove the auto-added one and add my own:

f="$HOME/.nvm/nvm.sh"
if [ -r "$f" ]; then
  . "$f" &>'/dev/null'
  nvm use --lts &>'/dev/null'
fi

With this setup, you get for example:

which node

gives:

/home/ciro/.nvm/versions/node/v0.9.0/bin/node

and:

which vaca

gives:

/home/ciro/.nvm/versions/node/v0.9.0/bin/vaca

and if we want to use the globally installed module:

npm link vaca
node -e 'console.log(require.resolve("vaca"))'

gives:

/home/ciro/.nvm/versions/node/v0.9.0/lib/node_modules/vaca/index.js

as mentioned at:

  • NodeJS require a global module/package
  • How do I import global modules in Node? I get "Error: Cannot find module <module>"?

For projects however, you are better off just using packages installed locally under node_modules and npx for executable to be able to have independent versions across projects, global usage is mostly for the Node executable itself and global CLI utilities not specific to any project.

so we see that everything is completely contained inside the specific node version.

Setting the NPM version

Simply:

npm install [email protected] -g

The executable is placed inside the current NVM version, so everything remains nice and isolated, e.g.:

which npm

gives something like:

/home/ciro/.nvm/versions/node/v14.17.0/bin/npm

How can I change the version of npm using nvm?