• Home
  • Knowledgebase
  • Blog
  • Shared Hosting
    • Linux Shared Hosting
  • Server
    • Dedicated Server Hosting
    • Smart Dedicated Server
    • Linux Vps Server
    • Windows VPS Server
  • Cloud Server
    • Micro Azure Cloud
    • Amazon Web Services
    • Google Cloud
    • Cloud VPS Server
  • Security
    • SSL Certificate
  • Email
    • Office 365
    • Google Workspace
  • Home
  • Knowledgebase
  • Blog
  • Shared Hosting
    • Linux Shared Hosting
  • Server
    • Dedicated Server Hosting
    • Smart Dedicated Server
    • Linux Vps Server
    • Windows VPS Server
  • Cloud Server
    • Micro Azure Cloud
    • Amazon Web Services
    • Google Cloud
    • Cloud VPS Server
  • Security
    • SSL Certificate
  • Email
    • Office 365
    • Google Workspace
home/Knowledge Base/Ubuntu/How To Install Node.js on Ubuntu 18.04

How To Install Node.js on Ubuntu 18.04

147 views 0 October 8, 2022 host_know_user

Introduction

Node.js is a JavaScript general-purpose programming framework that allows users to quickly develop network applications. Node.js makes development more consistent and integrated by employing JavaScript on both the front and backend.

This post will teach you how to install Node.js on an Ubuntu 18.04 server using three alternative techniques.

Prerequisites

This tutorial assumes you’re running Ubuntu 18.04. Before you begin, make sure your system has a non-root user account with sudo capabilities.

Apt Installing Node.js from Default Repositories

In its default repository, Ubuntu 18.04 includes a version of Node.js that can be used to give a consistent experience across various systems. The version in the repositories at the time of writing is 8.10.0. This will not be the most recent version, but it should be reliable and sufficient for first language exploration.

You can obtain this version by using the apt package manager. Update your local package index:

$ sudo apt update

Install Node.js now:

$ sudo apt install nodejs

Verify that Node.js was successfully installed by inquiring about its version number:

$ node –v
Output
v8.10.0

This is all you need to do to get started with Node.js if the package in the repository meets your requirements. You need also install npm, the Node.js package manager, in most circumstances. You can use apt to install the npm package:

$ sudo apt install npm

This will allow you to install Node.js modules and packages.

You have now installed Node.js and npm using apt and the default Ubuntu software repositories. You may, however, opt to work with various Node.js versions, package archives, or version managers. The following phases will go through these elements, as well as more flexible and durable installation methods.

Apt Installation of Node.js Using a NodeSource PPA

To install a more recent version of Node.js, add the NodeSource PPA (personal package archive). This will include more up-to-date Node.js versions than the official Ubuntu repository and will allow you to pick between different platform versions.

Install the PPA first to gain access to its contents. Curl the installation script for your selected version from your home directory, making sure to replace 17.x with your preferred version string (if different):

$ cd ~
$ curl -sL https://deb.nodesource.com/setup_17.x -o /tmp/nodesource_setup.sh

You can investigate the contents of this script with nano (or your choice text editor) if you want:

$ nano /tmp/nodesource_setup.sh

Exit the text editor after you’re sure the script is safe to run. If you used nano, simply hit CTRL + X to exit. The script should then be run with sudo:

$ sudo bash /tmp/nodesource_setup.sh.

The PPA will be added to your settings, and your local package cache will be automatically updated. You can now install the Node.js package as described in the previous section:

$ sudo apt install nodejs

Run node using the -v flag to ensure you’ve installed the new version:

$ node –v
Output
v17.3.0

This nodejs package, unlike the one in the usual Ubuntu package repositories, includes both node and npm, so you don’t need to install npm separately.

To keep track of updates, npm uses a configuration file in your home directory. When you run npm for the first time, it will be created. To check that npm is installed and to build the configuration file, run the following command:

$ npm –v
Output
8.3.0

Some npm packages (those that require compiling code from source, for example) require the installation of the build-essential package:

$ sudo apt install build-essential

You now have the tools you need to work with npm packages that need compilation from source code.

You installed Node.js and npm using apt and the NodeSource PPA in this section. The Node Version Manager will then be used to install and manage different versions of Node.js.

Using the Node Version Manager to Install Node

Another option for installing Node.js is to utilise nvm, the Node Version Manager (NVM). nvm operates at the level of an independent directory within your home directory, rather than at the level of the operating system. This means that you can install numerous self-contained Node.js versions without affecting the overall system.

Managing your environment with nvm allows you to access the most recent Node.js versions while retaining and managing prior releases. It is, however, a separate utility from apt, and the versions of Node.js you manage with it are distinct from the versions you manage with apt.

Before piping the command to bash, you should always audit the script to ensure it isn’t doing anything you don’t agree with. Remove the | bash element at the end of the curl command to accomplish this:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh

Review the output and ensure that you are happy with the adjustments it is making. When you’re finished, repeat the command with | bash appended at the end. The URL you use will change based on the newest version of NVM, but for the time being, you can download and execute the script by running the following:

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

The nvm script is now installed in your user account. To use it, first download the .bashrc file:

$ source ~/.bashrc

You can install isolated Node.js versions with nvm. First, ask nvm what Node versions are available:

$ nvm ls-remote

