install Node.js on Ubuntu
There are several different ways to install Node.js on an Ubuntu 18.04 machine. In this tutorial, we will show you three different ways to install Node.js; personally, I prefer to install Node.js using the NVM script.
Check out Our Best VPS Hosting and WordPress hosting for scaling your cloud-based applications and processes.
Install Node.js from the Ubuntu repository
The Node.js package is available from the Ubuntu 18.04 distribution repository. At the time of this writing, the version in the repositories is v8.10.0 which may not always be the latest version.
As always, before installing a package with apt, first we need to refresh the packages index and then install the Node.js package by typing:
sudo apt update sudo apt install nodejs
Then, verify the installation by typing:
nodejs --version v8.10.0
The Node.js executable from the Ubuntu repositories is named nodejs instead of node because of a conflict with another package.
To be able to download npm packages, we also need to install npm, the Node.js package manager. We can do that by typing:
sudo apt install npm
Install Node.js from the NodeSource repository
NodeSource is a company focused on providing enterprise-grade Node support, and they maintain a repository containing the latest versions of Node.js.
To enable the NodeSource repository, type 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 need to install version 10.x, just replace setup_8.x with setup_10.x in the command.
Once the NodeSource repository is enabled, install Node.js and npm by typing:
sudo apt install nodejs
Check if the installation was successful by printing the Node.js and npm versions:
Install Node.js using NVM
NVM (Node Version Manager) is a bash script used to manage multiple active Node.js versions. With NVM, we can install and uninstall any specific Node.js version, allowing us to have any number of Node.js versions that we may want to use or test.
To download the nvm install script, type:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
The script clones the nvm repository from Github to ~/.nvm and adds the script path to your Bash or ZSH profile.
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 says, we need to either close and reopen the terminal or run the commands to add the path to the nvm script to our current session. You can do whichever is easier for you.
To verify that nvm was properly installed, type:
nvm --version 0.33.11
Now that we have nvm installed, we can install the latest available version of Node.js by typing:
nvm install node
Let’s install two more versions, the latest LTS version and version 4.9.1:
nvm install --lts nvm install 4.9.1
We can verify the Node.js version, by typing:
node --version v10.1.0
Once LTS version and 4.9.1 are installed, we can list all installed Node.js instances by typing:
-> v4.9.1 # ACTIVE VERSION v8.11.1 v10.1.0 default -> node (-> v10.1.0) # DEFAULT VERSION node -> stable (-> v10.1.0) (default) stable -> 10.1 (-> v10.1.0) (default) iojs -> N/A (default) lts/* -> lts/carbon (-> v8.11.1) lts/argon -> v4.9.1 lts/boron -> v6.14.2 (-> N/A) lts/carbon -> v8.11.1
The output tells us that the entry with an arrow to its left (-> v4.9.1) is the version used in the current shell session, and the default version is set to v10.1.0. The default version is the version that will be active when opening new shells.
We change the current active version with:
nvm use 8.11.1
and verify it by typing:
nvm current v8.11.1
In case you want to set version 8.11.1 as a default Node.js version, you can do that with the following command:
nvm alias default 8.11.1
Install development tools
We need to install the development tools so that we are able to build native addons from npm. To install all the necessary packages, type:
sudo apt install gcc g++ make
If for some reason you want to uninstall the Node.js and npm packages, you can use the following command:
sudo apt remove nodejs npm
We have shown you three different ways to install Node.js and npm on your Ubuntu 18.04 server. The method you choose will depend on your requirements and preferences. While installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per-user basis.
If you have any questions or feedback, feel free to comment below.
One more thing..
Share this tutorial with your hosting administrators and networking experts friends, as it will help them and make my countless hours of work count.