Installing Node.js using package managers

The preferred method for installing Node.js is to use the versions available in package managers, such as apt-get, or MacPorts. Package managers make your life easier by helping to maintain the current version of the software on your computer, ensuring to update dependent packages as necessary, all by typing a simple command, such as apt-get update. Let’s go over installation from a package management system first.

1. Installing Node.js on macOS with MacPorts

The MacPorts project (http://www.macports.org/) has been packaging a long list of open-source software packages for macOS for years and they have packaged Node.js. The commands it manages are, by default, installed on /opt/local/bin. After you have installed MacPorts using the installer on their website, installing Node.js is

very simple, making the Node.js binaries available in the directory where MacPorts installs commands:

$ port search nodejs npm

nodejs8 @8.16.2 (devel, net)

Evented I/O for V8 JavaScript

nodejs10 @10.16.3 (devel, net)

Evented I/O for V8 JavaScript

nodejs12 @12.13.0 (devel, net)

Evented I/O for V8 JavaScript

nodejs14 @14.0.0 (devel, net)

Evented I/O for V8 JavaScript

npm6 @6.14.4 (devel) node package manager

$ sudo port install nodejs14 npm6

.. long log of downloading and installing prerequisites and Node

$ which node

/opt/local/bin/node

$ node –version v14.0.0

If you have followed the directions for setting up MacPorts, the MacPorts directory is already in your PATH environment variable. Running the node, npm, or npx commands is then simple. This proves Node.js has been installed and the installed version matched what you asked for.

MacPorts isn’t the only tool for managing open source software packages on macOS.

3. Installing Node.js on macOS with Homebrew

Homebrew is another open source software package manager for macOS, which some say is the perfect replacement for MacPorts. It is available through their home page at http://brew.sh/. After installing Homebrew using the instructions on their website and ensuring that it is correctly set up, use the following code:

$ brew update

… long wait and lots of output

$ brew search node

==> Searching local taps…

node libbitcoin-node node-build node@8 nodeenv leafnode llnode node node@10 node@12 nodebrew nodenv

==> Searching taps on GitHub… caskroom/cask/node-profiler

==> Searching blacklisted, migrated and deleted formulae…

Then, install like this:

$ brew install node

==> Installing node

==> ownloading

https://homebrew.bintray.com/bottles/node-14.0.0_1.high_sierra.bottle.tar.gz

########################… 100.0%

==> Pouring node-14.0.0_1.high_sierra.bottle.tar.gz

==> Caveats

Bash completion has been installed to:

/usr/local/etc/bash_completion.d

==> Summary

/usr/local/Cellar/node/14.0.0_1: 4,660 files, 60MB

 Like MacPorts, Homebrew installs commands on a public directory, which defaults to /usr/local/bin. If you have followed the Homebrew instructions to add that directory to your PATH variable, run the Node.js command as follows:

$ node –version v14.0.0

 This proves Node.js has been installed and the installed version matched what you asked for.

Of course, macOS is only one of many operating systems we might use.

4. Installing Node.js on Linux, *BSD, or Windows from package management systems

Node.js is now available through most package management systems. Instructions on the Node.js website currently list packaged versions of Node.js for a long list of Linux, as well as FreeBSD, OpenBSD, NetBSD, macOS, and even Windows. Visit https://nodejs.org/en/download/package-manager/ for more information.

For example, on Debian and other Debian-based Linux distributions (such as Ubuntu), use the following commands:

$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash – [sudo] password for david:

## Installing the NodeSource Node.js 14.x repo…

## Populating apt-get cache…

… much apt-get output

## Run `sudo apt-get install -y nodejs` to install Node.js 13.x and npm

## You may also need development tools to build native addons: sudo apt-get install gcc g++ make

$ sudo apt-get install -y nodejs

… Much output

$ sudo apt-get install -y gcc g++ make build-essential

… Much output

 This adds the NodeSource APT repository to the system, updates the package data, and prepares the system so that you can install Node.js packages. It also instructs us on how to install Node.js and the required compiler and developer tools.

To download other Node.js versions (this example shows version 14.x), modify the URL to suit you:

$ node –version v14.0.0

The commands will be installed in /usr/bin and we can test whether the version downloaded is what we asked for.

Windows is starting to become a place where Unix/Linux geeks can work, thanks to a new tool called the Windows subsystem for Linux (WSL).

4.1. Installing Node.js in WSL

WSL lets you install Ubuntu, openSUSE, or SUSE Linux Enterprise on Windows. All three are available via the store built into Windows 10. You may need to update your Windows device for the installation to work. For the best experience, install WSL2, which is a major overhaul of WSL, offering an improved integration between Windows and Linux.

Once installed, the Linux-specific instructions will install Node.js in the Linux subsystem.

The process may require elevated privileges on Windows.

4.2. Opening an administrator-privileged PowerShell on Windows

Some of the commands that you’ll run while installing tools on Windows are to be executed in a PowerShell window with elevated privileges. We are mentioning this because during the process of enabling WSL, a command will need to be run in a PowerShell window.

The process is simple:

  1. In the Start menu, enter PowerShell in the application’s search The resulting menu will list PowerShell.
  2. Right-click the PowerShell
  3. The context menu that comes up will have an entry called Run as Administrator. Click on that.

The resulting command window will have administrator privileges and the title bar will say Administrator: Windows PowerShell.

In some cases, you will be unable to use Node.js from package management systems.

5. Installing the Node.js distribution from nodejs.org

The https://nodejs.org/en/ website offers built-in binaries for Windows, macOS, Linux, and Solaris. We can simply go to the website, click on the Install button, and run the installer. For systems with package managers, such as the ones we’ve just discussed, it’s better to use the package management system. That’s because you’ll find it easier to stay up to date with the latest version. However, that doesn’t serve all people because of the following reasons:

  • Some will prefer to install a binary rather than deal with the package manager.
  • Their chosen system doesn’t have a package management system.
  • The Node.js implementation in their package management system is out of date.

Simply go to the Node.js website and you’ll see something as in the following screenshot. The page does its best to determine your OS and supply the appropriate download. If you need something different, click on the DOWNLOADS link in the header for all possible downloads:

For macOS, the installer is a PKG file that gives the typical installation process. For Windows, the installer simply takes you through the typical install wizard process.

Once you are finished with the installer, you have command-line tools, such as node and npm, which you can run Node.js programs with. On Windows, you’re supplied with a version of the Windows command shell preconfigured to work nicely with Node.js.

As you have just learned, most of us will be perfectly satisfied with installing prebuilt packages. However, there are times when we must install Node.js from a source.

Source: Herron David (2020), Node.js Web Development: Server-side web development made easy with Node 14 using practical examples, Packt Publishing.

Leave a Reply

Your email address will not be published. Required fields are marked *