Installing Node.js in Ubuntu 18.04

Instructions for installing Node.js on Ubuntu 18.04

Node.js is a software platform that transforms JavaScript from a highly specialized language into a general-purpose language. Node.js adds the ability for JavaScript to interact with input/output devices via its API (written in C++), to connect other external libraries written in different languages, providing calls to them from JavaScript code. Node.js is used primarily on the server, acting as a web server, but there is an opportunity to develop in Node.js and desktop window applications (using NW.js, AppJS or Electron for Linux, Windows and Mac OS)

file

Installation from standard repositories

Before installing Node.js, update the existing Ubuntu package list: ``.shell sudo apt update

Let's install Node.js from the repositories:

sudo apt install nodejs

Next let's install **npm** - package manager for Node.js 

sudo apt install npm

This will allow you to install modules and packages for Node.js.
Due to a conflict with another package, the executable from Ubuntu repositories is called **nodejs** instead of **node** 

To check the version of Node.js you have installed, run the command:

nodejs -v

root@kvmde67-19464:~# nodejs -v v8.10.0


**Installing with PPA**

Using the personal package archive (PPA) supported by NodeSource, you can choose to install between Node.js v8.x (supported until December 2019), Node.js v10.x (supported until April 2021) and Node.js v12.x.

First, you need to install the PPA itself to access its contents 

Make sure you are in your home directory

cd ~


Then use **curl** to get the installation script for the version you need, replacing **10.x** with the version you need:

curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh


You can view the contents of the script with the nano editor:

nano nodesource_setup.sh

#!/bin/bash

Discussion, issues and change requests at:

https://github.com/nodesource/distributions

Script to install the NodeSource Node.js 10.x repo onto a

Debian or Ubuntu system.

Run as root or insert sudo -E before bash:

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

or

wget -qO- https://deb.nodesource.com/setup_10.x | bash -

CONTRIBUTIONS TO THIS SCRIPT

This script is built from a template in

https://github.com/nodesource/distributions/tree/master/deb/src

please don't submit pull requests against the built scripts.

export DEBIAN_FRONTEND=noninteractive SCRSUFFIX="_10.x" NODENAME="Node.js 10.x" NODEREPO="node_10.x" NODEPKG="nodejs"

print_status() { echo echo "## $1" echo }

if test -t 1; then # if terminal ncolors=$(which tput > /dev/null && tput colors) # supports colors if test -n "$ncolors" && test $ncolors -ge 8; then termcols=$(tput cols) bold="$(tput bold)" underline="$(tput smul)" standout="$(tput smso)" normal="$(tput sgr0)" black="$(tput setaf 0)" red="$(tput setaf 1)" green="$(tput setaf 2)" yellow="$(tput setaf 3)" blue="$(tput setaf 4)" magenta="$(tput setaf 5)" cyan="$(tput setaf 6)" white="$(tput setaf 7)" fi

Run the script with sudo privileges:

sudo bash nodesource_setup.sh

The PPA will be included in the configuration 
After running the installation script from Nodesource, you can install Node.js in the same way as described earlier:

sudo apt install nodejs

To check which version of Node.js is which, run the command:

nodejs -v

root@kvmde67-19464:~# nodejs -v v10.20.1


The nodejs package contains both nodejs and npm, so there is no need to install npm additionally .

**npm** uses a configuration file in your home directory to track updates. This file will be created the first time you run npm 

To make sure npm is installed, run the following command:

npm -v

root@kvmde67-19464:~# npm -v 6.14.4

Some packages from npm (such as those that require compilation from source) will require the **build-essentials** package to work:

sudo apt install build-essential



**Installation with NVM**

An alternative to installing Node.js via **apt** is to use the **nvm**(Node.js version manager) tool 
With this tool you can install several independent versions of Node.js that won't interfere with each other.
Controlling your development environment through nvm allows you to access the latest versions of Node.js while retaining previous versions 

