Skip to content

Instantly share code, notes, and snippets.

Embed
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- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
      • wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | 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.sh" ] && \. "$NVM_DIR/nvm.sh" # 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.sh" ] && \. "$NVM_DIR/nvm.sh"
      
        # This loads nvm bash_completion
        [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
      fi
  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 v12.18.0
  3. Install latest Node.js Current release (for testing new feature improvements)
    • nvm install v14.4.0
  4. Install previous LTS release of Node.js LTS release (if you need to run older applications)
    • nvm install v10.21.0
  5. If you want to change the default Node version later, you can run a command to adjust it.
    • nvm alias default v12.18.0 changelog (for production quality applications)
    • nvm alias default v14.4.0 changelog (if you use Node.js features from the Current release)
    • nvm alias default v10.21.0 changelog (if you need to use old version of Node.js for older projects)

You can select Node.js version by running nvm use v12.18.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 v12.18.0
    • nvm use v14.4.0
    • nvm use v10.21.0
  • Linking global packages from previous version:
    • nvm reinstall-packages v12.17.0
    • nvm reinstall-packages v13.14.0
    • nvm reinstall-packages v14.3.0
    • nvm reinstall-packages v10.20.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 v12.17.0
    • nvm uninstall v13.14.0
    • nvm uninstall v14.2.0
    • nvm uninstall v10.20.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.

cd PROJECT_NAME
npm rebuild

Notes about this documentation

@d2s tested older versions of these install instructions with:

Contributions

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.

@bliotti

This comment has been minimized.

Copy link

bliotti commented Oct 12, 2017

Thank You

@llccing

This comment has been minimized.

Copy link

llccing commented Oct 21, 2017

when installed nvm, must source ~/.bashrc, otherwise run "nvm ls", will alert ,common not defined

@d2s

This comment has been minimized.

Copy link
Owner Author

d2s commented Oct 24, 2017

@llccing Oh, I had forgotten to mention that part of the original installation,
since I have mostly used this as a reminder about upgrade steps. Thanks for mentioning. 👍

@aksayps

This comment has been minimized.

Copy link

aksayps commented Nov 7, 2017

Thank You..!!

@d2s

This comment has been minimized.

Copy link
Owner Author

d2s commented Nov 15, 2017

Happy to see that people are finding these notes useful. 👍

@jbenz

This comment has been minimized.

Copy link

jbenz commented Dec 28, 2017

Worked like a charm for Debian 9. Cheers.

@Favna

This comment has been minimized.

Copy link

Favna commented Jan 2, 2018

  1. If everything went well, open new Terminal window/tab.

Concerning this, for people who SSH'd into a device/raspberrypi/vps it is much easier to just to source ~/.bashrc to reload it than actually opening a new Terminal window/tab. This is probably worth mentioning in the guide.

@d2s

This comment has been minimized.

Copy link
Owner Author

d2s commented Jan 4, 2018

Updated the guide based on your feedback. Thanks for the tip. 👍

@MarcusAfolabi

This comment has been minimized.

Copy link

MarcusAfolabi commented Feb 26, 2018

  • [ ]

Yeah. it worked

@larka06

This comment has been minimized.

Copy link

larka06 commented Apr 28, 2018

thank you this is the only complete set of instructions on the whole internet. A BIG THANK YOU

@d2s

This comment has been minimized.

Copy link
Owner Author

d2s commented May 2, 2018

Good to see that these are useful to people. 👍

@Sandhya-Bairi

This comment has been minimized.

Copy link

Sandhya-Bairi commented Jul 5, 2018

Thanks!!

@yar00001

This comment has been minimized.

Copy link

yar00001 commented Aug 10, 2018

Adding the nvm loading scrip to my .zshrc gives me this error:

~ λ source ~/.zshrc
nvm is not compatible with the npm config "prefix" option: currently set to "/local/home/username/.nvm/versions/node/v4.4.5"
Run `nvm use --delete-prefix v4.4.5 --silent` to unset it.
@jonkeller

This comment has been minimized.

Copy link

jonkeller commented Nov 1, 2018

Yarn doesn't seem to like the current LTS version:

yarn install v1.12.1
$ node build-system/check-package-manager.js
Detected node version v10.13.0 (latest LTS).
Detected gulp version 3.9.1.
Detected yarn version 1.12.1 (stable). Installing packages...
[1/5]   Validating package.json...
error amp-html@0.1.0: The engine "node" is incompatible with this module. Expected version "^8.0.0". Got "10.13.0"
error Found incompatible module
@d2s

This comment has been minimized.

Copy link
Owner Author

d2s commented Nov 16, 2018

@jonkeller,

Yarn doesn't seem to like the current LTS version

Not sure about Yarn, since I haven't been using it in a while.
You might want to install the latest Yarn release from GitHub.
https://github.com/yarnpkg/yarn/releases

@begueradj

This comment has been minimized.

Copy link

begueradj commented Jan 28, 2019

Thank you for sharing.
Nowadays the default alias for the Node.js installed version is automatically created after the Node.js specific version is installed:

Downloading and installing node v11.8.0...
Downloading https://nodejs.org/dist/v11.8.0/node-v11.8.0-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v11.8.0 (npm v6.5.0)
Creating default alias: default -> v11.8.0

Billal Begueradj

@hawk8

This comment has been minimized.

Copy link

hawk8 commented Jul 15, 2019

I tried to install v.10.16.0, and it looks like below:

$ nvm install v10.16.0
Downloading and installing node v10.16.0...
Downloading https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-x64.tar.xz...
##################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
N/A: version "v10.16.0 -> N/A" is not yet installed.

You need to run "nvm install v10.16.0" to install it before using it.

My first question is - why did it not install it in the first place?

If I try to do as they say - run "nvm install v10.16.0" to install it again, then I get error messages such as below:

mv: cannot move '/home/mydomain/.nvm/.cache/bin/node-v10.16.0-linux-x64/files/bin' to '/home/mydomain/.nvm/versions/node/v10.16.0/bin': Directory not empty

It was a clean install (nothing there before).
Any advice?

@najarvg

This comment has been minimized.

Copy link

najarvg commented Aug 10, 2019

This is a fantastic reference, thanks! Worth bookmarking

@d2s

This comment has been minimized.

Copy link
Owner Author

d2s commented Aug 11, 2019

This is a fantastic reference, thanks! Worth bookmarking

Thanks for the feedback. :)
Mainly been documenting the details as a reminder for myself,
and always happy to hear that it has been useful for other people too.

@d2s

This comment has been minimized.

Copy link
Owner Author

d2s commented Feb 2, 2020

Rewrote a lot of the instructions to be more inclusive and understandable.

Git commit

fix: clarify text structure

  • Clarify text structure
    • Reduce unclear words
    • Reduce duplicate words
  • Remove mentions about "easy" steps
  • Remove Ubuntu version names because those can be seen as offensive
  • Check for potential issues with
    • vale
    • alex

More details:

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.