Output
…
v14.18.2 (Latest LTS: Fermium)
v15.0.0
v15.0.1
v15.1.0
v15.2.0
v15.2.1
v15.3.0
v15.4.0
v15.5.0
v15.5.1
v15.6.0
v15.7.0
v15.8.0
v15.9.0
v15.10.0
v15.11.0
v15.12.0
v15.13.0
v15.14.0
v16.0.0
v16.1.0
v16.2.0
v16.3.0
v16.4.0
v16.4.1
v16.4.2
v16.5.0
v16.6.0
v16.6.1
v16.6.2
v16.7.0
v16.8.0
v16.9.0
v16.9.1
v16.10.0
v16.11.0
v16.11.1
v16.12.0
v16.13.0 (LTS: Gallium)
v16.13.1 (Latest LTS: Gallium)
v17.0.0
v17.0.1
v17.1.0
v17.2.0
v17.3.0

It’s a large list, but you can install Node by entering any of the released versions listed. For instance, to obtain version v16.13.1, execute the following:

$ nvm install v16.13.1
Output
Now using node v16.13.1 (npm v8.1.2)

Occasionally, nvm will use the most recently installed version. However, you can instruct nvm to use the version you just downloaded (if it differs):

$ nvm use v16.13.1

Run the following commands to determine the current version:

$ node –v
Output
v16.13.1

If you have numerous Node versions installed, you can retrieve a list of them by running ls:

$ nvm ls

Output
-> v16.13.1
system
default -> v16.13.1
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.13.1) (default)
stable -> 16.13 (-> v16.13.1) (default)
lts/* -> lts/gallium (-> v16.13.1)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.8 (-> N/A)
lts/fermium -> v14.18.2 (-> N/A)
lts/gallium -> v16.13.1

You can alternatively select one of the following versions as your default:

$ nvm alias default 16.13.1
Output
default -> 16.13.1 (-> v16.13.1)

When a new session starts, this version is automatically picked. You can also use an alias to refer to it, like in the following command:

$ nvm use default
Output
Now using node v16.13.1 (npm v8.1.2)

Every version of Node will maintain track of its own packages, which may be managed via npm.

You can also have npm install packages to the Node.js project’s./node modules directory. Install the express module using the following syntax:

$ npm install express

Output
added 50 packages, and audited 51 packages in 4s

2 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.1.2 -> 8.3.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.3.0
npm notice Run npm install -g npm@8.3.0 to update!
npm notice

If you want to install the module globally, making it available to other projects running the same Node.js version, use the -g flag:

$ npm install -g express

Output
added 50 packages, and audited 51 packages in 1s

2 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities

This will install the package in the following locations:

$ ~/.nvm/versions/node/16.13.1/lib/node_modules/express

You can run commands from the command line if you install the module globally, but you must link the package into your local sphere to use it from within a programme:

$ npm link express

Run the following commands to learn more about the choices available with nvm:

$ nvm help

You’ve successfully installed Node by utilising the Node Version Manager, nvm, to install and manage different Node versions.

Removing Node.js

Depending on the version you want to target, you can remove Node.js with apt or nvm. At the system level, you will use apt to delete the default repository version. This command uninstalls the software while keeping the configuration files. This is useful if you intend to reinstall the software later:

$ sudo apt remove nodejs

If you do not want to save the configuration files for future usage, run the following command to uninstall the package and delete the configuration files associated with it:

sudo apt purge nodejs

Finally, you can uninstall any unwanted packages that were installed automatically with the removed package:

$ sudo apt autoremove

To uninstall a Node.js version that you have activated with nvm, first establish whether the version you want to delete is the current active version:

$ nvm current

If the version you want is not the current active version, you can use the following command:

$ nvm uninstall node_version
Output:
Uninstalled node node_version

This command will uninstall the selected Node.js version.

If the version you want to uninstall is the current active version, you must first disable nvm before you can make changes:

$ nvm deactivate

You can now uninstall the current version with the previously used uninstall command. Except for the cached files that can be used for reinstallation, this eliminates all files connected with the targeted version of Node.js.

Conclusion

There are several ways to get Node.js up and running on your Ubuntu 18.04 server. Your circumstances will determine which strategy is best for you. While utilising the bundled version from the Ubuntu repository is one option, using nvm or a NodeSource PPA provides more freedom.

Was this helpful?

Yes  No
Related Articles
  • How to Install Jenkins in ubuntu 20.04
  • How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 20.04 Server
  • How To Set Up vsftpd for a User’s Directory on Ubuntu 20.04
  • How To Install Nginx on Ubuntu 20.04
  • How To Install the Apache Web Server on Ubuntu 18.04
  • How To Reset Your MySQL or MariaDB Root Password ubuntu 16.4

Didn't find your answer? Contact Us

Leave A Comment Cancel reply

Ubuntu
  • How To Install Node.js on Ubuntu 18.04
  • How To Install MySQL on Ubuntu 18.04
  • How to Config Initial Server Setup With Ubuntu 18.04
  • How To Reset Your MySQL or MariaDB Root Password ubuntu 16.4
  • How To Install the Apache Web Server on Ubuntu 18.04
  • How To Install Nginx on Ubuntu 20.04
  • How To Set Up vsftpd for a User’s Directory on Ubuntu 20.04
  • How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 20.04 Server
  • How to Install Jenkins in ubuntu 20.04
All Categories
  • container
  • Ubuntu
  • Debian
  • Linux Basics
  • Almalinux
  • windows server
  • Centos

  How to Config Initial Server Setup With Ubuntu 18.04

How To Reset Your MySQL or MariaDB Root Password ubuntu 16.4  

Support
  • Live chat
  • Knowledge Base
  • Blog
Manual Head Office
Toll free : 1800 572 8782
  • Copyright 2022 Hostzop Expert Solutions. All Rights Reserved.