How to install Node.js the right way
November 30th, 2022

For better or worse, JaveScript has become one of the most ubiquitous languages in software, and Web3 has not escaped its reach. In fact, most of the tools you will need for Web3 development have been built using JavaScript. For this reason, there’s no way around setting up a JavaScript runtime environment like Node.js on your development machine.

However, instead of setting up Node.js “directly,” we’ll use the so-called Node Version Manager (nvm) for our installation. The Node Version Manager is a bash script to manage multiple active Node.js versions. In particular, it allows you to easily install and uninstall any specific Node.js version you want to use or test.

In this post, you’ll learn the installation process for the three major operating systems: Ubuntu, macOS, and Windows.

By installing Node.js, you’ll also automatically install its default package manager called npm. This will allow you to easily install many tools you’ll need for full-stack Web3 development, e.g., popular frameworks like Hardhat or React.

Let’s dive right in!

Installing Node.js on Ubuntu

To install Node.js and npm using nvm on your Ubuntu system, perform the following steps:

1. Installing nvm

To download and install nvm, you need to run the install script. You may either download and run this script manually or use one of the following two commands:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

The install script will then clone the nvm repository to ~/.nvm, and attempt to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

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

If something goes wrong during this process, check out nvm’s official README. It has an excellent troubleshooting section covering all of the common installation issues.

Once the installation is completed, restart your terminal and verify that everything worked by typing:

nvm --version

If this prints a version number, you’re good to go!

2. Installing Node.js and npm

Now that nvm is installed, you can install a specific version, e.g., version 16.14.0, via:

nvm install 16.14.0

To install the current stable LTS release of Node.js (recommended for production applications), use the following command instead:

nvm install --lts

To install the current release of Node.js (for testing the latest Node.js features and improvements), you can use the following:

nvm install node 

Once one (or more) of the above installations are completed, you can verify that everything works by printing the version of the Node.js installation that’s currently in use:

node --version

3. Using nvm to organize your Node.js installations

To list all installed Node.js versions, run:

nvm ls

The output should look something like this:

      v12.22.10
       v14.19.0
       v16.13.2
->     v16.14.0
        v17.5.0
         system
default -> 16 (-> v16.14.0)
node -> stable (-> v17.5.0) (default)
stable -> 17.5 (-> v17.5.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/gallium (-> v16.14.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.10
lts/fermium -> v14.19.0
lts/gallium -> v16.14.0

In the fourth line of the above example output, you can see that the Node.js version that’s used in my current shell session is 16.14.0. Furthermore, you can see that my default is also set to 16.14.0, i.e., whenever I open a new shell, 16.14.0 will be used in that shell.

You can change the active version with the “use” command. For example, I could switch from my default version to 14.19.0 via:

nvm use 14.19.0 

However, this would only activate 14.19.0 in the current shell session. If, instead, I would like to make 14.19.0 my new default, I could achieve that using the following command:

nvm alias default 14.19.0

Finally, to uninstall a Node.js version that you no longer want to use (e.g., 12.22.10), run:

nvm uninstall 12.22.10

Installing Node.js on macOS

The installation instructions for macOS are the same as on Ubuntu, with only one exception: You must manually install the Xcode command line tools first.

To do that, open a terminal and run the following command:

xcode-select --install

When you run the command, you might get a prompt to confirm the installation. You can verify that the installation was successful by running:

xcode-select -p

You should see the following output:

/Library/Developer/CommandLineTools

Once that’s done, you can follow the installation instructions for Ubuntu, as described in the previous section of this post.

Installing Node.js on Windows

If you want to set up Node.js with nvm on Windows, you first need to install the Windows Subsystem for Linux (WSL). That’s pretty easy and can be accomplished with a single command.

Open PowerShell or Windows Command Prompt in administrator mode by right-clicking and selecting Run as administrator. Then, run the following command:

wsl --install

This command will enable all features necessary to run WSL and will install Ubuntu. Once WSL is installed, restart your machine.

The first time you launch your newly installed Ubuntu distribution, a console window will open, and you’ll be asked to wait for files to de-compress and be stored on your machine. This might take a while, but all future launches should take less than a second.

Lastly, you want to install cURL (a tool for downloading internet content in the command line). For that, open your Ubuntu command line and run the following command:

sudo apt-get install curl

Once cURL is successfully installed, you can follow the Node.js installation instructions for Ubuntu, as described in the first section of this post.

Subscribe to Marco Besier
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from Marco Besier

Skeleton

Skeleton

Skeleton