How to Keep Node.js and NVM Installed in WSL Forever

If you’re using Windows Subsystem for Linux (WSL) to write code and work with Node.js, you might have run into this annoying issue:

You install Node.js and NVM in WSL… everything works — until you close the terminal.
Next time you open WSL, it’s gone.

Sound familiar? That’s because WSL can sometimes forget your setup if it’s not installed the right way.

In this guide, I’ll walk you through how to permanently install NVM and Node.js in WSL, step by step — even if you’re not super technical.


🧠 What’s Happening Behind the Scenes

WSL is like a mini version of Linux running inside Windows. But it can get confused if:

  • You have Node.js already installed on Windows
  • You install tools like NVM (Node Version Manager) the usual way in WSL

This causes two main problems:

  1. WSL accidentally uses the Windows version of Node.js
  2. Your NVM install disappears when you close the terminal

✅ How to Fix It (The Simple Way)

Here’s what worked for me — and should work for you too.


🔹 Step 1: Clean Out the Confusion

Before anything else, tell WSL to ignore the Windows version of Node.js:

  1. Open your WSL terminal (Ubuntu)
  2. Type this command exactly as it is:
nano ~/.bashrc
export PATH=$(echo "$PATH" | tr ':' '\n' | grep -vE '/mnt/c/.*(nvm|nodejs)' | tr '\n' ':' | sed 's/:$//')
  1. Save and exit (Ctrl+O, then Enter, then Ctrl+X)
  2. Reload WSL settings:
source ~/.bashrc

This makes sure WSL won’t try to use the Windows version of Node.js again.


🔹 Step 2: Install NVM the Right Way

  1. Run this command to install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  1. Then activate NVM:
source ~/.bashrc
  1. Now install Node.js using NVM:
nvm install 18
nvm alias default 18
  1. Check your Node.js version:
node -v

It should show something like v18.XX.X.


🔹 Step 3: Make It Stick (Even After Restart)

To keep this setup permanent:

  1. Make sure WSL always loads your settings by creating a file:
nano ~/.bash_profile
  1. Paste this into it:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  1. Save and exit.

Now, every time you open WSL, NVM and Node.js will load automatically.


✅ That’s It — You’re Done!

Close WSL, reopen it, and run:

nvm --version
node -v

If both commands work, your setup is now locked in for good.


🔁 Bonus: Clean Up Old Node.js

If WSL still shows an old version like v12, you can safely remove it:

sudo apt remove nodejs

Then run:

nvm use default

Now WSL will always use the version you installed with NVM.


🎉 Why This Matters

Using NVM makes your development environment flexible and future-proof. You can easily switch Node versions, and your setup won’t break just because you closed your terminal.

This setup is reliable, clean, and works across restarts.

Leave a Reply

Your email address will not be published. Required fields are marked *

fifteen − two =

This site uses Akismet to reduce spam. Learn how your comment data is processed.