Methods of Installing Node.js and npm on Ubuntu Server?

By | June 21, 2022
Methods of Installing Node.js and npm on Ubuntu Server

What can be better than a platform that allows you to run a specific JavaScript code without a web browser? Well, being a reliable JavaScript environment, Node.js does the work decently. This special feature makes Node.js a great choice for JavaScript developers. Besides, the collaboration of Node.js with npm makes JavaScript coding way easier. Needless to say that installing Node.js on Ubuntu offers loads of benefits to Ubuntu users. Especially the latest Ubuntu 18.04 version, which has already been popular among developers and JavaScript writers. Installing Node.js on the latest version of Ubuntu enables the user to utilize the features of the OS in better ways.

However, you need to gather adequate knowledge about Node.js and npm before installing the open-source environment on Ubuntu. Though the process is not that hard, you need to execute the entire process consciously to avoid flaws. Beforehand, let’s discuss Node.js and npm so that you don’t face difficulties while installing these applications as every Ubuntu user is not tech-savvy. So, let’s move on to the next phases of the article.

Introduction to Node.js

At present, no server-side environment has earned as much popularity as Node.js. It is a server-side environment with loads of programming benefits. Being an open-source environment, Node.js gained popularity among developers as this is accessible for all. Moreover, Node.js has shortened the time frame required to write JavaScript programming. Let’s have a glance at why you should go for Node.js over PHP or ASP.

  • Anyone with the basic knowledge of JavaScript can learn using Node.js early.
  • The scalability of Node.js is beyond question. There is no doubt that the environment is way more scalable than other server-side environments.
  • MVP development becomes way more convenient and straightforward as you go for Node.js over other server-side platforms.
  • Node.js is suitable for platforms and operating systems.

As you see, Node.js is beneficial from every aspect in terms of JavaScript programming and development. So, we suggest you start using this convenient server-side environment to run your JavaScript programs on Ubuntu.

npm in a Nutshell

Using Node.js becomes way easier because of its official package manager npm. Being the world’s biggest software registry software, consisting of 8 lakh code packages, npm is the best library to date. npm makes it easier and more convenient to install Node.js on Ubuntu. The package installer is easily accessible and that’s why it got a preference from programmers and developers.

Now, let’s jump into the main procedure of installing Node.js and npm on Ubuntu.

How to Install Node.js and npm on Ubuntu?

Multiple methods are available for installing Node.js and npm on the Ubuntu server. Apart from the simple approach towards installing Node.js and Ubuntu, there are other flexible methods to do so. We are going to discuss all those methods in the following segments.

Prerequisites

If you’re using Ubuntu 18.04, make sure you have the access to a non-root account that already has sudo privileges set up. Generally, this set-up isn’t an in-built feature of the Ubuntu OS and you’re likely to set up your server manually.

Conventional Method to Install the Distro-stable Version on Ubuntu system

Did you know that a version of Node.js comes with the Ubuntu OS as an in-built feature? The version comes as a default depository with Ubuntu systems. The in-built version of Node.js, which comes as an in-built repository, isn’t the latest version of the server-side environment, but it does the job. Generally, the 8.10.0 version turns out to be available in the default repository of Node.js.

You may use the apt package manager to get the latest version of the Node.js repository and you need to refresh your local package index by using the following command:

sudo apt update

Now, input the following command to install Node.js from the local repository:

sudo apt install nodejs

Generally, the package available in the repository turns out to be efficient for writing JavaScript by an individual. However, downloading the npm package manager to manage the Node.js repository is a better way. Doing that becomes easier by entering this command:

sudo apt install npm

To ensure that you have installed the correct version of Node.js, use the command:

nodejs -v

If you’re satisfied with the version of Node.js that you’ve installed from the Ubuntu repository, start using Node.js with the help of the npm package on your Ubuntu server.

How to Use PPA to Install Node.js and npm in the Ubuntu Server?

