
Node.js is an opensource cross-platform JavaScript run-time environment for server-side execution of JavaScript code. It’s built on Chrome’s JavaScript engine so it can be used to build different types of server-side application. npm is the very popular and default package manager for Node.js with large number of packages. In this tutorial, we will learn how to install Node.js and npm on Ubuntu 18.04 machine.
You can install the Node.js packages using apt from default Ubuntu repository, if your purpose is only to deploy Node.js applications. To install from NVM is the best option if you are using Node.js for the development purposes.
Install Node.js and npm on Ubuntu
There are multiple ways to install Node.js and npm on your Ubuntu 18.04 systems. You can follow any of following to install:
Install Node.js and npm from the Ubuntu repository
By default Node.js
and npm
packages are available from the Ubuntu 18.04 repository. Perform the following steps to install:
At first, you should update the package list by issuing below command:
sudo apt update
Next, install nodejs
using apt package manager by executing below command:
sudo apt install nodejs
Once the installation is completed you can verify it by typing:
nodejs --version
It show output as below:
v8.10.0
Using npm
the package manager for Node.js
, you can download the npm
packages using it. So you should install npm
also by issuing below command:
sudo apt install npm
Verify the installation by type:
npm --version
3.5.2
Install Node.js from NodeSource Repository
NodeSource provides a repository containing latest version of Node.js and npm. It’s very simple to install Node.js and npm from NodeSource.
At first, you need to add NodeSource repository on your system using following curl command:
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
NOTE : The latest LTS version of Node.js is 10.x if you want to install 8.x version then just replace setup_10.x
with setup_8.x
After that, install Node.js and npm with the following command:
sudo apt install nodejs
Verify installation of Node.js and npm running below command:
node --version
v10.13.0
npm --version
6.4.1
Install Node.js and npm using nvm
NVM (Node Version Manager) script is used to manage multiple Node.js versions. You can install or uninstall specific Node.js versions using NVM.
At first, we need to download nvm install script on your system using below command:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
It will copy the nvm
repository from Github to the ~/.nvm directory and add the nvm path to your Bash profile.
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion
As you can see in output, it is suggesting to either open new shell session or run the commands to add the path to the nvm script to your current session.
You can verify that nvm is installed properly by typing:
nvm --version
0.33.11
Now you have nvm installed on your Ubuntu system. You can install the latest available version of Node.js using below command:
nvm install node
It will show output as below:
Downloading and installing node v12.0.0... Downloading https://nodejs.org/dist/v12.0.0/node-v12.0.0-linux-x64.tar.xz… ################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v12.0.0 (npm v6.9.0) Creating default alias: default -> node (-> v12.0.0)
Once the installation is finished you can check the version of Node.js by typing :
node --version
v12.0.0
Next, We will install two more versions of Node.js. To do so type following:
nvm install --lts
nvm install 9.10.1
Once the both of above versions are installed we can view list using below command:
nvm ls
-> v9.10.1
v10.15.3
v12.0.0
default -> node (-> v12.0.0)
node -> stable (-> v12.0.0) (default)
stable -> 12.0 (-> v12.0.0) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> v10.15.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.0 (-> N/A)
lts/dubnium -> v10.15.3
In output version with arrow is used for the current shell session. Default version is set to v12.0.0
and it will be used when you open new shell sessions.
You can change the current default version of Node.js by using the following command:
nvm use 10.15.3
Now verify it by typing:
nvm current
v10.15.3
You also can set set version 10.15.3 as the default Node.js version using below command:
nvm alias default 10.15.3
You can uninstall a Node.js version type following command:
nvm uninstall 9.10.1
Uninstall Node.js and npm
To uninstall Node.js use following command:
sudo apt remove nodejs npm
sudo apt autoremove
To uninstall node.js version using nvm type following command:
nvm uninstall 10.15.3
Conclusion
Finally, you have learned how to install node.js and npm on your Ubuntu 18.04 system with different methods. At the end of tutorial you also learned how to uninstall it.
If you have any question or suggestion please leave comment below.
Leave a Reply