After downloading node, where to place it correctly?

When i go to https://nodejs.org/en/ i can download the linux tar.xz file (I understand this is not what is called a package).

I open the downloaded file in the Ubuntu extractor and find a directory with subdirectories bin, lib ... In bin is the node binary node. Where do I place now this node parent directory in my system and how do I correctly link it such that entering node on the command line calls this new node binary?


Solution 1:

It is recommended to install nodejs through the setup script , your system will be able to install the security update through apt. As you can see the warning on the official website:

Important security releases, please update now!

To install nodjs 8.x :

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt install -y nodejs

To install nodjs 9.x :

curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
sudo apt install -y nodejs

To answer your question you can install the tarball as follow:

wget https://nodejs.org/dist/v8.9.3/node-v8.9.3-linux-x64.tar.xz
sudo mkdir /usr/lib/nodjs
sudo tar xvf node-v8.9.3-linux-x64.tar.xz -C /usr/lib/nodjs

It will extract the tarball to /usr/lib/nodjs . rename node-v8.9.3-linux-x64 to node:

sudo mv /usr/lib/nodjs/node-v8.9.3-linux-x64 /usr/lib/nodjs/node

Run the following command :

export NODEJS_HOME=/usr/lib/nodejs/node
export PATH=$NODEJS_HOME/bin:$PATH

You can add the above commands to your ~/.bashrc then run source ~/.bashrc.

Nodejs help: How to install Node.js via binary archive on Linux?