Install Node.js on Linux, Mac, and Windows

· 7 min read
Install Node.js on Linux, Mac, and Windows

If you are an old web programmer, you are familiar with JavaScript to beautify or make the web pages you build more dynamic. Javascript is run in the browser to manipulate web pages, and maybe your website's backend is PHP and MySQL.

In that era, AJAX (Asynchronous JavaScript and XML) emerged, and many JavaScript libraries to make programming more accessible, such as Jquery, Angular JS, and others. But all of that can only be run on the client or browser side; web developers must still understand programming on the server side, such as PHP, ASP, and others.

Therefore, the developers are considering whether Javascript can be run on the server side so that web developers only need to master one programming language, namely Javascript. Then came Node.js, which is currently very popular and has built thousands of websites and applications that can run on multiple platforms.

What is Node.js?

Node.js is an open-source JavaScript runtime environment that allows developers to write server-side code using JavaScript. Node.js is gaining popularity due to its performance, scalability, and cross-platform capabilities, making it even more ideal for developing web applications that run on multiple platforms.

Ryan Dahl first released Node.js in 2009 as an open-source project on GitHub. It incorporates a program interface between JavaScript and the operating system that is non-blocking, allowing it to handle higher requests in less time and with fewer resources.

Node.js is currently the most popular platform for building fast, responsive, and scalable web applications. This allows developers to build applications in the same JavaScript environment they use to create web pages. It also helps developers build applications with increased performance and high productivity.

Comparison of Using Node.js
Comparison of Using Node.js. Source w3techs.com.

Node.js has been used by many large companies such as Netflix, Walmart, and LinkedIn to build their web applications. Developers have also used Node.js to create mobile applications using platforms such as React Native and Cordova. So with Node.js, application developers don't need to learn various programming languages; just Javascript is enough, isn't it interesting?

Node.js has become one of the most popular web and mobile application development platforms. It has become an essential part of the JavaScript ecosystem and is considered one of the most important technologies for developing web-based applications.

Install Node.js on Linux

Installing Node.js on Linux is relatively easy; there are several ways to install Node.js; please choose the easiest way.

Install Using Default Package System.

In the latest operating system version, Node.js has entered the package system; for example, in this case, we are using Ubuntu 20.04 to install and type the following command.

sudo apt update
sudo apt install nodejs
sudo apt install npm

Install With NodeSource PPA

If Node.js is unavailable in the repository, you add the source to your operating system; in this example, we will use the NodeSource PPA.

Type the following command in your Linux terminal;

curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
sudo apt-get install -y npm

Version 19.x It is the latest version of this article; if you want to use an earlier or a newer version, please look at the official NodeSource documentation.

Install Using NVM (Node Version Manager)

Installing using NVM is a bit difficult, but we are given the freedom to choose the Node.js version easily. Type the following command to download the NVM source.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

when finished, edit the ~/.profile file using nano.

nano ~/.profile

Paste the code below in the ~/.profile file.

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

When finished, save the file by pressing the hotkey CTRL+X and pressing button  Y to save. Then make the ~/.profile file as a source by typing.

source ~/.profile

At this stage, your NVM should be running; you can verify it by checking the version of NPM with the following command;

nvm --version

If the output from the above command is the nvm version, your nvm installation was successful. Next, we have to install Node.js and npm. To see the available versions of Node.js, type the following command.

nvm list-remote

All available versions will come out, sample output as below;

   ......
        v17.7.0
        v17.7.1
        v17.7.2
        v17.8.0
        v17.9.0
        v17.9.1
        v18.0.0
        v18.1.0
        v18.2.0
        v18.3.0
        v18.4.0
        v18.5.0
        v18.6.0
        v18.7.0
        v18.8.0
        v18.9.0
        v18.9.1
       v18.10.0
       v18.11.0
       v18.12.0   (LTS: Hydrogen)
       v18.12.1   (Latest LTS: Hydrogen)
        v19.0.0
        v19.0.1
        v19.1.0
        v19.2.0
        v19.3.0

So, we will try to install the latest Node.js, namely version 19.3.0; type the following command to install;

nvm install v19.3.0

Wait until the process is complete.

To verify the installation results, you can check the version of Node.js installed with the following command.

node -v
npm -v

The output is the version of Node.js and npm that was successfully installed on your Linux system.

Install Node.js on Windows

Installing Node.js on a Windows operating system is very easy because Node.js already provides a GUI (Graphic User Interface) installation file, so you can download the installation file on the Node.js download page, select Windows, then download according to the Windows architecture. For example, 64-bit.

Node.js download page
Node.js download page

After the file has been successfully downloaded, open the file by double-clicking, then follow the instructions in the installation process. The installation process will take a while, depending on your computer's and the internet's speed. Check the "Automatically install the necessary tools" option so that the installer can install all the packages needed.

Node.js installation process on Windows
Node.js installation process on Windows

When finished, you can verify the installation results by checking the Node.js version via Command Prompt or Windows Power Shell. By typing;

node -v
npm -v

If the Node.js and npm versions have been released, then the Node.js installation process on Windows has been successful.

Install Node.js on Mac

Installing Node.js on a Macbook is relatively easy because it can be done using homebrew; if your Macbook doesn't have homebrew installed, please do the homebrew installation first. Please type the following command in your Macbook terminal if it's already installed.

brew install node

This command will install the complete Node.js package. The process will take a long time. Please wait until the process is complete; when finished, please verify the installation results by checking the version.

Create the First Node.js Application

Next, we will test the results of the Node.js installation that we have done by creating a simple server program, which will display the words 'Hello Teknotut' in the browser sent via the Node.js server.

Make your project directory, then create a file, for example, server.js, and fill the file with the following code;

//simple nodejs program to display "Hello Teknotut"

//Create server with nodejs
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

//Create Server
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello Teknotut!');
});

//Run a server
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

The program above will create a web server running on port 3000, and when accessed, it will respond with status code 200 (OK), sending a header of type text/plain with the contents 'Hello Teknotut'.

When finished, save the file, then in the file directory, type the command;

node server.js

Hooray, you have successfully created a Node.js server that displays the text 'Hello Teknotut' in your browser when you access http://localhost:3000.

To stop the server, press CTRL+C in your terminal or command prompt.

Easy enough, right?

Frequently Asked Questions

Here are some frequently asked questions regarding Node.js

What is Node.js?

Node.js is an open-source JavaScript platform that can be used to build web server applications in JavaScript. This allows you to use JavaScript outside of the browser and create applications that run on servers.

What are the benefits of using Node.js?

Node.js offers various benefits, including high performance using an asynchronous I/O model, modules easily imported from the community, and high scalability because applications can run on multiple servers.

How to install Node.js?

Node.js can be easily installed on multiple platforms. Detailed instructions for each platform can be seen on the pages of this article.

What are the advantages of writing JavaScript code with Node.js?

Node.js allows you to create web server applications written in JavaScript, which means you can use the same JavaScript code to build web applications without having to write different codes for each platform. It also makes writing JavaScript code faster and easier.

Conclusion

The article above taught us how to install Node.js on Linux, Mac, and Windows in full and in detail. We've covered how to install Node.js on different operating systems, the stages of installation, and some of the problems that may arise. Thus, we can conclude that the Node.js installation process is not complicated and can be done quickly on all operating systems.

I hope this article helps.