There’s no reason to use an outdated version of Node.js when you can install the latest version of Node.js using the PPA. NodeSource executes the responsibility of maintaining PPA or personal package archives. This method will allow you to download all the latest and upgraded versions of Node.js.

Let’s have a close look into the entire process:

  • No need to mention separately that you need to download the PPA to access the packages in it. You have to use curl in terms of retrieving your desired version’s installation script. As the installation script appears on the screen, you must replace the 10.x with the string of the version you want to install. Use the following command for that:

cd ~

curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh

  • You have the option to look into all the contents of the script with this command:

nano nodesource_setup.sh

  • Input this command to run the script under sudo:

sudo bash nodesource_setup.sh

  • Congratulations! You have successfully added the PPA to your system. The process also automates the updating of your system’s local package cache.
  • Now, install Node.js and npm in your system as mentioned in the above method. The procedure is the same in this method too.
  • It’s better to verify whether npm is installed in your device by using the following command:

npm -v

Output

6.14.6

  • If you find that the npm package is not functioning as it should, use this command to install the build-essential package:

sudo apt install build-essential

  • That’s all. You’re done.

Method of Installing Node.js and npm with NVM

Installing Node.js using the nvm tool has been very popular recently. The best thing about the nvm tool is that you get the chance to install the latest version of Node.js and retain the older versions in your system simultaneously. Having the nvm tool installed means that you have a separate independent directory under the main home directory. Go through the following to understand the process in a better way.

  • It’s the best choice to use curl for downloading nvm installation scripts from GitHub. You need to input the following code to perform this:

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o install_nvm.sh

  • Now, it’s time to verify the installation using nano. Input this:

nano install_nvm.sh

  • In this step, you should bash to run the script. This code will help you do so:

bash install_nvm.sh

  • Don’t look for the nvm package in your home directory, rather check for a subdirectory named ~/.profile. To access the nvm package, log out of the session. After that, login again. Else, you can use the code below to inform your current session about the changes that happened in the system:

source ~/.profile

  • As the installation process has been completed, you have the option to install isolated Node.js versions separately. This command will let you know all the information regarding all the available Node.js versions:

nvm ls-remote

Output

v12.18.3 (Latest LTS: Erbium)

v13.0.0

v13.0.1

v13.1.0

v13.2.0

v13.3.0

v13.4.0

v13.5.0

v13.6.0

v13.7.0

v13.8.0

v13.9.0

v13.10.0

v13.10.1

v13.11.0

v13.12.0

v13.13.0

v13.14.0

v14.0.0

v14.1.0

v14.2.0

v14.3.0

v14.4.0

v14.5.0

v14.6.0

v14.7.0

  • You will be able to see the latest LTS version in the output and you can install that version by inputting:

nvm install 12.18.3

  • Typing the following command lets the nvm know about the Node.js version you’re using:

nvm use 12.18.3

  • You can also inspect which version of the node the shell is accessing by inputting this command:

node -v

  • Check all the downloaded node versions with the command:

nvm ls

  • To set one of the nodes as default in your system, use the command:

nvm alias default 12.18.3

Now, let’s come to installing npm using this method.

  • Firstly, you need to use this syntax so that the express module gets installed in your system:

npm install express

  • With the following command, you can install the module globally. That’s how you make the module available for the same Node.js versions.

npm install -g express

  • The package will get installed as:

~/.nvm/versions/node/12.18.3/lib/node_modules/express

  • Type in the following command to get information about all the available options with nvm:

nvm help

Conclusion

So, now you know all about the installation procedure of Node.js and npm on the Ubuntu server. All these methods are vastly used by programmers and developers. Needless to say that Node.js and npm collectively help your Ubuntu system perform different types of programming tasks. Being an excellent server-side environment, Node.js is extremely beneficial and friendly for beginners. If you have stepped into the programming industry recently, there is nothing better than Node.js, and installing this environment on your Ubuntu server makes your user experience way better. So, consider using Node.js and npm on your Ubuntu server following the step-by-step guide mentioned above.

Leave a Reply

Your email address will not be published.