Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active March 1, 2019 16:36
Show Gist options
  • Save plembo/b5145f38aaf4b88ef0292f6f0d1a48c5 to your computer and use it in GitHub Desktop.
Save plembo/b5145f38aaf4b88ef0292f6f0d1a48c5 to your computer and use it in GitHub Desktop.
Installing Node.js on Linux

Installing Node.js on Linux

There are two different approaches here: the "per user" install that developers like best, and the legacy "global" install favored by most server admins. Both these methods work on Ubuntu 16.04 and 18.04 LTS.

Using NVM

This installation method lets you manage multiple versions easily, and does not require root privileges.

The nvm script uses git to download the specified version of node (the default is "current") that lives in the $HOME/.nvm of the user.

https://github.com/creationix/nvm#install-script

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

This will also modify .bashrc to source nvm for the user:

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

Because I'm still a sysadmin, I usually change that last line in .bashrc to append "--no-use", so it looks like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As a result, any node versions installed using nvm won't load until I deliberately activate them, like:

nvm use 11.10.1

To install the LTS version of node, you'd use this command:

$ nvm install --lts

Switching back and forth between versions is easy:

$ nvm use 10.15.2

or

$ nvm use 8.15.1

To use the system version:

$ nvm use system
Now using system version of node: v10.15.2 (npm v6.4.1)

Get a list of what's installed with:

$ nvm ls
        v8.15.1
       v10.15.2
       v11.10.1
->       system
default -> 11.10.1 (-> v11.10.1)
node -> stable (-> v11.10.1) (default)
stable -> 11.10 (-> v11.10.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.15.2)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.0 (-> N/A)
lts/carbon -> v8.15.1
lts/dubnium -> v10.15.2

When using a "per user" deploy of node, where you'll have used npm init to set up each project, you should install modules to projects individually using the "--save" option:

me@mine:~/Projects/newldap$ npm init

...

me@mine:~/Projects/newldap$ npm install ldapjs yargs --save

No one will argue with you about doing this on your developer workstation, or if you're deploying apps to containers, but this is going to go against the grain of many server admins.

If someone decides to set up a "node.js server", then all bets are off. In that case you will want have to take the route described below, and go with the nodesource packages (under no circumstances allow yourself to be stuck with the distros official packages, they're going to be way too out of date!). If you do that I'd still install modules on a per user basis, unless they're needed for a system-wide script. In that case install sudo root with the "-g" option.

Using apt and npm

The official package repositories for Ubuntu 18.04 LTS have node 8.10.0 (and npm 3.5.2), but you really should be using packages for the latest stable LTS or Current (which are built by nodesource). For those, read the instructions here:

https://github.com/nodesource/distributions#debinstall

Go with Current for latest features, LTS for production systems.

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
$ sudo apt-get install -y nodejs

NOTE: The name "node" was originally used by a Amateur Packet Radio program in the Ubuntu repositories, making it unavailable for the node.js project. That program was later renamed to ax25-node.

To upgrade npm from the shipping version to the latest:

$ sudo npm update -g npm

Keep in mind that you must use the "-g" option to install node components globally. Leaving off the "-g" will result in installation to the user's home directory. So,

$ sudo npm install -g ldapjs

will be installed to /usr/lib/node_modules, while

$ npm install ldapjs

will be installed to ~/node_modules.

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