I often need to play and experiment and using different environments help. In the era of virtualisation we have many options. For this exercise, I'm using Ubuntu Multipass which allow you to vey quickly create virtual machines.
The steps described here will not only install the latest version of Node.js but will also setup the environment to install any global packages in ~/.npm-packages
.
$ multipass launch -n nodejs
Launched: nodejs
Getting a shell into the new virtual machine is a quick one liner:
$ multipass shell nodejs
The rest of the instructions is essentially a summary of what worked for me after reading this Stackoverflow discussion
The Node.js installation steps were taken from this site (last accessed 2020-06-04)
Prepare the environment:
$ mkdir ~/.npm-packages
$ sudo apt-get install gcc g++ make nodejs
$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt-get update && sudo apt-get install yarn
$ npm config set prefix .npm-packages
I have also added the next lines to ~/.bashrc
:
$ cat >>.bashrc <<EOL
alias ls='/bin/ls -lahrt --color'
export NPM_PACKAGES="${HOME}/.npm-packages"
export NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath`
# command
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
EOL