You do not mention how you would like to manage the versions, I will illustrate a simple method using symlinks. There are packages that can manage the versions for you, such as the Node Version Manager if you would rather use those.
Starting with a node
directory in my user's home, I have populated it with two versions of node
:
$ tree -L 3 node
node
|-- node-v12.16.1-linux-x64
| |-- CHANGELOG.md
| |-- LICENSE
| |-- README.md
| |-- bin
| | |-- node
| | |-- npm -> ../lib/node_modules/npm/bin/npm-cli.js
| | `-- npx -> ../lib/node_modules/npm/bin/npx-cli.js
| |-- include
| | `-- node
| |-- lib
| | `-- node_modules
| `-- share
| |-- doc
| |-- man
| `-- systemtap
`-- node-v13.10.1-linux-x64
|-- CHANGELOG.md
|-- LICENSE
|-- README.md
|-- bin
| |-- node
| |-- npm -> ../lib/node_modules/npm/bin/npm-cli.js
| `-- npx -> ../lib/node_modules/npm/bin/npx-cli.js
|-- include
| `-- node
|-- lib
| `-- node_modules
`-- share
|-- doc
|-- man
`-- systemtap
You would then make a symbolic link to the version you would like to use:
$ ln -s node-v12.16.1-linux-x64 node/nodejs
and finally, we need to link all up this through your PATH:
$ export PATH=~/node/nodejs/bin:$PATH
Now, you should be able to run node
commands:
$ node --version
v12.16.1
$ npm --version
6.13.4
You can switch the versions by changing the symbolic link:
$ unlink node/nodejs
$ ln -s node-v13.10.1-linux-x64 node/nodejs
$ node --version
v13.10.1
$ npm --version
6.13.7
Unfortunately, this does not handle global packages, as all versions would install/use global packages from the same place. If you have two users attempting to use different versions of node
, having global packages can break things.
PATH = /path/to/node.js/bin:$PATH
– Jared Smith Mar 06 '20 at 21:17