Skip to content

Instantly share code, notes, and snippets.

@d2s
Last active March 13, 2024 12:09
Show Gist options
  • Save d2s/372b5943bce17b964a79 to your computer and use it in GitHub Desktop.
Save d2s/372b5943bce17b964a79 to your computer and use it in GitHub Desktop.
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. (If you already had it, remember to update to more recent versions.)

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

      • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
      • wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/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="$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
    • 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 v20.11.1
  3. Install latest Node.js Current release (for testing new feature improvements)
    • nvm install v21.6.2
  4. If you want to change the default Node version later, you can run a command to adjust it.
    • nvm alias default v20.11.1 changelog (for production quality applications)
    • nvm alias default v21.6.2 changelog (if you use Node.js features from the Current release)

You can select Node.js version by running nvm use v20.11.1 (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 v20.11.1
    • nvm use v21.6.2
  • Linking global packages from previous version:
    • nvm reinstall-packages v20.3.1
    • nvm reinstall-packages v18.16.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 v20.3.1
    • nvm uninstall v18.16.1

Updating outdated packages

Warning: Make sure you have latest nvm version installed, just in case. Update instructions of it in the beginning of this article. nvm v0.39.2 version of fixes potential problems that might cause older node environments to break if accidentally installing npm v9 to those.

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

Warning: This might be too risky approach. Instead, it is better to update packages individually, as that avoids the risk of accidentally updating npm package to incompatible versions. npm v9 installation to older incompatible Node.js environments is known to cause problems (potentially breaking down the development environment), so it's better be safe than sorry.

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.

@d2s
Copy link
Author

d2s commented Nov 9, 2022

@favna @ComicWriter Homebrew doesn't work on that old macOS, because the minimum supported version is macOS Big Sur (11) (or higher) (according to the official documentation: https://docs.brew.sh/Installation section).

Better option is to use web-based services like CodeSandbox or other similar services, so you don't have to install anything. It's likely one of the only options (besides of running Node.js on some server etc.).

@ComicWriter
Copy link

@favna @d2s Thanks for the replies. I'm in the middle of taking a course to learn the back end, with installing node.js as a first step. Instead, I'll practice my coding until I can get a computer I can work with. Thanks for the sandbox suggestion: it will definitely help :)

@rikumo
Copy link

rikumo commented Apr 9, 2023

Thank you for putting this together. Works for me.

@ArslanYM
Copy link

Works for me, You might run into this while installing nvm too. The given solution works.

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