Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Installing node via windows subsystem for linux

Windows 10 Fall Creators Update - Installing Node.js on Windows Subsystem for Linux (WSL)

Windows just released the windows subsystem for linux feature to the public with its latest windows fall creator update, if you are not familiar with this feature it allows you to run linux binaries natively on windows - F.A.Q.

Enabling WSL

The feature is not enabled by default and you need to activate it, you can do it via powershell (with admin rights):

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Or you can open: Control-Panel -> Programs -> Turn Windows feature on ro off, and click the "windows subsystem for linux" button.

Now to install your linux version, again two options, you can either install from Windows store (search linux) or you can turn on developer mode and open it via the command line: Open Settings -> Update and Security -> For developers, and Select the "Developer Mode" radio button.

Installing linux (ubuntu)

For those who are not familiar with linux, fragmentation if part of the fun - there are a lot of linux distributions and package management systems for it, the default installation in WSL is ubuntu and I recommend to keep it as it one of the most popular distribution around. If you need different distribution the easiest why is to install it from the windows store.

reference

After installing the destribution via windows store click the "launch" button to launch the cli based installer wizard, the ubuntu image will automatically download and you'll be prompt to enter username and password.

Now you can run CMD.exe and type:

bash

That's it, you're running ubuntu on windows!

If you found yourself in need to reset/reinstall the ubuntu machine you can use wsl.exe:

wsl.exe --unregister Ubuntu

reference (wsl.exe tool also provide cli tools for WSL managment).

Installing Node.js

First We'll start by updating linux, for those of you that are not familiar with linux this require running the process as root by add the sudo command before the command we need to execute:

sudo apt-get update
sudo apt-get upgrade

You'll need to enter your user password to proceed, if you prompt just press y (or yes) to continue.

We also need to install some basic build tool for node-gyp - node binaries build tool.

sudo apt-get install build-essential

I do not recommend on the "official" to install node, node rapid development life cycle (and the entire ecosystem for that matter) leads more then often for the need to have tight control over the node binaries version, or more likely to have to project running on different versions of node (such as LTS versions).

NVM tool gives you the ability to install and use multiple version of node, and prevent the (bad) usage of sudo for running node application, installation is via the command line:

# This command install version 0.35.0, check the NVM reference it there are newer versions. 
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash

Restart your terminal after install.

Now you can install the latest the latest LTS version of node:

nvm install --lts

To update node just run the command again.

One thing you'll have to remember when use NVM is that you'll need to explicitly specify the node version you want to use (installing does it automatically on the end of the install), so next time you'll login to ubuntu you'll need to run the command:

nvm use --lts

You can also add this command to the end of your .bash_profile file:

echo "nvm use --lts" >> .bash_profile

Developing

Your windows hard disk drivers are mounted in linux under /mnt/<drive letter>/ so you can access any folder via linux and run whatever command you need in order to get your project up and running, missing needed tools can be installed via npm or apt-get for system tools.

IDE terminal integration support is still on its early days, vscode offers support with minimal configuration, guide. If your IDE just point to the executable (like cmd.exe) check if you can point it to c:\Windows\System32\bash.exe.

@Seefer

This comment has been minimized.

Copy link

Seefer commented Apr 28, 2019