You can use curl to download the installation script **nvm** from the project page on [GitHub](https://github.com/creationix/nvm) 
Note that the version number may be different than this example:

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o install_nvm.sh

You can view the install script using nano:

nano install_nvm.sh

#!/usr/bin/env bash

{ # this ensures the entire script is downloaded #

nvm_has() { type "$1" > /dev/null 2>&1 }

nvm_default_install_dir() { [ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nv$ }

nvm_install_dir() { if [ -n "$NVM_DIR" ]; then printf %s "${NVM_DIR}" else nvm_default_install_dir fi }

nvm_latest_version() { echo "v0.35.3" }

nvm_profile_is_bash_or_zsh() { local TEST_PROFILE TEST_PROFILE="${1-}" case "${TEST_PROFILE-}" in *"/.bashrc" | *"/.bash_profile" | *"/.zshrc") return ;; *) return 1 ;; esac }

Outputs the location to NVM depending on:

# The availability of $NVM_SOURCE

# The method used ("script" or "git" in the script, defaults to "git")

NVM_SOURCE always takes precedence unless the method is "script-nvm-exec"

nvm_source() { local NVM_METHOD NVM_METHOD="$1" local NVM_SOURCE_URL NVM_SOURCE_URL="$NVM_SOURCE" if [ "_$NVM_METHOD" = "script-nvm-exec" ]; then NVM_SOURCE_URL="https://raw.githubusercontent.com/nvm-sh/nvm/$(nvm_latest_version)/nvm-exe$ elif [ "$NVM_METHOD" = "_script-nvm-bash-completion" ]; then

Run the script in bash:

bash install_nvm.sh

This command will install the software in the **~/.nvm** subdirectory of your home directory. It will also add some necessary settings to the **~/.profile** file.

To get access to the nvm functionality, you can use the source command to apply the changes without interrupting your current session:

source ~/.profile

Once nvm is installed you can install isolated versions of Node.js. To find out which versions of Node.js are available to install, type:

nvm ls-remote

Output
      v8.17.0 (Latest LTS: Carbon)
     v9.0.0
     v9.1.0
     v9.2.0
     v9.2.1
     v9.3.0
     v9.4.0
     v9.5.0
     v9.6.0
     v9.6.1
     v9.7.0
     v9.7.1
     v9.8.0
     v9.9.0
    v9.10.0
    v9.10.1
    v9.11.0
    v9.11.1
    v9.11.2
    v10.0.0
    v10.1.0
    v10.2.0
    v10.2.1
    v10.3.0
    v10.4.0
    v10.4.1
    v10.5.0
    v10.6.0
    v10.7.0
    v10.8.0
    v10.9.0
   v10.10.0
   v10.11.0
   v10.12.0
   v10.13.0 (LTS: Dubnium)
   v10.14.0 (LTS: Dubnium)
   v10.14.1 (LTS: Dubnium)
   v10.14.2 (LTS: Dubnium)
   v10.15.0 (LTS: Dubnium)
   v10.15.1 (LTS: Dubnium)
   v10.15.2 (LTS: Dubnium)
   v10.15.3 (LTS: Dubnium)
   v10.16.0 (LTS: Dubnium)
   v10.16.1 (LTS: Dubnium)
   v10.16.2 (LTS: Dubnium)
   v10.16.3 (LTS: Dubnium)
   v10.17.0 (LTS: Dubnium)
   v10.18.0 (LTS: Dubnium)
   v10.18.1 (LTS: Dubnium)
   v10.19.0 (LTS: Dubnium)
   v10.20.0 (LTS: Dubnium)
   v10.20.1 (Latest LTS: Dubnium)
    v11.0.0
    v11.1.0
    v11.2.0
    v11.3.0
    v11.4.0
    v11.5.0
    v11.6.0
    v11.7.0
    v11.8.0
    v11.9.0
   v11.10.0
   v11.10.1
   v11.11.0
   v11.12.0
   v11.13.0
   v11.14.0
   v11.15.0
    v12.0.0
    v12.1.0
    v12.2.0
    v12.3.0
    v12.3.1
    v12.4.0
    v12.5.0
    v12.6.0
    v12.7.0
    v12.8.0
    v12.8.1
    v12.9.0
    v12.9.1
   v12.10.0
   v12.11.0
   v12.11.1
   v12.12.0
   v12.13.0 (LTS: Erbium)
   v12.13.1 (LTS: Erbium)
   v12.14.0 (LTS: Erbium)
   v12.14.1 (LTS: Erbium)
   v12.15.0 (LTS: Erbium)
   v12.16.0 (LTS: Erbium)
   v12.16.1 (LTS: Erbium)
   v12.16.2 (LTS: Erbium)
   v12.16.3 (Latest LTS: Erbium)
    v13.0.0
    v13.0.1
    v13.1.0
    v13.2.0
    v13.3.0
    v13.4.0
    v13.5.0
    v13.6.0
    v13.7.0
    v13.8.0
    v13.9.0
   v13.10.0
   v13.10.1
   v13.11.0
   v13.12.0
   v13.13.0
   v13.14.0
    v14.0.0
    v14.1.0
    v14.2.0
Install e.g. version 10.20.1

nvm install 10.20.1

Normally, nvm will switch to use the latest installed version. You can tell nvm to use the version you just downloaded explicitly as follows:

nvm use 10.20.1

If you install Node.js via nvm, the executable will be named node. To see what version the shell is currently using, you can use the command:

node -v

root@kvmde67-19464:~# node -v v10.20.1

If you have more than one version of Node.js installed, you can see their list by using the command:

nvm ls

root@kvmde67-19464:~# nvm ls -> v10.20.1 system default -> 10.20.1 (-> v10.20.1) node -> stable (-> v10.20.1) (default) stable -> 10.20 (-> v10.20.1) (default) iojs -> N/A (default) unstable -> N/A (default) lts/* -> lts/erbium (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.20.1 lts/erbium -> v12.16.3 (-> N/A)

If you want to set one of the versions as the default version, type:

nvm alias default 10.20.1

This version will be automatically chosen when you start a new session. You can also refer to it by alias as follows:

nvm use default

Each version of Node.js has its own packages, which you can manage with npm.

**npm** can install packages in the **./node_modules directory of Node.js** projects.

------------------------

You can also use our **One-Click-Apps** for one-click installation of Node.js 
To do this, select the required [VPS](https://fornex.com/ssd-vps) plan and select **Apps → Node.JS version 10 or 12**, to install.

![file](/media/article/img/Снимок_экрана_2020-05-12_в_17.12.04.png)

------------------------

If you have difficulty setting up or have additional questions, you can always contact our support team via [ticket system](https://fornex.com/my/tickets/).
Need help?Our engineers will help you free of charge with any question in minutesContact us