How to install Node.js on Ubuntu from tar.xz? [SOLVED]


Written By - Omer Cakmak
Advertisement

Getting started with Node.js Installation

Node.js is an open source (MIT License) runtime environment developed for networked applications that run mainly on the server side. Node.js applications are typically developed using JavaScript, a client-side scripting language. Since it is based on Javascript, it has a very dynamic and fast structure.

In this article, we will explain the steps of how to install node.js from tar.xz to Ubuntu but you wish to use any other method to install Node.js then you can refer Install Node.js on Ubuntu 20.04 [3 Different Methods].

But to learn more you can refer to our in-depth Node.js Tutorial.

Let's start the installation of Node.js from tar.gz on Ubuntu step by step.

 

Step-1: Downloading Node.js tar.gz File

The recommended version on the Node.js official website is 18.12.1 LTS. Go to its official address, right click on the download link and copy the link.

install nodejs

 

19.1.0 is available in the latest version at the same address. You can also download and install a different version.

foc@ubuntu22:~$ wget https://nodejs.org/dist/v18.12.1/node-v18.12.1-linux-x64.tar.xz
--2022-11-27 19:08:00--  https://nodejs.org/dist/v18.12.1/node-v18.12.1-linux-x64.tar.xz
Resolving nodejs.org (nodejs.org)... 104.20.23.46, 104.20.22.46, 2606:4700:10::6814:162e, ...
Connecting to nodejs.org (nodejs.org)|104.20.23.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23726988 (23M) [application/x-xz]
Saving to: ‘node-v18.12.1-linux-x64.tar.xz’

node-v18.12.1-lin 100%[=============>]  22.63M  1.85MB/s    in 11s     

2022-11-27 19:08:12 (2.10 MB/s) - ‘node-v18.12.1-linux-x64.tar.xz’ saved [23726988/23726988]

 

Step-2: Unzip the Compressed File

Unzip the tar.gz file to your current directory:

foc@ubuntu22:~$ tar xvfJ node-v18.12.1-linux-x64.tar.xz 
node-v18.12.1-linux-x64/
node-v18.12.1-linux-x64/share/
...
node-v18.12.1-linux-x64/LICENSE
node-v18.12.1-linux-x64/CHANGELOG.md
node-v18.12.1-linux-x64/bin/
...
node-v18.12.1-linux-x64/lib/

 

Step-3: Installing Node.js on the System

Copy the libraries and executables under the /usr directory. In this way, you can access node.js packages without changing the PATH:

Advertisement
foc@ubuntu22:~$ sudo cp -r node-v18.12.1-linux-x64/{bin,include,lib,share}  /usr/

The files are placed in the relevant directories:

foc@ubuntu22:~$ find /usr -name node
/usr/bin/node
/usr/include/node
/usr/share/doc/node

 

Step-4: Check Version

Invoke the node command from the terminal:

foc@ubuntu22:~$ node 
Welcome to Node.js v18.12.1.
Type ".help" for more information.
> 

or

foc@ubuntu22:~$ node --version
v18.12.1

 

Bonus Tip: Create and test a web server!

One of the biggest features that distinguishes Node.js from other languages is that it can create a web server with just a few lines of code.

Create example_server.js file:

foc@ubuntu22:~$ touch example_server.js

Open the file and type the following lines:

foc@ubuntu22:~$ nano example_server.js

var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);

Then run the generated file:

foc@ubuntu22:~$ nohup node example_server.js > node.log 2>&1 &

Now make a request to port 8080:

Advertisement
foc@ubuntu22:~$ wget -q -O - http://127.0.0.1:8080
Hello World!foc@ubuntu22:~$

Great, the server worked!

 

Summary

In this article, we explained how to install Node.js from the tar.gz file. To get help with installed Node.js, run the following command in terminal:

foc@ubuntu22:~$ node --help
Usage: node [options] [ script.js ] [arguments]
node inspect [options] [ script.js | host:port ] [arguments]

Options:
- script read from stdin (default if no
file name is provided, interactive mode
if a tty)
-- indicate the end of node options
--abort-on-uncaught-exception
...

or you can get support from the manual page:

foc@ubuntu22:~$ man node

NAME
node — server-side JavaScript runtime

SYNOPSIS
node [options] [v8-options] [-e string | script.js | -] [--]
[arguments ...]
node inspect [-e string | script.js | - | <host>:<port>] ...
node [--v8-options]

DESCRIPTION
Node.js is a set of libraries for JavaScript which allows it to
be used outside of the browser. It is primarily focused on cre‐
ating simple, easy-to-build network clients and servers.
...

 

References

stackoverflow.com - How to install node.tar.xz file in linux
man page for node

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment