This post shows users and new students that how to Install Node.js and npm on Ubuntu 18.04 | 20.04. If you want to learn how to install Node.js on ubuntu then this post ideal for you.
Node.js is an open source, back-end, cross-platform which is used to build back-end server applications using JavaScript runtime environment. Node.js executes JavaScript code outside a Browser.
npm is the default package manager for the JavaScript runtime environment Node.js and it is the world’s largest software registry.
For more information about Node.js, please visit it’s official website.
there are many ways to install Node.js and npm on Ubuntu
Install Node.js and npm from NodeSource
Install Node.js from the NodeSource repository is the simplest way to install. If you need Node.js as local runtime for your Node.js Application then install it from the NodeSource repository.
To install Node.js from NodeSource repository, Run the below commands to add the specific version that you want to install.
For the Latest release (version 14), add this PPA.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
For the version 12, Run the below command :
curl -sL https://deb.nodesource.com/setup_12.x | bash -
To install version 10, Run the below command :
curl -sL https://deb.nodesource.com/setup_10.x | bash -
After above steps , you will now install latest version of Node.js from the particular repository you choose.
Install Node.js and npm
Run the commands below for the install
sudo apt install nodejs
Use the below commands to view the version of installed Node.js and npm
node --version
npm --version
The above commands will show the current installed versionof both Node.js and Npm.
Install Node.js and npm via Snap
It is a another way to install Node.js is using Snap package. This is also a easiest way to to install Node.js and npm.
To install using Snap package, Run the below commands to install Snap.
sudo apt update
sudo apt install snapd
Run the commands below :
For the version 14 –
sudo snap install node --channel=14/stable --classic
For the version 13 –
sudo snap install node --channel=13/stable --classic
To install the LTS version 10 –
sudo snap install node --channel=10/stable --classic
Run the below commands to create a test file called http_server.js in your home folder.
cd ~/
nano http_server.js
Now copy and paste the below content into the http_server.js file and save it.
const http = require('http');
const hostname = '127.0.0.1';
const port = 4000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello Worldn');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
After save the file and run the below commands to start the server.
node http_server.js
When you run the above command, you will see an output like below lines :
Server running at http://127.0.0.1:4000/
Now open your browser and browse for http://localhost:4000
When you browse it will show result like below :
That’s all
If you find any error and issue in above steps , please use comment box below to report.