Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Installing Node.js to Linux & macOS & WSL with nvm

Installing Node.js with nvm to Linux & macOS & WSL

A quick guide on how to setup Node.js development environment.

Install nvm for managing Node.js versions

nvm allows installing several versions of Node.js to the same system. Sometimes applications require a certain versions of Node.js to work. Having the flexibility of using specific versions can help.

  1. Open new Terminal window.

  2. Run nvm installer

    • … with either curl or wget, depending on what your computer has available.

      • curl -o- | bash
      • wget -qO- | bash
    • The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc). (You can add the source loading line manually, if the automated install tool does not add it for you.)

      export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
      [ -s "$NVM_DIR/" ] && \. "$NVM_DIR/" # This loads nvm
    • Another option: when you have consistent directory location between systems, following example Bash/Zsh configuration allows to load nvm when the directory exists. This allows more consistent sharing of your shell configuration between systems, improving reliability of rest of your configuration even when nvm does not exist on a specific system.

      if [ -d "$HOME/.nvm" ]; then
        # export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
        export NVM_DIR="$HOME/.nvm"
        # This loads nvm
        [ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"
        # This loads nvm bash_completion
        [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
  3. If everything went well, you should now either open a new Terminal window/tab, or reload the shell configuration by running:

    • source ~/.bashrc
  4. Verify installation

    • To check if nvm command got installed, run:
      • command -v nvm

Install Node.js with nvm

  1. List installed Node.js versions with:
    • nvm ls
  2. Install latest LTS Version of Node.js (for production quality applications)
    • nvm install v16.15.1
  3. Install latest Node.js Current release (for testing new feature improvements)
    • nvm install v18.3.0
  4. Install previous LTS release of Node.js LTS release (if you need to run older applications)
    • nvm install v14.19.2
  5. If you want to change the default Node version later, you can run a command to adjust it.
    • nvm alias default v16.15.1 changelog (for production quality applications)
    • nvm alias default v18.3.0 changelog (if you use Node.js features from the Current release)
    • nvm alias default v14.19.2 changelog (if you need to use old version of Node.js for older projects)

You can select Node.js version by running nvm use v18.3.0 (or another version number). Another alternative: create a small Bash shell script to enable the right environment variables for your project.

Read the Node.js Long Term Support (LTS) schedule to have more understanding of their release roadmap. List of all previous releases is also useful for finding details about Node.js release history.

npm package repository has a lot of packages to discover. Have a good time with the freshly installed tools.

Migrating packages from a previous Node.js version

If you already have existing Node.js version via nvm, you can migrate older packages from the installed Node.js versions.

  • Open new Terminal window (to make sure you have latest Node.js version active in your command line environment).
  • Before running next commands, remember to switch to the right version of Node with nvm use command. For example:
    • nvm use v16.15.1
    • nvm use v18.3.0
    • nvm use v14.19.2
  • Linking global packages from previous version:
    • nvm reinstall-packages v16.14.2
    • nvm reinstall-packages v14.19.1

Deleting old Node.js versions

  • Check installed Node.js versions with:
    • nvm ls
  • Delete an older version (if you don't use it in some of your projects):
    • nvm uninstall v16.14.2
    • nvm uninstall v14.19.1

Updating outdated packages

List of globally installed top level packages

npm ls -g --depth=0.

List outdated global packages

npm outdated -g --depth=0.

Updating all globally installed npm packages

npm update -g

CLI aliases for Bash & Zsh environments

Example configuration for your Bash & Zsh command line environments.

# -----------------------------------------------------------
# npm helpers
# -----------------------------------------------------------

# List what (top level) packages are installed globally
alias list-installed-npm-packages="npm ls -g --depth=0."

# List what globally installed packages are outdated
alias list-outdated-npm-packages="npm outdated -g --depth=0."

# Update outdated globally installed npm packages
alias update-npm-packages="npm update -g"

Fixing old package versions

If you have older npm packages with compiled native extensions, recompiling native extensions can improve compatibility with the new Node.js version. Go to your project’s root directory, and run npm rebuild command.

npm rebuild

Notes about this documentation

@d2s tested older versions of these install instructions with:


If you have improvement suggestions to make these instructions simpler & better, post a comment under the original Gist by @d2s with your documentation improvement suggestions. If you are reading a forked version of the document, check the original Gist for a more recent instructions.

Copy link

inidaname commented May 20, 2021

Thank you this, I tried nvm install --lts
and I got this error

Installing latest LTS version.
Downloading and installing node v14.16.1...
Warning: Failed to create the file                                             
Warning: /home/hassansani/.nvm/.cache/bin/node-v14.16.1-linux-x64/node-v14.16.1
Warning: -linux-x64.tar.xz: Permission denied
curl: (23) Failure writing output to destination

Binary download from failed, trying source.
grep: /home/hassansani/.nvm/.cache/bin/node-v14.16.1-linux-x64/node-v14.16.1-linux-x64.tar.xz: No such file or directory
Provided file to checksum does not exist.
Binary download failed, trying source.
Warning: Failed to create the file                                             
Warning: /home/hassansani/.nvm/.cache/src/node-v14.16.1/node-v14.16.1.tar.xz: 
Warning: Permission denied
curl: (23) Failure writing output to destination

Binary download from failed, trying source.
grep: /home/hassansani/.nvm/.cache/src/node-v14.16.1/node-v14.16.1.tar.xz: No such file or directory
Provided file to checksum does not exist.

looks like a permission issue, do you have root privileges when executing the command?
try executing sudo -i first and then try again

Found a possible solution, it was a curl issue. Refer to issue here nvm-sh/nvm#2504 for more information

Copy link

tohagan commented Aug 21, 2021

If you have node / nvm installed in Windows and then run WSL ... you may end up running Node or NVM from Windows.

To fix this ... remove Windows PATH folders from your WSL environment by ...

  • Confirming that you have Windows paths in WSL
    $ echo $PATH | tr : '\n'

For Windows build LOWER than 17713

  • Open Regedit at HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss where you'll see a GUI key for each WSL Linux distro
  • Update <distro>/Flags value from 0x07 => 0x05 (WSL1) - check the initial value carefully
  • Update <distro>/Flags value from 0x15 => 0x13 (WSL2) - check the initial value carefully
  • Restart the lxssmanager service

For Windows build HIGHER than 17713

  • Inside WSL ... create a /etc/wsl.conf file containing ...
enabled=false # enable launch of Windows binaries; default is true
appendWindowsPath=false # append Windows path to $PATH variable; default is true
  • Restart WSL and confirm that your PATH is now free of Windows paths:
    $ echo $PATH | tr : '\n'

Copy link

d2s commented Oct 6, 2021

Thanks for the tips, @tohagan. 👍
I haven't been using WSL in a long while, so part of the instructions are probably outdated related to that environment.

If you have node / nvm installed in Windows and then run WSL ... you may end up running Node or NVM from Windows.

Copy link

MwigoJonathanMark commented Mar 29, 2022

Downloading ############################################################################ 100.0% Computing checksum with sha256sum Checksums matched! $>./configure --prefix=/home/mwigojm/.nvm/versions/node/v16.14.2 < Node.js configure: Found Python 3.10.3... Traceback (most recent call last): File "/home/mwigojm/.nvm/.cache/src/node-v16.14.2/files/./configure", line 27, in <module> import configure File "/home/mwigojm/.nvm/.cache/src/node-v16.14.2/files/", line 14, in <module> import bz2 File "/usr/local/lib/python3.10/", line 17, in <module> from _bz2 import BZ2Compressor, BZ2Decompressor ModuleNotFoundError: No module named '_bz2' nvm: install v16.14.2 failed!
how can i solve this, tried to pip install bz2 but its not available

Copy link

suvel-the-dev commented May 24, 2022

The article was very helpful, thank you.

Copy link

JackGowan commented Jun 9, 2022

Thank you - these notes were extremely helpful.
Note: I also had an issue with curl

Copy link

ssbarnea commented Jun 19, 2022

I am pretty sure that nvm install script does not automatically change shell rc files, you need to add the required lines yourself.

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