Install system-wide Node.js with NVM: the painless way
This article explains how to install Node.js system-wide through the Node Version Manager.
Node.js deployment and maintenance made easy
Node.js is a quite active project and new versions (both maintenance and major releases) are published very often.
If you use Ubuntu/Debian systems, you expect that the Node.js official - stable? - package will be rarely updated, and you're right.
If you do not want to manually download, install or compile recent Node.js binaries and you prefer not to use unverified package repositories for your Linux distribution, you should consider Node Version Manager from creationx/nvm.
Node Version Manager, a.k.a. nvm, is a small and effective solution that allows to easily fetch, install and manage different and multiple Node.js versions within the same system.
Here follows a brief tutorial to let you get nvm installed as system-wide tool, a much more solid and consistent way than per-user basis.
Install system-wide Node Version Manager and Node.js
To install nvm as system-wide tool, you need to login as root or act a super user (with "sudo" commands) and be sure to have installed the minimal requirements needed by node.js and nvm. If you are on Ubuntu/Debian linux, execute:
# get super powers sudo -i # install compilers and other essential tools apt-get -y install gcc make curl git python python-dev libssl-dev build-essential
The nvm setup is extremely easy through the creationx script.To install nvm globally, you need to add a couple of environment variables during default setup. Just execute this:
# get super powers sudo -i # fetch and execute nvm setup script (you need to be connected to the internet) curl https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | NVM_DIR=/usr/local/nvm PROFILE=/etc/bash.bashrc bash
The previous code will download and copy nvm sources in /usr/local/nvm folder (that will contain all the node.js runtime versions) and will add the environment support to the system-wide bash configuration file.
After the installation succeeds, you need to logout/login or create a new terminal session to apply changes.
Now, you should be able to install latest node.js version through:
# get super powers sudo -i # install latest stable release and make it the default one nvm install stable nvm alias default stable # or, install specific release and/or make it the default one nvm install 4.0 nvm alias default 4.0
If you need to execute node and npm directly from services (systemd, sysvinit or other "limited" shell instances), you will need executables in /usr/local/bin folder:
# create node executable echo "#\!/bin/bash export NVM_DIR=\"/usr/local/nvm\" [ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" node \$@" > /usr/local/bin/node chmod +x /usr/local/bin/node # create npm executable echo "#\!/bin/bash export NVM_DIR=\"/usr/local/nvm\" [ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" npm \$@" > /usr/local/bin/npm chmod +x /usr/local/bin/npm
Then, to test if everything has been installed correctly:
node -v npm -v
If you get the node and npm related versions printed, everything is ok.
Remember that node.js binaries, headers and node_modules global folder are installed in system directories.
If you need to install node modules globally through the "npm install -g" flag, you will need to login as root or act as superuser (with "sudo" commands).
Multiple parallel Node.js environments in a single system
The Node Version Manager allows you to get every Node.js version you want. You can indeed install multiple versions and, despite the "default" aliased version, you can instantly switch among them in a terminal session with:
# install multiple specific releases nvm install 0.10 nvm install 0.12 # select a specific release for the current terminal session nvm use 0.10 node -v nvm use 0.12 node -v nvm use stable node -v nvm use default node -v
Just remember that Node.js versions are installed in separate folders within the /usr/local/nvm one: you should check whether your needed global modules are installed in the "active" version.
# list the global node modules for "active" node.js release npm list -g --depth=0
Keep systems up-to-date against security releases
Just in case a generic upgrade or a security release is needed, the fastest procedure is to login as root or act as superuser (with "sudo -i" command) and install specific version as usual. If you have installed node.js modules globally (with the "npm install -g" flag), you have to reinstall every global module or just make nvm take care of it:
# list installed versions nvm list # list available versions nvm ls-remote # upgrade from 0.12.7 to 4.0.0 nvm install 4.0.0 --reinstall-packages-from=0.12.7 nvm alias default 4.0.0 # clean up (better after some testing) nvm uninstall 0.12.7 # upgrade to the latest stable release and clean up nvm install stable --reinstall-packages-from=default nvm uninstall default nvm alias default stable
Security considerations and production deployment
Just to have a more complete picture of deployment strategies, nvm actually gets the binaries directly from official node.js mirror (and iojs.org ones).
If you do not override the mirror repositories with NVM_NODEJS_ORG_MIRROR variable, you should trust compiled stuff from nodejs.org (and iojs.org).
If security is really an issue for your systems and applications, please consider compiling node.js from certified or verified source code, it's not that difficult.
Forget Windows and Darwin (Mac) platforms
Installing node.js on Windows systems is still painful if you need to compile complex and powerful modules. If you still play with Windows, I strongly suggest to get a Linux virtual machine to develop on realistic production-looking systems. The Node Version Manager is not available for Windows and other derived/similar projects on that platform are still not reliable, as the time of writing. Install node.js with the official MSI installer.
Darwin (Mac) platform is based on Linux and nvm supports it but, instead of having to adapt the above nvm install code, I suggest to use the much more easy official PKG installer. I guess you do not have MacOSX server in production, do you?
Other architectures including ARM for IoT projects
If you're working on embedded, IoT or special projects, you should mind a couple of things.
The Node Version Manager should work on your custom systems without any problem (assuming they meet minimal requirements).
ARM architectures support have been improved in the latest releases and, as for 4.0.0 release, the following architectures seems to be supported by nvm through official node.js mirrors: arm6l, arm7l and arm64 (https://nodejs.org/download/release/v4.0.0/).
Previous releases could miss some ready-to-install binaries, but nvm is good enough to try getting the source code and compile node.js from source when needed.
The latest node.js versions, starting from 0.12 stable branches, are based on a updated V8 engine that drops the support for VFP lower than v4. If you need to develop embedded applications, make sure your target processor features VFPv4 instructions support or check if using node.js version 0.10 is a viable way.