How to install nodejs
This article revisits essentials on how to install a nodejs
environment, on both development and production servers.
This article has complementary materials to the Testing
nodejs
Applications book. However, the article is designed to help both those who already bought the book, as well as the wide audience of software developers to setup working environment. You can grab a copy of this book on this link
Installing nodejs
on Linux
When running aptitude, or similar package management tools, installing nodejs
becomes quite like installing any other program. The following commands are the ones most people tend to use.
Installing a nodejs
on Linux using apt
Challenge using this strategy, is to make sure we have nodejs
in our PPA database.
# Adding and using nodejs from individual PPAs
$ sudo add-apt-repository ppa:chris-lea/nodejs
$ sudo apt-get update
$ sudo apt-get install -y nodejs
# Removing Chriss Lea PPA before using NodeSource PPA
# @link https://github.com/nodesource/distributions#manual-installation
$ sudo add-apt-repository -y -r ppa:chris-lea/nodejs
$ sudo rm -f /etc/apt/sources.list.d/chris-lea-node_js-*.list
$ sudo rm -f /etc/apt/sources.list.d/chris-lea-node_js-*.list.save
# Adding and using NodeSource distribution
# @link https://github.com/nodesource/distributions
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# Alternatively
$ curl -sL https://deb.nodesource.com/setup | sudo bash -
$ sudo apt-get install -y nodejs
Example: install nodejs
binaries aptitude
Once we have nodejs
to be recognized by aptitude, most operations becomes a little more familiar. Normally running apt-get upgrade
should upgrade all binaries including nodejs
. We should mention that updating nodejs
also updates npm
.
Installing a nodejs
on Linux using nvm
When we want to test an application against a particular version of nodejs
, the environment we work in should be able to manage multiple versions of nodejs
. That is where tools such as nvm come into play.
To make the tool work, we will have to have nodejs
installed on our work environment. The second step will be to download and install nvm
to our work environment. After installation, we can use tools provided by the nvm to install and toggle a specific version.
$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
# Or
$ wget https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
# Install a particular nodejs version
$ nvm install v<Major>.<Minor>.<Patch>
# toggle a particular nodejs version
$ nvm use v<Major>.<Minor>.<Patch>
# execture code with a particular nodejs version
$ nvm run v<Major>.<Minor>.<Patch> /path/to/server.js
# uninstall a particular nodejs version
$ nvm uninstall v<Major>.<Minor>.<Patch>
Example: working with nvm
tool
An approach a bit similar to this is available for yet another nodejs
version manager known under pseudonym n
Installing nodejs
on macOS
It is possible to download and install nodejs
manually, with ready to use installer. However, when wanting to tuning nodejs
environment at our taste, messing with methods and other tools described below becomes a must.
In case homebrew
is not already available on your mac, this is how to get one up and running. On its own, homebrew
depends on ruby
runtime to be available.
homebrew
is a package manager and software installation tool that makes most developer tools installation on macOS a breeze. We should also highlight thathomebrew
requiresxcode
to be available on the system.
$ /usr/bin/ruby -e \
"$(curl -fsSL https://raw.githubusercontent.com \
/Homebrew/install/master/install)"
Example: installation instruction as provided by brew.sh
Generally speaking, this is how to install and uninstall things with brew
$ brew install wget
$ brew uninstall wget
Example: installing/uninstalling wget
binaries using homebrew
We have to to stress on the fact that Homebrew installs packages to their own directory and then symlinks their files into
/usr/local
.
It is always a good idea to update the system before start working. And that, even when we have a daily task that automatically updates the system for us. macOS can use homebrew
package manager on maintenance matters. To update/upgrade or check outdated packages, following commands would help.
$ brew outdated # lists all outdated packages
$ brew cleanup -n # visualize the list of things are going to be cleaned up.
$ brew upgrade # Upgrades all things on the system
$ brew update # Updates all outdated + brew itself
$ brew update <formula> # Updates one formula
$ brew install <formula@version> # Installs <formula> at a particular version.
$ brew tap <formular@version>/brew # Installs <formular> from third party repository
# untap/re-tap a repo when previous installation failed
$ brew untap <formular> && brew tap <formula>
$ brew services start <formular>@<version>
Example: key commands to work with homebrew
cli
For more informations, visit: Homebrew ~ FAQ.
Installing a nodejs
on a macOS using curl
nodejs
installation guide suggests this alternative for the MacOs. However, we have to customize a download directory, the version, and installation directory.
$ curl "https://nodejs.org/dist/latest/node-${VERSION:-$(wget \
-qO- https://nodejs.org/dist/latest/ | \
sed -nE 's|.*>node-(.*)\.pkg</a>.*|\1|p')}.pkg" > \
"$HOME/Downloads/node-latest.pkg" && sudo installer \
-store -pkg "$HOME/Downloads/node-latest.pkg" -target \
"/"
Example: installation instruction as provided by nodejs
Using homebrew
, as demo-ed in next paragraph, may be the most popular approach to install nodejs
version on a macOS machine.
Installing a nodejs
on a Mac using homebrew
In case homebrew
is not already available on your mac, this is how to get one up and running. On its own, homebrew
depends on ruby runtime to be available.
homebrew
is a package manager and software installation tool that makes most developer tools installation a breeze.
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Example: installation instruction as provided by brew.sh
In case the homebrew
is already available, it is a good practice to update it before adding new software. In the next code sample, we install some tools such as wget
, sometimes useful to download binaries.
how to install things with brew
$ brew install nodejs
Example: install nodejs
binaries using homebrew
After installation, add binary to the PATH [ /usr/local/share/npm/bin
], $ nano /etc/paths
, append the path /usr/local/share/npm/bin
at the end of the file.
homebrew
installs packages to their own directory and then symlinks their files into/usr/local
.
Installing nodejs
on a Windows machine
MacOs and most Linux distributions come with Python and Ruby already enabled. These two languages are somehow required to run successfully a nodejs
environment on any server. This is an easy target as nodejs
gives windows binaries that we can download and install on a couple of clicks.
Automated upgrades
Before we dive into automatic upgrades, we should consider nuances associated to managing a nodejs
instance. The updates fall into two major, quite interesting, categories: patch updates and version upgrades.
Following the SemVer ~ aka Semantic Versioning standard, it is not recommended to consider minor/major versions for automated upgrades. One of the reasons being that these versions are subject to introducing breaking changes or incompatibility between two versions. On the other hand, patches are less susceptible to introduce breaking changes, whence ideal candidates for automated upgrades. Another among other reasons, being that security fixes are released as patches to a minor version.
We should highlight that it is always better to upgrade at deployment time. The process is even easier in containerized context. We should also automate only patches, to avoid to miss security patches.
In the context of Linux, we will use the unattended-upgrades package to do the work.
$ apt-get install unattended-upgrades apticron
Example: install unattended-upgrades
Two things to fine-tune to make this solution work are: to enable a blacklist of packages we do not to automatically update, and two, to enable particular packages we would love to update on a periodical basis. That is compiled in the following shell scripts.
Unattended-Upgrade::Allowed-Origins {
// "${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security"; # upgrading security patches only
// "${distro_id}:${distro_codename}-updates";
// "${distro_id}:${distro_codename}-proposed";
// "${distro_id}:${distro_codename}-backports";
};
Unattended-Upgrade::Package-Blacklist {
"vim";
};
Example: fine-tune the blacklist and whitelist in /etc/apt/apt.conf.d/50unattended-upgrades
The next step is necessary to make sure unattended-upgrades download, install and cleanups tasks have a default period: once, twice a day or a week.
APT::Periodic::Update-Package-Lists "1"; # Updates package list once a day
APT::Periodic::Download-Upgradeable-Packages "1"; # download upgrade candidates once a day
APT::Periodic::AutocleanInterval "7"; # clean week worth of unused packages once a week
APT::Periodic::Unattended-Upgrade "1"; # install downloaded packages once a day
Example: tuning the tasks parameter /etc/apt/apt.conf.d/20auto-upgrades
This approach works on Linux(Ubuntu), especially deployed in production, but not Windows nor macOS. The last issue, is to be able to report problems when an update fails, so that a human can intervene whenever possible. That is where the second tool apticron
in first paragraph intervenes. To make it work, we will specify which email to send messages to, and that will be all.
EMAIL="<email>@<host.tld>"
Example: tuning reporting tasks email parameter /etc/apticron/apticron.conf
Conclusion
In this article we revisited ways to install nodejs
on various platforms. Even though configuration was beyond the scope of this article, we managed to get everyday quick refreshers out.
Reading list
#nodejs #homebrew #UnattendedUpgrades #nvm #n #y2020 #Jan2020 #HowTo #ConfiguringNodejsApplications #tdd #TestingNodejsApplications