I constantly get this when running that nvm curl script :(

➜ ~ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12461 100 12461 0 0 61688 0 --:--:-- --:--:-- --:--:-- 61688
=> nvm is already installed in /home/seefer/.nvm, trying to update using git
=> => Compressing and cleaning up git repository

=> Appending nvm source string to /home/seefer/.zshrc
=> Appending bash_completion source string to /home/seefer/.zshrc
net.js:268
err = this._handle.open(fd);
^

Error: EINVAL: invalid argument, uv_pipe_open
at new Socket (net.js:268:24)
at createWritableStdioStream (internal/process/stdio.js:191:18)
at process.getStdout [as stdout] (internal/process/stdio.js:20:14)
at console.js:454:19
at NativeModule.compile (internal/bootstrap/loaders.js:364:7)
at Function.NativeModule.require (internal/bootstrap/loaders.js:176:18)
at setupGlobalConsole (internal/bootstrap/node.js:407:41)
at startup (internal/bootstrap/node.js:142:7)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion

The upshot is even though ~/.bashrc contains the lines to run nvm.sh and the .nvm directory exists on my system, typing nvm always results in command not found.

@Seefer

This comment has been minimized.

Copy link

Seefer commented Apr 28, 2019

FYI

After much Googling, I found nothing but saw a chat unrelated to NVM about ensuring scripts are marked as executable. On a whim, I went into the .nvm folder and typed chmod +x nvm.sh. Now things seem to be working so far. Unsure why I had to do this on my system when all the instructions I see on the web about that cover installing nvm on a WSL system do not cover this manual step.

@csnowbar

This comment has been minimized.

Copy link

csnowbar commented Oct 9, 2019

Wow! Your instructions worked flawlessly. Thank you so much for taking the time to put this gist together!!!

@psychicbologna

This comment has been minimized.

Copy link

psychicbologna commented Oct 12, 2019

Thanks so much for this.

@adames

This comment has been minimized.

Copy link

adames commented Oct 15, 2019

After the command curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash, I received an error nvm is already installed and simply had to source ~/.bashrc to pick up the installations's new paths (for any users who had something similar).

@noygal

This comment has been minimized.

Copy link
Owner Author

noygal commented Oct 25, 2019

Thanks for the comments, I'm glad to see that this post is useful.

It has been a while since I wrote this post (WSL was on beta), so it needed some updates:

  • lxrun.exe is deprecated, replaced it with wsl.exe
  • Update NVM to v0.35.0
  • Change Node.js version to LTS , currently it is v12.13.0
  • Adding nvm use --lts to .bash_profile

Also I see that most of the ppl encounter problems with the NVM installation, I recommend to first see if there is a newer version of NVM - if so install that, if that didn't help try to check NVM repo/docs for known issues.

@thelazyliz

This comment has been minimized.

Copy link

thelazyliz commented Nov 9, 2019

Thank you, this worked perfectly!

@minlare

This comment has been minimized.

Copy link

minlare commented Nov 29, 2019

In order to get this working for me I first had to uninstall my current version of node using:
sudo apt-get purge --auto-remove nodejs

Works perfectly otherwise, thanks!

@calvimor

This comment has been minimized.

Copy link

calvimor commented Feb 12, 2020

If you are using zsh, must add following in ~/.zshrc

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

@fakesquid

This comment has been minimized.

Copy link

fakesquid commented Feb 23, 2020

This works great!

@aderchox

This comment has been minimized.

Copy link

aderchox commented Mar 13, 2020

All the steps worked just fine! Thank you very much.

@pedropcruz

This comment has been minimized.

Copy link

pedropcruz commented Mar 17, 2020

If you are using zsh, must add following in ~/.zshrc

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

This gives an error:
image
I really cant switch node version. Everytime i do, if i open a new terminal I lost all configuration (using WSL on Windows)

Many thanks

@dompascal

This comment has been minimized.

Copy link

dompascal commented Mar 19, 2020

Splitting into 2 lines should get rid of that error.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

You will then need to run source ~/.zshrc to update ZSH.

@ameft

This comment has been minimized.

Copy link

ameft commented Mar 25, 2020

I followed this tutorial and installed node 10 (over v8) with just this two commands:
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
$ sudo apt-get install -y nodejs

@salvimateus

This comment has been minimized.

Copy link

salvimateus commented Apr 9, 2020

Thank you!

@salvimateus

This comment has been minimized.

Copy link

salvimateus commented Apr 9, 2020

If you are using zsh, must add following in ~/.zshrc

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

WOW, I needed this! Thank you!

@schnoggo

This comment has been minimized.

Copy link

schnoggo commented Apr 13, 2020

Yup. chmod solved my install issue.
Should probably be added to the install script.

@hscheuerle

This comment has been minimized.

Copy link

hscheuerle commented May 18, 2020

Feels much nicer than git bash, was running into issues with it after what was probably windows updates.
Enabling wsl with the scripts in Microsoft's guide worked better for me.

@pnosov

This comment has been minimized.

Copy link

pnosov commented May 20, 2020

"Your windows hard disk drivers are mounted in linux..."
It's "drives", not "drivers".

@marciojg

This comment has been minimized.

Copy link

marciojg commented May 23, 2020

Hi guys, I did have this problem. Anybody did have too? And anybody know how to solve it? Thanks

devel@DESKTOP-GMOMRKS:~$ nvm install --lts
Installing latest LTS version.
Downloading and installing node v12.16.3...
Local cache found: ${NVM_DIR}/.cache/bin/node-v12.16.3-linux-x64/node-v12.16.3-linux-x64.tar.xz
Checksums match! Using existing downloaded archive ${NVM_DIR}/.cache/bin/node-v12.16.3-linux-x64/node-v12.16.3-linux-x64.tar.xz
mv: cannot move '/home/devel/.nvm/.cache/bin/node-v12.16.3-linux-x64/files/bin' to '/home/devel/.nvm/versions/node/v12.16.3/bin': Permission denied
mv: cannot move '/home/devel/.nvm/.cache/bin/node-v12.16.3-linux-x64/files/include' to '/home/devel/.nvm/versions/node/v12.16.3/include': Permission denied
mv: cannot move '/home/devel/.nvm/.cache/bin/node-v12.16.3-linux-x64/files/lib' to '/home/devel/.nvm/versions/node/v12.16.3/lib': Permission denied
mv: cannot move '/home/devel/.nvm/.cache/bin/node-v12.16.3-linux-x64/files/share' to '/home/devel/.nvm/versions/node/v12.16.3/share': Permission denied
Binary download failed, trying source.
Local cache found: ${NVM_DIR}/.cache/src/node-v12.16.3/node-v12.16.3.tar.xz
Checksums match! Using existing downloaded archive ${NVM_DIR}/.cache/src/node-v12.16.3/node-v12.16.3.tar.xz
$>./configure --prefix=/home/devel/.nvm/versions/node/v12.16.3 <
./configure: 4: exec: python: not found
nvm: install v12.16.3 failed!
@edboyledev

This comment has been minimized.

Copy link

edboyledev commented May 26, 2020

Flawless, fantastic, you saved me a lot of reading and hours of my day!

Thanks!

@gibsonbm

This comment has been minimized.

Copy link

gibsonbm commented Jun 24, 2020

Thank you very much, the fix worked like a charm.
Has anyone figures out why installing nodejs through apt-get doesn't always work ? I mean we are installing one single version right ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.