Read in 4 minutes

last updated 

How to install Node.js and npm on Debian 9

In this tutorial, we will cover how to install Node.js and npm on a Debian 9 system.

Node.js is an open source cross-platform JavaScript run-time environment built on Chrome’s JavaScript engine that allows server-side execution of JavaScript code. It is mainly used to build server-side applications, but it is also very popular as a full-stack and front-end solution. npm is the default package manager for Node.js and the world’s largest software registry.

In this tutorial we will show you two different ways to install Node.js on Debian. Choose the installation option that is best for you.

Install Node.js and NPM from the NodeSource repository

The easiest way to install the latest versions of Node.js and npm is to install the package from the repository maintained by NodeSource.

To add the NodeSource repository to your system run the following command:

curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -

The current LTS version of Node.js is version 8.x, Carbon. If you want to install Node.js version 10.x just change setup_8.x with setup_10.x

Once the NodeSource repository is added install Node.js and npm with the following command:

sudo apt install nodejs

To verify if the installation was successful, print the Node.js and npm versions:

node --version
v8.11.3
npm --version
5.6.0

Install Node.js and NPM using NVM

NVM (Node Version Manager) is a bash script that allows you to manage multiple Node.js versions. With NVM you can install and uninstall any specific Node.js version that you want to use or test.

Download the nvm install script with the following curl command:

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

The installation script clones the nvm repository from Github to the ~/.nvm directory and adds the nvm path to your Bash or ZSH profile.

=> Close and reopen your terminal to start using nvm or run the following to use it now:

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

As the output above suggests, you can either open a new shell session or run the commands to add the path to the nvm script to your current session. Do whatever is easier for you.

To ensure that nvm is properly installed type:

nvm --version
0.33.11

Now that you have nvm installed on your Debian machine, to install the latest available version of Node.js, type:

nvm install node
Downloading and installing node v10.5.0...
Downloading https://nodejs.org/dist/v10.5.0/node-v10.5.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.5.0 (npm v6.1.0)
Creating default alias: default -> node (-> v10.5.0)

Verify the Node.js version, by typing:

node --version
v10.5.0

Let’t install two more versions, the latest LTS version and version 4.9.1

nvm install --lts
nvm install 6.14.3

Once LTS version and 6.14.3 are installed we can list installed Node.js instances by typing:

nvm ls
->      v6.14.3                     # ACTIVE VERSION
        v8.11.3
        v10.5.0
         system
default -> node (-> v10.5.0)        # DEFAULT VERSION
node -> stable (-> v10.5.0) (default)
stable -> 10.5 (-> v10.5.0) (default)
iojs -> N/A (default)
lts/* -> lts/carbon (-> v8.11.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.3
lts/carbon -> v8.11.3

In the output above, the entry with an arrow on the right (-> v6.14.3), is the version used in the current shell session and the default version is set to v10.5.0.

Default version is the version that will be used when you open new shell sessions.

If you want to change the current active version use the following command:

nvm use 8.11.3

and verify it by typing:

nvm current
v8.11.3

To set version 8.11.3 as the default Node.js version type:

nvm alias default 8.11.3

Install development tools

To compile and install native addons from the npm registry you need to install the development tools.

sudo apt install build-essential

Uninstall Node.js

If for some reasons you want to uninstall Node.js and npm packages, you can use the following command:

sudo apt remove nodejs npm

Conclusion

We have shown you two different ways to install Node.js and npm on your Debian 9 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per user basis.

Now that you’ve installed Node.js on your Debian 9 system, it’s time to start developing your application!

If you want to manage your npm packages with yarn, you can check this tutorial about how to install and use yarn on Debian 9.

If you have any questions or feedback, feel free to comment below.