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.

Previous versions of these install instructions had been tested with:

Install nvm for managing Node.js versions

The reason for using nvm instead of other install types is mainly in how easy it is to have multiple versions of Node.js (if needed) without too much of extra complexity. Sometimes applications might require a certain version of Node.js to work, so having the flexibility of using specific versions can save a lot of time from you.

  1. Open new Terminal window.
  2. Run nvm installer
    • ...with either curl or wget, depending on what you have on your computer.
      • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
      • wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/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 might want/need to add the source loading line by yourself, if the automated install tool does not add it for you.)
      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
  3. If everything went well, you should now be able to reload the shell by running
    • source ~/.bashrc
    • (Another option is to open a new Terminal window/tab.)
  4. Verify installation
    • To verify that nvm has been installed, do: command -v nvm
  5. List what versions of Node are currently installed (probably none).
    • nvm ls
  6. Install latest Node.js LTS release (recommended for production usage).
    • nvm install v10.16.3
  7. Install Current Node.js Current release with latest features (for testing new feature improvements).
    • nvm install v12.10.0
  8. If you want to change the default Node version later, you can run a command to adjust it.
    • nvm alias default v10.16.3 (when you work with production quality projects) changelog
    • nvm alias default v12.10.0 (when you want to test the latest features of Node.js) changelog

It is also possible to select what Node.js version is used per project basis, by running nvm use v10.16.3 (or another version number) on a directory where your project's files are located. One way to do that is to create small Bash shell script for enabling the right environment variables when needed, so you would not have to remember what exact version was needed.

You can find a lot of packages from the npm package repository. Have a good time with the freshly installed tools.

Migrating packages from the previously installed Node.js versions

If you already have previous version of Node.js installed with nvm, you can migrate packages from previously installed Node.js versions.

  • Open new Terminal window (to make sure you have latest Node.js version active in your command line environment).
  • Linking global packages from previous version:
    • nvm reinstall-packages v10.16.2
    • nvm reinstall-packages v12.9.1

Deleting old Node.js versions

  • Look what versions are installed:
    • nvm ls
  • Delete an older version (if it is no longer used in some of your projects):
    • nvm uninstall v10.16.2
    • nvm uninstall v12.9.1

Updating outdated packages

List what (top level) packages are installed globally

npm ls -g --depth=0.

List what packages are outdated

npm outdated -g --depth=0.

Update outdated globally installed npm packages

npm update -g

Command line aliases for Bash & zsh

# -----------------------------------------------------------
# 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

After installation of Node v10.x (or similar), it can be a good idea to recompile old packages (that have compiled native extensions) to make sure that they work with the new version of Node. That can be done easily by going to a project’s root directory, and running npm rebuild:

cd PROJECT_NAME
npm rebuild
@bliotti

This comment has been minimized.

Copy link

commented Oct 12, 2017

Thank You

@llccing

This comment has been minimized.

Copy link

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

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

commented Nov 7, 2017

Thank You..!!

@d2s

This comment has been minimized.

Copy link
Owner Author

commented Nov 15, 2017

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

@jbenz

This comment has been minimized.

Copy link

commented Dec 28, 2017

Worked like a charm for Debian 9. Cheers.

@Favna

This comment has been minimized.

Copy link

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

commented Jan 4, 2018

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

@MarcusBliss

This comment has been minimized.

Copy link

commented Feb 26, 2018

  • [ ]

Yeah. it worked

@larka06

This comment has been minimized.

Copy link

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

commented May 2, 2018

Good to see that these are useful to people. 👍

@Sandhya-Bairi

This comment has been minimized.

Copy link

commented Jul 5, 2018

Thanks!!

@yar00001

This comment has been minimized.

Copy link

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

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

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

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

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

commented Aug 10, 2019

This is a fantastic reference, thanks! Worth bookmarking

@d2s

This comment has been minimized.

Copy link
Owner Author

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.

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.