node.js

HOW TO INSTALL NODE.JS ON UBUNTU 18.04

This tutorial explains how to install Node.js and npm on an Ubuntu 18.04 machine. Node.js is an open source cross-platform JavaScript run-time environment that allows server-side execution of JavaScript code which simply means that you can run JavaScript code on your machine as a standalone application, free of any web browser.

Node.js is mainly used to build back-end 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 three different ways to install Node.js on Ubuntu.

If you need Node.js only for deploying Node.js applications then the simplest option is to install the Node.js packages using apt from the Default Ubuntu repository or from the NodeSource repository in case you need the latest Node.js and npm versions.

If you are using Node.js for development purposes then your best option is to install Node.js using the NVM script.

Choose one of the installation options that will work best for you.

Although this tutorial is written for Ubuntu the same instructions apply for any Ubuntu-based distribution, including Linux Mint and Elementary OS.

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.

Node.js package is available from the Ubuntu 18.04 distribution repository. At the time of writing, the version in the repositories is v8.10.0 which may not always be the latest version.

To install Node.js and npm from the default Ubuntu repositories, follow these steps:

  1. Install Node.jsRefresh the packages index and install the Node.js by typing:
    sudo apt update
    sudo apt install nodejs

    To verify the installation execute the following command:

    nodejs --version
    v8.10.0

    The Node.js executable from the Ubuntu repositories is named nodejs instead of nodebecause of a conflict with another package.

  2. Install npmTo 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

    Verify the installation by typing:

    npm --version
    3.5.2

NodeSource is a company focused on providing enterprise-grade Node support and they maintain a repository containing the latest versions of Node.js.

To install Node.js and npm from the NodeSource repository, follow these steps:

  1. Enabling NodeSource repositoryTo enable the NodeSource repository type the following command:
    curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -

    The command above will add the NodeSource signing key to your system, create an apt sources repository file install all necessary packages and refresh the apt cache.

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

  2. Installing Node.js and npmOnce the NodeSource repository is enabled install Node.js and npm by typing:
    sudo apt install nodejs
  3. Verifying Node.js and npm installationThe simplest way to verify that Node.js and npm installation were successful is by printing their versions:
    node --version
    v8.11.3
    npm --version
    5.6.0

     

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 that we want to use or test.

To install Node.js and npm using NVM on your Ubuntu system, follow these steps:

  1. Downloading NVM (Node Version Manager) scriptTo download or update the NVM install script type:
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

    The script will clone the nvm repository from Github to the ~/.nvm directory and add the nvm path to your Bash or ZSH profile.

    download-nvm-script.

    => 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

    Copy

    As the output above says, we need to either close and reopen the terminal or run the commands to add the path to nvm script to our current session. You can do whatever is easier for you.

    Once the script is in your PATH verify that nvm was properly installed by typing:

    nvm --version
    0.33.11
  2. Installing Node.js using NVMNow that we have nvm installed we can install the latest available version of Node.js, by typing:
    nvm install node
    

    Once the installation is completed we can verify in by printing the Node.js version:

    node --version
    v10.6.0

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

nvm install --lts
nvm install 4.9.1

Once LTS and 4.9.1 versions are installed we can list installed Node.js instances by typing:

nvm ls

The output tells us that the entry with an arrow on the right (-> v4.9.1), is the version used in the current shell session and the default version is set to v10.6.0. Default version is the version that will be active when opening new shells.

We change the currently active version with:

nvm use 8.11.

and verify it by typing:

nvm current
v8.11.3

In case you want to set version 8.11.3 as a default Node.js version you can do that with the following command:

nvm alias default 8.11.3

To be able to compile and install native add-ons from npm you need to install the development tools.

The following command will install all the necessary packages:

sudo apt install gcc g++ make

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

sudo apt remove nodejs npm

About: Androxus


    Leave a Reply

    Your email address will not be published. Required fields are marked *