10

When installing Node on my machine, I used the below command that I found on a website.

tar --strip-components 1 -xzf /usr/save/node-v4.2.1-linux-x64.tar.gz

I know that tar -xzf is used to extract the archives but I want to know how is node getting installed?

I tried installing it in this way. I've extracted the node files to /usr/local using the below command,

tar /usr/local -xzf /usr/save/node-v4.2.1-linux-x64.tar.gz

But this doesn't work. Can somebody explain or help me to understand the difference between these two commands?

What more is command 1 doing than what command 2 is doing?

  • 1
    You can install jodejs through apt command: sudo apt install nodejs nodejs-legacy. – pa4080 Mar 30 '17 at 7:23
24

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.

| improve this answer | |
14

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.