Grid Guide Topics
Table of Contents
- Installing the Distro Stable Version
- Installing using a PPA
- Installing using NVM
- Removing Node js
You should have access to an Ubuntu 18.04 server and created a non-root user account with sudo privileges by following our guide, Initial server setup with Ubuntu 18.04.
Installing the Distro-Stable Version
Ubuntu 18.04 contains default version of Node.js in its repositories and it can be used to provide a pleasant user experience across multiple systems. At the time of writing, the version in the repositories may not be the latest version, so it should be stable and sufficient for quick experimentation with the language. You can use the apt package manager to know the version, To refresh your local package index by entering the below command.
$ sudo apt update
To install node.js from the repositories enter the below command.
$ sudo apt install nodejs
If you want to install Node.js packet manager, then you can enter the below command.
$ sudo apt install npm
By entering this, also allows you to install modules and packages to use with Node.js. If you want to check the version of the Node.js you have installed after these steps, you have to enter the below command.
$ nodejs -v
Installing using a PPA
If you want to get the most recent version of Node.js then, you can add the PPA means personal package archive maintained by NodeSource. This contains the most up-to-date versions of Node.js than the official Ubuntu repositories. First, install the PPA to get access to its contents. In your home directory, use curl to retrieve the installation script for your preferred version, and replace 10.x with your preferred version.
$ cd ~ $ curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
You can inspect the contents of this script with nano
$ nano nodesource_setup.sh
Then you can run this script under sudo:
$ sudo bash nodesource_setup.sh
The PPA will be added to your configuration and local package cache will be updated automatically. Once you running the setup from NodeSource, you can install the Node.js package in the same way as described above.
$ sudo apt install nodejs
If you want to check which version of the Node.js you have installed after these initial steps, enter the below command.
$ nodejs -v
The Node.js package contains the node.js binary and npm, So you don’t need to install the npm separately. Npm uses a configuration file in your home directory to keep track of updates. It will be created the first time you run npm.
Run the following command to verify that npm is installed and to create the configuration file:
$ npm -v
If you want to work with npm packages, you need to install the build-essential package.
$ sudo apt install build-essential
Now, you have the necessary tools to work with npm packages that need compiling code from source.
Installing using NVM
You can also install Node.js with apt is to use a tool called nvm, which stands for “Node.js Version Manager”. In spite of working at the operating system level, nvm works at the level of an independent directory within your home directory. You can use multiple self-contained versions of Node.js without affecting the entire system. Controlling your environment with nvm allows you to access the newest versions of Node.js and retain and manage previous releases. It is a different utility than apt, and the versions of Node.js that you manage with it are different with versions manage with apt. To download the nvm installation, you have to use curl.
$ curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh
You can inspect the installation steps with nano, To do this use below command.
$ nano install_nvm.sh
You can run the script with bash, To do this use below command.
$ bash install_nvm.sh
It will install the software into a subdirectory of your home directory at ~/.nvm. It will add the required lines to your ~/. Profile file to use the file. To gain access to the nvm functionality, you will need to either log out and log back in again or source the ~/.profile file so that your current session knows about changes.
$ source ~/.profile
Once nvm installed, you can install isolated Node.js versions. For information about the versions of Node.js enter the below command.
$ nvm ls-remote
The current LTS version at the time of this writing is v8.11.1. You can install by entering the below command.
$ nvm install 8.11.1
Usually, nvm will switch to use the most recently installed version. You can tell nvm to use the version you have it now. This can be done by entering the below command.
$ nvm use 8.11.1
If you install Node.js using nvm, the executable is called node. You can see the version currently used by the shell by entering the below command.
$ node -v
If you have multiple Node.js versions, you can see what is installed, then enter the below command.
$ nvm ls
If you want default versions, enter below commands.
$ nvm alias default 8.11.1
This version will be automatically selected when a new session spawns. You can also refer it by the alias like this:
$ nvm use default
Every version of Node.js will keep track of its packages and has npm available to manage these things. You can also have node.js projects ./node_modules directory. Use the below syntax to install the express module.
$ npm install express
If you want to install the module globally, make sure it available to other projects using the same version of Node.js, you can add the -g flag.
$ npm install -g express
It will install the package in:
Install the module globally will let you execute commands from the command line, but you have to link the package into your local sphere to require it from within a program.
$ npm link express
If you want to know more options available in nvm by entering the below command.
$ nvm help
Removing Node js
If you want to uninstall Node.js then you can do it by using apt or nvm, depends on version. If you want to remove the distro-stable version then you have to work with apt utility at the system level. To do this enter the below command.
$ sudo apt remove nodejs
This command will remove all configuration files. If you don’t want to save the configuration files for later use, enter the below command.
$ sudo apt purge nodejs
This will uninstall the package and remove the configuration files contained it. At last, you may remove all unused packages which are installed automatically. To do this enter the below command.
$ sudo apt autoremove
If you want to uninstall the current version of Node.js that you have enabled using nvm, First you have to know the version.
$ nvm current
If you want the current active version, you can run below command.
$ nvm uninstall node_version
This command will uninstall the selected version of the Node.js. If you want to remove the current active version, you should first deactivate nvm to enable your changes.
$ nvm deactivate
Now you can uninstall the current version using the uninstall command. It will remove all files which are associated with the targeted version of Node.js excluding cache files.
These are the steps to get up and running with Node.js on Ubuntu 18.04.