Skip to content

Instantly share code, notes, and snippets.

@1eedaegon
Last active July 14, 2023 04:48
Show Gist options
  • Select an option

  • Save 1eedaegon/cb18c59f3c2f83a33aeb4e6a4e174b29 to your computer and use it in GitHub Desktop.

Select an option

Save 1eedaegon/cb18c59f3c2f83a33aeb4e6a4e174b29 to your computer and use it in GitHub Desktop.
How to install nodejs on wsl2

How to install node.js on ubuntu

Simplify

1. Remove legacy node.js

# Check legacy node version
> /usr/bin/node -v
v8.10.0

# remove old nodejs
> sudo apt purge nodejs 
# or sudo apt-get purge --auto-remove nodejs

# check removed nodejs
> /usr/bin/node -v
zsh: no such file or directory: /usr/bin/node

2. Install nvm for manage nodejs version

# Install and restart shell
> curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
> source ~/.bashrc

# Check nvm
> nvm --version
0.38.0

> nvm ls
->       system
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)
nvm_list_aliases:36: no matches found: /home/[HOME]/.nvm/alias/lts/*

3. Install nodejs each version

# 1. Latest version
> nvm install node

# 2. Lts version
> nvm install --lts

# 3. Specific version(v14)
> nvm install --lts=Fermium

# 4. Check installed node.js
> nvm ls
->     v14.18.1
       v16.13.0
        v17.0.1
         system
default -> node (-> v17.0.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v17.0.1) (default)
stable -> 17.0 (-> v17.0.1) (default)

# 5. Check current node.js version
> node -v
v14.18.1

> npm -v
6.14.15

> which node && which npm
/home/[HOME]/.nvm/versions/node/v14.18.1/bin/node
/home/[HOME]/.nvm/versions/node/v14.18.1/bin/npm

# etc: search node version using nvm 
> nvm ls-remote --lts
...
...
       v14.18.0   (LTS: Fermium)
->     v14.18.1   (Latest LTS: Fermium)
       v16.13.0   (Latest LTS: Gallium)

4. Use specific node version

> nvm list
->     v14.18.1
       v16.13.0
        v17.0.1
         system
default -> node (-> v17.0.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v17.0.1) (default)
stable -> 17.0 (-> v17.0.1) (default)
lts/* -> lts/gallium (-> v16.13.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.7 (-> N/A)
lts/fermium -> v14.18.1
lts/gallium -> v16.13.0

>  nvm use --lts=fermium
Now using node v14.18.1 (npm v6.14.15)

5. Set default node version

> nvm alias default v14
default -> v14 (-> v14.18.1)

ref

ms docs - Install Node.js on Windows Subsystem for Linux (WSL2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment