nvm
to Linux & macOS & WSL
Installing Node.js with A quick guide on how to setup Node.js development environment.
nvm
for managing Node.js versions
Install 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.
-
Open new Terminal window.
-
Run nvm installer
-
… with either
curl
orwget
, depending on what your computer has available.curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/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
-
-
If everything went well, you should now either open a new Terminal window/tab, or reload the shell configuration by running:
source ~/.bashrc
-
Verify installation
- To check if nvm command got installed, run:
command -v nvm
- To check if nvm command got installed, run:
nvm
Install Node.js with - List installed Node.js versions with:
nvm ls
- Install latest LTS Version of Node.js (for production quality applications)
nvm install v16.15.1
- Install latest Node.js Current release (for testing new feature improvements)
nvm install v18.3.0
- Install previous LTS release of Node.js LTS release (if you need to run older applications)
nvm install v14.19.2
- If you want to change the default Node version later, you can run a command to adjust it.
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.
cd PROJECT_NAME
npm rebuild
Notes about this documentation
@d2s tested older versions of these install instructions with:
- Debian 11.2
- Debian 10
- Ubuntu on WSL (Windows Subsystem for Linux)
- Ubuntu 18.04 LTS
- Ubuntu 17.04
- Ubuntu 16.04 LTS
- Ubuntu 14.04.3 LTS
- macOS 10.14.6 (Mojave)
- macOS 10.13.6 (High Sierra)
- macOS 10.12.6 (Sierra)
- OS X 10.11.6 (El Capitan)
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.
thank you this is the only complete set of instructions on the whole internet. A BIG THANK YOU