How to Install Node.js on Ubuntu

Node.js, a JavaScript-based platform, is an increasingly popular, lightweight, and efficient option for developers. It allows coders to use JavaScript on both the front-end and the back-end thanks to Google’s awesome V8 engine.

With more than 100,000 packages, Node package manager, or npm, is a fantastic way to find open-source solutions for extending Node and npm’s semantic versioning system, which allows for very tight control over what packages you are using.

5 Ways to Install Node.js on Ubuntu

There are several ways to do this, but we recommend Option 1: Node Version Manager (nvm). Here is the full list of options:
Option 1 is our recommended method for everyone. Option 2 is incredibly simple, while Options 3, 4, and 5 have the advantage of keeping your Node and npm packages the most current. Before trying any of these install options, you’ll want to remove the old Node package to avoid conflicts. And for any more information on Node.js, you can always check out the Github repository.

Before You Get Started: Remove Old Node Package to Avoid Conflicts

On Ubuntu, the Node.js package has a similar name to the older version, Node. The latter is an amateur packet radio program you can more than likely remove.
If you already have Node installed, you might want to remove it. Some Node.js tools might execute Node.js as Node instead of Node.js, causing conflicts.
You can look for and remove the Node package by executing these commands in a terminal. To access a terminal, navigate through the desktop menu:
Applications → Accessories → Terminal
Run this command and if it says install in the right column, Node is on your system:
If you found the old Node package installed, run this command to completely remove it:

Option 1: Install Node.js with Node Version Manager

First, make sure you have a C++ compiler. Open the terminal and install the build-essential and libssl-dev packages if needed. By default, Ubuntu does not come with these tools — but they can be installed in the command line.
Use apt-get to install the build-essential package:
Employ a similar process to get libssl-dev:
You can install and update Node Version Manager, or nvm, by using cURL:
You will be asked to close and reopen the terminal. To verify that nvm has been successfully installed after you reopen the terminal, use:
That command will output nvm if the installation worked.
To download, compile and install the latest version of Node:
In any new shell, you’ll need to tell nvm which version to use:
To set a default Node.js version to be used in any new shell, use the alias default:
Not only does nvm allow you to run newer versions of Node.js and npm, you can install and migrate any desired versions you’d prefer. Go to the nvm GitHub repository for more information.

Option 2: Install Node.js with Ubuntu Package Manager

To install Node.js, type the following command in your terminal:
Then install the Node package manager, npm:
Create a symbolic link for node, as many Node.js tools use this name to execute.
Now we should have both the Node and npm commands working:

Option 3: Install Node.js with Maintained Ubuntu Packages

Add the Node.js-maintained repositories to your Ubuntu package source list with this command:
Then install Node.js with apt-get:
Optionally we can create a symbolic link for node (for reasons mentioned earlier):
Using this install option, we end up with newer versions of Node.js and npm:

Option 4: Install Node.js with Standard Binary Packages

Go to the official Node.js download page and download either the 32-bit or 64-bit Linux binary file, depending on your system type.
You can determine the CPU architecture of your server with these commands:
You can download the file from the browser or from the console. The latter is shown below (Note: the specific Node.js version might be different for you):
To make sure you can unpack the file, install xz-utils:
Next, execute the following command to install the Node.js binary package in /usr/local/:
You should now have both Node.js and npm installed in /usr/local/bin. You can check this with:

Common Errors: 

If you enocounter this following error ( permission error ) then run the following command to fix this error:

#  npm config set unsafe-perm=true

Error Details:
 ubuntu@swarmlab:~$ sudo npm install -g aws-sam-local
[sudo] password for ubuntu:> aws-sam-local@0.2.2 postinstall /usr/local/lib/node_modules/aws-sam-local
> go-npm install

Downloading from URL: https://github.com/awslabs/aws-sam-local/releases/download/v0.2.2/sam_0.2.2_linux_amd64.tar.gz
fs.js:681
  return binding.rename(pathModule._makeLong(oldPath),
                 ^

Error: EACCES: permission denied, rename 'bin/sam' -> '/usr/local/bin/sam'
    at Error (native)
    at Object.fs.renameSync (fs.js:681:18)
    at /usr/local/lib/node_modules/aws-sam-local/node_modules/go-npm/bin/index.js:62:12
    at /usr/local/lib/node_modules/aws-sam-local/node_modules/go-npm/bin/index.js:51:9
    at ChildProcess.exithandler (child_process.js:204:7)
    at emitTwo (events.js:87:13)
    at ChildProcess.emit (events.js:172:7)
    at maybeClose (internal/child_process.js:821:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
npm ERR! Linux 4.4.0-62-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "aws-sam-local"
npm ERR! node v4.2.6
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE

npm ERR! aws-sam-local@0.2.2 postinstall: `go-npm install`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the aws-sam-local@0.2.2 postinstall script 'go-npm install'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the aws-sam-local package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     go-npm install
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs aws-sam-local
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls aws-sam-local
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/ubuntu/npm-debug.log

Comments

Popular Posts