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
- 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.
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.
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
sudo apt install npm
To ensure that you have installed the correct version of Node.js, use the command:
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:
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:
- 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:
- 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:
- In this step, you should bash to run the script. This code will help you do so:
- 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:
- 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:
v12.18.3 (Latest LTS: Erbium)
- 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:
- Check all the downloaded node versions with the command:
- 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:
- Type in the following command to get information about all the available options with nvm:
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.