I needed an updated node.js
on my WSL … for some reason ubuntu 20 has quite an old version.
First, as sudo, I removed all the old versions to avoid any conflicts:
apt purge nodejs
apt auto-remove
The purge command completely removes it, while the autoremove removes any dependencies. That should be optional, because those should work once we install the new node.js
versions, but YMMV.
Next, install the node.js
installer, called nvm
for node version manager
To begin, set up your install location. For a single install it should be in ~/.nvm
but for a site-wide install, you might want to install nvm
in /usr/local/nvm
.
Put these lines in your ~/.bashrc
export NVM_ROOT_DIR=$HOME/.nvm
# export NVM_ROOT_DIR=/usr/local/nvm ## << use this version if you are doing a site wide install
export NVM_DIR=$HOME/.nvm
[ -s "$NVM_ROOT_DIR/nvm.sh" ] && . "$NVM_ROOT_DIR/nvm.sh"
[ -s "$NVM_ROOT_DIR/bash_completion" ] && . "$NVM_ROOT_DIR/bash_completion"
NVM_BIN=$HOME/.nvm/versions/node/v23.3.0/bin ## << you may need to change this line
NVM_INC=$HOME/.nvm/versions/node/v23.3.0/include/node ## << you may need to change this line
export PATH=$NVM_DIR:$NVM_BIN:$PATH
Download nvm
and get the files you want:
git clone https://github.com/nvm-sh/nvm.git
mkdir -p $NVM_ROOT_DIR
cp nvm/nvm.sh nvm/bash_completion $NVM_ROOT_DIR
Now, reload your bash settings
source ~/.bashrc
You should be able to run the nvm
commands:
which nvm
nvm ls
To install node.js
use nvm install
nvm install node
If you see this error:
wsl -bash: node: cannot execute binary file: Exec format error
then try installing a different version
node install --lts
For me, using the default node install node
didn’t work because of the binary file Exec format error, while using node install --lts
worked like a charm!