How does tar command work for installing Node js

The tarball directory structure is like this:

$ tar tf node-v6.10.1-linux-x64.tar.xz | head
node-v6.10.1-linux-x64/
node-v6.10.1-linux-x64/bin/
node-v6.10.1-linux-x64/bin/npm
node-v6.10.1-linux-x64/bin/node
node-v6.10.1-linux-x64/share/
node-v6.10.1-linux-x64/share/man/
node-v6.10.1-linux-x64/share/man/man1/
node-v6.10.1-linux-x64/share/man/man1/node.1
node-v6.10.1-linux-x64/share/systemtap/
node-v6.10.1-linux-x64/share/systemtap/tapset/

When you extract this archive without any other options in /usr/local, you get this:

/usr/local/node-v6.10.1-linux-x64/
/usr/local/node-v6.10.1-linux-x64/bin/
/usr/local/node-v6.10.1-linux-x64/bin/npm
/usr/local/node-v6.10.1-linux-x64/bin/node
/usr/local/node-v6.10.1-linux-x64/share/
/usr/local/node-v6.10.1-linux-x64/share/man/
/usr/local/node-v6.10.1-linux-x64/share/man/man1/
/usr/local/node-v6.10.1-linux-x64/share/man/man1/node.1
/usr/local/node-v6.10.1-linux-x64/share/systemtap/
/usr/local/node-v6.10.1-linux-x64/share/systemtap/tapset/

So, a new directory is created in /usr/local, and the files get dumped there.

However, with --strip-components=1, one directory component from the extracted path is removed, so node-v6.10.1-linux-x64/bin/ becomes bin/ and node-v6.10.1-linux-x64/bin/npm becomes bin/npm:

/usr/local/
/usr/local/bin/
/usr/local/bin/npm
/usr/local/bin/node
/usr/local/share/
/usr/local/share/man/
/usr/local/share/man/man1/
/usr/local/share/man/man1/node.1
/usr/local/share/systemtap/
/usr/local/share/systemtap/tapset/

And /usr/local/bin is already in PATH, so you don't need to do anything else to execute npm and node.


This is sort of a cool (yet annoying) way of installing NodeJS.

If you run tar tf /usr/save/node-v4.2.1-linux-x64.tar.gz on the file, you'll see something like this:

node-v4.2.1-linux-x64/
node-v4.2.1-linux-x64/bin/
node-v4.2.1-linux-x64/bin/npm
node-v4.2.1-linux-x64/bin/node
node-v4.2.1-linux-x64/share/
node-v4.2.1-linux-x64/share/man/
node-v4.2.1-linux-x64/share/man/man1/

Basically, this means that when you extract this tar archive, it'll extract to a folder called node-v4.2.1-linux-x64 with all of these subfolders (and the node installation) inside of it. In fact, you can even try this extraction to get a better idea:

mkdir /tmp/node
cd /tmp/node
tar xvf /usr/save/node-v4.2.1-linux-x64.tar.gz

If you run ls, you'll see a node-v4.2.1-linux-x64 folder.

Now, --strip-components 1 does something interesting to the extraction process. From man tar:

 --strip-components=NUMBER
       strip NUMBER leading components from file names on extraction

Basically, this means that when tar is going to extract your archive, it's going to pretend like the node-v4.2.1-linux-x64 folder isn't there. Instead, it's going to extract bin/, share/ and all the other folders directly.

In fact, you can try it:

mkdir /tmp/node
cd /tmp/node
tar xvf /usr/save/node-v4.2.1-linux-x64.tar.gz --strip-components=1

If you run ls, you'll see there's no longer a node-v4.2.1-linux-x64 folder. It's just bin/, include/, lib/, and share/ (all coincidentally folders in /usr/local/).

Your second command wouldn't have worked because it would have just extracted the node-v4.2.1-linux-x64 folder to /usr/local (if it even ran at all). If you run ls /usr/local, you might even see this folder hanging around. It's useless, feel free to delete with rm. As for why it's useless, keep reading...


Now that we've explained how the tar command works, we can explain how this gets installed.

Every Linux system has something called the $PATH variable, which determines where executable files are stored. Among these places is /usr/local/bin. When you extract that binary inside /usr/local (which I'm confident is what your install instructions say), the NodeJS binary is being written to /usr/local/bin/node as per how extractions are done. Similarly, all the libraries are being added to the local library folder and everything pretty much just goes where it belongs.

Now, the caveat (and why this is annoying) is that apt won't see or understand or realize what's going on. You won't be able to update it through sudo apt upgrade or similar. You'd need to manually go in and clean the old NodeJS install and then put in the new one in case you ever want to upgrade.

I would recommend you just run sudo apt install nodejs-legacy instead. Less pain, and it automatically updates for you.