Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to install Node.js and NPM on Ubuntu

Published

Node.js is an open-source JavaScript runtime environment that allows you to create network applications efficiently. Node.js allows you to use Javascript on frontend as well as backend. It means that you can develop the whole application in Javascript. It makes the development and management of the application easier. In this guide, I will show you different ways to install Node.js and NPM on your Ubuntu server.

NPM is a default package manager for Node.js based applications. Also, It is the largest software registry right now. It allows you to easily add, remove and update dependencies on your project. As there are many different methods to install Node.js and NPM on Ubuntu, you can refer to Node.js documentation to select the best method for yourself.

Now, Let us get started with the first and the easiest method to install Node.js and npm on Ubuntu server.

Install Node.js from Ubuntu Repositories

Node.js and NPM are already available inside the Ubuntu Repositories. But, Note that the version of Node.js and NPM might not be the latest. If you are going to install Node.js and NPM using this method, you will get the latest stable version of Node.js for Ubuntu based on it’s version and support.

To install Node.js and NPM using this method, Open up the terminal/console on your Ubuntu machine and execute the following commands.

$ sudo apt-get update
$ sudo apt-get install nodejs -y

The -y in the second command will suppress the confirmation prompt to install a package. The first command will update Ubuntu repositories. It is important to run the first command if you just got a brand new Ubuntu server.

It might take a minute or two to install Node.js and NPM using this method. Once the installation is complete, you can verify the installation by checking the version of Node.js you have on your server.

To check the current version of Node.js in your machine, execute the following command.

$ nodejs -v

To check the current version of NPM on your machine, execute the following command.

$ npm -v

If you can see the version numbers as output, you have successfully installed Node.js and NPM on your server. Now, Let us see the other method that allows you to choose the version of Node.js you want to install on your server.

Install Node.js and NPM from NodeSource

NodeSource is a company that provides enterprise support for Node.js. They maintain a repository containing multiple Node.js and NPM versions. We can use the NodeSource repository to install Node.js on our server.

This process is similar to the previous one, but in this case, we will first add the repository that contains the desired Node.js version. After adding the repository, we will simply run the commands that we ran in the previous method.

In this demonstration, we will install Node.js v10.x on our server. But you can replace the number 10 with any version you want. So, Let us first add the repository on our server.

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

This command will add a new repository in our source list and also a NodeSource signing key. Note that you can change the version number in this command. Once the execution is complete, run the following commands on your server to install Node.js and NPM.

$ sudo apt-get update
$ sudo apt-get install nodejs -y

It might take a minute or two for this process to complete. Once done, check the Node.js version using the following command.

$ nodejs -v

If should see a version number similar to the version number in the repository we added in our system. Similarly, execute the following command to check NPM version.

$ npm -v

So, this is how you can install Node.js using a NodeSource repository. I recommend you to use this method if you want a specific Node.js version on your system.

Install Node.js and npm using NVM

This method is not as easy to understand as other two methods. But in this case, you can manage and use multiple versions of Node.js on your server. You can change the active version of Node.js as per your choice.

To install Node.js using NVM, we first have to install NVM on our server. It is a simple bash script. We just have to download and execute it. To do so, execute the following command on your server.

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

Once the process is complete, close the terminal and open a new SSH connection with a new terminal to your server. Once you are on a new terminal, execute the following command to check the current version of NVM.

$ nvm --version

If you can see the version number, NVM is successfully installed on your server. Now, It is time to install Node.js using NVM. To install Node.js using NVM, execute the following command on your server.

$ nvm install node

Once the process is complete, execute the following command to check the current version of Node.js on your server.

$ node --version

Similarly, you can run the following command to check the current version of NPM on your server.

$ npm --version

To install Latest LTS(Long term support) version of Node using NVM, execute the following command.

$ nvm install --lts

So, this is how you can install, manage and use multiple versions of node on your machine using NVM. I recommend you to use this method on development environment if you have many applications based on node that requires different node versions.

Install Build Essentials (Important)

Installation of build-essential package is very important regardless of the method you are following to install Node.js and NPM on your system. You might face lots of troubles while compiling your code if you miss this step.

The package contains everything that is required to compile your code. It will also install gcc compilers on your server. Execute the following commands on your server to install this package.

$ sudo apt-get update
$ sudo apt-get install build-essential

It might take a minute or two to complete the installation. Once done, you will be able to add and compile additional packages on your application without facing lots of errors.

 

Conclusion: In this guide, I have demonstrated three methods to install Node.js and NPM on your server. The first and the easiest one can be used to quickly install Node.js on your server. But you will usually install older versions of Node with that method. I recommend you to use the Second method in which we add the NodeSource repository to install the desired Node version on our server. It is stable as well as efficient and fast.

Let us know if you are stuck somewhere in the process in the comment section given below. If you are an InterServer custoner, please contact our support staff to resolve your issues as soon as possible.

Account services are unavailable until the production backend is configured.