If you are a serious developer, you won't live without Linux.
Ubuntu is one of the widely used Linux distribution by developers from all around the world.
You can develop a lot of open source software on Ubuntu without any restriction.
Node.js is such one of the best platform which helps to develop network applications.
Today in this article, I am going to show you how to install Node.js on your Ubuntu server.
Node.js is the famous Open source Java Script run time environment. Node.js enables the Java Scripts to run on the server side.
java script is used both server side and client. Leveraging the Java Script on Both front end and back end will lead to the consistent development.
Being a programmer you should have Node.js installed on your Ubuntu Machine. If you are going to install Ghost, you have to have Node.js installed on your server.
Here, in this tutorial we will see How to install Node.js on your Ubuntu Machine in different ways.
There are three methods available to install the Node.js on Ubuntu. They are
- Installing from Repository
- Installing using PPA
- Installing using NVM.
- Ubuntu 16.04 with sudo non root user access( If you don't know how to create the Ubuntu Non root user with sudo permissions, just refer the article.
How to Install Distro- Stable Version for Ubuntu?
Ubuntu 16.04 comes with a default node.js version in its repository.
You can install Node.js from its repository. In this article we are going to see how to install Node.js v4.2.6.
This is not the latest version of Node.js. However, this is sufficient for most of the operations.
First update the package repository and then install Node.js using the below command.
$ sudo apt-get update $ sudo apt-get install nodejs
You just have to look at the package in the repository. If it satisfies your requirement, then follow the above steps.
Sometimes you also need to install NPM( Node.js Package Manager).
After executing the command, you will get the following output.
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libuv1 The following NEW packages will be installed: libuv1 nodejs 0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded. Need to get 3,218 kB of archives. After this operation, 13.4 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://mirrors.digitalocean.com/ubuntu xenial/universe amd64 libuv1 amd64 1.8.0-1 [57.4 kB] Get:2 http://mirrors.digitalocean.com/ubuntu xenial- updates/universe amd64 nodejs amd64 4.2.6~dfsg-1ubuntu4.1 [3,161 kB] Fetched 3,218 kB in 0s (5,102 kB/s) Selecting previously unselected package libuv1:amd64. (Reading database ... 212964 files and directories currently installed.) Preparing to unpack .../libuv1_1.8.0-1_amd64.deb ... Unpacking libuv1:amd64 (1.8.0-1) ... Selecting previously unselected package nodejs. Preparing to unpack .../nodejs_4.2.6~dfsg-1ubuntu4.1_amd64.deb ... Unpacking nodejs (4.2.6~dfsg-1ubuntu4.1) ... Processing triggers for libc-bin (2.23-0ubuntu7) ... Processing triggers for man-db (2.7.5-1) ... Setting up libuv1:amd64 (1.8.0-1) ... Setting up nodejs (4.2.6~dfsg-1ubuntu4.1) ... update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode Processing triggers for libc-bin (2.23-0ubuntu7) ...
To install NPM, execute the below command.
$ sudo apt-get install npm
The Node.js Package Manager helps you to install the modules and packages which is required for Node.js.
While installing the node.js, you have to use nodejs in the command.
==Dont simply use node in the command. ==
Since there is a package in the repository with the same name, the conflict will occur.
So be careful while installing the software.
How to install Node.js using PPA?
If you install the Node.js using the above method, you will only get the v4.2.6. But if you want to install the current version of Node.js, you have to follow this method.
You can download the latest version of node.js from the PPA created and maintained by nodesource. PPA stands for Personal Packages Archive).PPA has the latest version of Node.js.
The Ubuntu repositories only have the v4.x.
You can choose Between v4.x, v6.x and v7.x from the PPA. Before installing, check the validation of The node.js versions.
First, you have to install the PPA. Then only you have to download and install the latest version of Node.js.
Use the below command to install the Latest version of Node.js. Make sure to replace the v6.x with your expected version.
$ cd ~ $ curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh
If you want to look at the content inside the setup file, use any of the editors to open it. ( Here I am using nano)
$ nano nodesource_setup.sh
The PPA will be added to the configuration and the local package cache will be updated.
After the installation of the setup script from nodesecure, you can install the Node.Js by following the above steps.
$ sudo apt-get install nodejs
The Nodejs has the nodejs binary and npm. So, you don't need to install the NPM separately. But we have to install build-essential package.
$ sudo apt-get install build-essential
How to Install Using NVM
In the above method we have seen, how to install Node.js through the Apt commands. But here we are going to see how to install the Node.js using NVM.
NVM is the special app used to install the node.js. This app lets you install your desired version of Node.js.
You can install the latest version of Node.js or you can install the specific version of node.js on your machine. This is all differs based on your app requirement.
Here we are going to get the software packages from the Ubuntu repository which will help us to build the source packages. Execute the below commands.
$ sudo apt-get update $ sudo apt-get install build-essential libssl-dev
After that, get the NVM installation script from Github website. The version may differ. just replace the version with the latest version.
Use the curl to download the script.
$ curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh
After that, inspect the installation script with editor( Here we use nano)
$ nano install_nvm.sh
Now, install the script with bash.
$ bash install_nvm.sh
The above command will install the nvm at the subdirectory of home at ~/.nvm. It also adds the required lines to ~/.profile file.
To make the NVM work, you have to log out and log in again. Instead of that, you can use the below code. It will let the current session to know about the changes.
$ source ~/.profile
That's all. Here you have installed Node.js. Now, you can install the required version of Node.js. To see the available version, Just execute the following commands.
$ nvm ls-remote Output ... v5.8.0 v5.9.0 v5.9.1 v5.10.0 v5.10.1 v5.11.0 v6.0.0
All the available versions will be listed. You can select the one to install. I am choosing the version 6.0 to install.
$ nvm install 6.0.0
Since you have many versions of node.js, the NVM will choose the recently installed version. But you can specify the exact version to use.
$ nvm use 6.0.0
Here, I am instructing the NVM to use version 6.0
If you install node.js using nvm, the executable is called node. To know the latest version used by the shell, just use the following command.
$ node -v Output v6.0.0
The above command will work if you have the single version installed on your machine.
If you have multiple node.js versions installed on your machine, use the below command to see which one is running.
$ nvm ls
It will show you the currently running version. If you like to mention a specific version of node.js.
Just execute the following command by mentioning the version which you want to set by default.
$ nvm alias default 6.0.0
Now the version 6.0 will become default node.js version. You can also use this below command to do the same job.
$ nvm use default
Let us look at the package management now. Each version of Node.js will keep track its packages with the help of NPM. You can make the npm to install the packages at Node.js projects ./node_modules directory.
Here is the example of express module
$ npm install express
If you like to make this same version of Node.js to other projects(Globally), you can use the -g flag. Here is the example command for that.
$ npm install -g express
The above command will install the package in the following path.
$ npm link express
If you like to know more about the nvm, just use the help option.
$ nvm help
Here in this article you have learned
- How to install Node.js on Ubuntu repository
- How to install Node.js using PPA
- How to install Node.js using NVM.
The method of installing node.js will differ based on your requirement. The easiest way is installing Node.js from Ubuntu Repository.
If you still have the doubts or get any error while executing, please let us know that in the comments.