Nodejs InstallNodejs Install

As a developer make development environment like a pro, set up and use different versions of Node.js simultaneously on Windows, macOS and Linux (Ubuntu).

What is the need for multiple versions and why use a tool to install and set up?

As a developer, you might be working on and maintaining several frontend/server-side projects which require different Node.js runtime versions or build dependencies.

One way is to use docker and have a complete development environment containerized, which comes with its advantages and disadvantages. Another way is to simply use a tool that can help you easily install and switch different versions of node.js.

How to do it?

There are several ways to install Node.js on your development machine. I am going to share the ways to set up the top 4 tools.

  1. Using nvm

    nvm is a (most famous) version manager for node.js, designed to be installed per user, and invoked per shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.

    Linux/MacOS

    • To install the node version manager, use any of the below commands
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
    
    # or 
    
    wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

    Replace version 'v0.39.3' with the latest stable from releases. For M1/M2 chips some changes might be needed, you can refer here for troubleshooting.

    • Using nvm is super easy, to install the latest/stable version of Node.js. You can find the current latest or stable version from here or simply use the below command.
    nvm install --lts
    • Using the installed Node.js for the current terminal session:
    nvm use --lts
    • Setting the latest installed node as the terminal/system default (assuming my default version is 18.14)
     nvm alias default stable

    Windows

    Install on Windows is not easy as it is on linux. We need to use coreybutler/nvm-windows, which is not identical to nvm-sh/nvm but offers a similar cli for Windows. Its requires Administrator access for installation and some usage, but even Microsoft suggests to use the same.

    To use just download the latest version exe from the releases.

  2. Using Volta

    Volta is a new universal node package manager built in Rust and comes with dependency free static binary.

    Linux/MacOS

    • On most Unix systems including macOS, you can install Volta with a single command:
    curl https://get.volta.sh | bash
    • Using volta is easy as nvm. You can install the latest or LTS version of Node.js using the below commands:
    # for LTS
    volta install node
    
    # for latest
    volta install node@latest
    
    # for any other version
    volta install node@18.14.0
    • Similar to nvm you can pin node versions by using the below commands:
    volta pin node@18.14.0
    
    volta pin yarn@3.4.1
    
    # check using
    node --version # 18.14.0
    
    yarn --version # 3.4.1

    Windows

    For Windows, download the latest .msi from releases and run the Windows installer. For any reference refer to here.

  3. Using n

    n is also one of the oldest version managers for node.js. It is written as a BASH script and is relatively simpler than other version managers.

    Linux

    • You can install n by using the below commands:
    # make a cache folder (if missing) and take ownership
    sudo mkdir -p /usr/local/n
    sudo chown -R $(whoami) /usr/local/n
    
    # make sure the required folders exist (safe to execute even if they already exist)
    sudo mkdir -p /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
    
    # take ownership of Node.js install destination folders
    sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
    
    # finally install using the below script
    curl -fsSL https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s lts
    
    npm install -g n

    For any references, check out the installation guide.

    • Using n is similar to other version managers, to install LTS or any specific version run the below commands:
    # for lts
    n lts
    
    # for 18.14.0
    n 18.14.0
    
    
    ### MacOS
    
    - On macOS with Homebrew you can install the n formula.
    ```bash
    brew install n
    • Or on macOS with MacPorts you can install the n port:
    port install n
    • On Linux and macOS, n-install allows installation directly from GitHub; for instance:
    curl -L https://bit.ly/n-install | bash

    Windows

    Sorry, n does not work in native shells on Microsoft Windows.

  4. Using fnm

    fnm is also built in Rust but it's a little bit older than volta.

    Linux/MacOS

    • To install simply run the below command:
    curl -fsSL https://fnm.vercel.app/install | bash

    For references, check out this

    • To install lts or any specific version run:
    fnm install v18.4.0
    
    fnm install --lts

    Windows

    For installing on Windows simply download the latest fnm-windows.zip from releases, extract and install the exe.

Debugging

  • Curl SSL failure: You can install all required certificates or disable SSL verification temporarily. Example as below:
# curl: (60) SSL certificate problem: unable to get local issuer certificate
# More details here: https://curl.se/docs/sslcerts.html

curl --insecure https://get.volta.sh | bash
  • NVM failing to install any version with below error, its due to same ssl error. Disable by add -k in ~/.curlrc. Refer to this.
# nvm install --lts
# Version '' (with LTS filter) not found - try `nvm ls-remote --lts` to browse available versions.
  • NPM install fails with npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY. Fix with disabling ssl for npm. Refer to this.
npm config set strict-ssl false
npm config set registry http://registry.npmjs.org/  

References: