Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install node binary distribution files on Linux

Tags:

linux

node.js

My production server (Centos 5.9) won't compile nodejs, possibly because it's gcc is only 4.1.2 (4.2 or above is recommended) so I've trying to install the binaries.

$ wget http://nodejs.org/dist/v0.10.22/node-v0.10.22-linux-x86.tar.gz
$ tar -zxvf node-v0.10.22-linux-x86.tar.gz
$ cd node-v0.10.22-linux-x86
$ sudo cp bin/* /usr/local/bin
$ sudo cp -R lib/* /usr/local/lib
$ sudo cp -R share/* /usr/local/share

And now for testing:

$ node -v  # => v0.10.22
$ man node # looks fine
$ npm -v   # UH OH, PROBLEM - Cannot find module 'npmlog'

Now (keeping in mind I'm a complete beginner at node) I did some searching and found there's an environment variable called NODE_PATH, so I tried:

$ export NODE_PATH=/usr/local/lib/node_modules
$ npm -v   # SAME PROBLEM - Cannot find module 'npmlog'

So then I found out where npmlog lives and tried modifying NODE_PATH accordingly:

$ find /usr/local/lib -name npmlog # => /usr/local/lib/node_modules/npm/node_modules/npmlog
$ export NODE_PATH=/usr/local/lib/node_modules/npm/node_modules
$ npm -v   # DIFFERENT PROBLEM - Can't find '../lib/npm.js'

At this stage, after more unhelpful googling, I decided I was in over my depth and decided to ask for help. Can anyone tell me what I'm doing wrong?

like image 608
sanichi Avatar asked Nov 17 '13 09:11

sanichi


3 Answers

It is much faster to do clean NPM reinstall which will remove "broken" links:

wget https://npmjs.org/install.sh

chmod +x install.sh
sudo ./install.sh

Then it will ask you to remove old NPM link

like image 55
Anthony Akentiev Avatar answered Nov 09 '22 09:11

Anthony Akentiev


Using Node Version Manager

Use a Node version manager like nvm to handle installation and version management for you. After you install nvm you can simply install any Node version, for example nvm install 8.

But if you just want to install the binary yourself, see below:

Using apt-get

In special cases where you need a system wide Node installation, you can use apt-get:

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

The above snippet will install the latest Node 8.

Installing the Binary Manually

In order to install the binary manually, all you need to do is to download the binary and create a bunch of symbolic links. Execute the commands below one after the other, and it should do the job. I have also written a shell script that does it for you if that is easier (see the bottom of the answer). Hope that helps.

Make sure to use the correct download link for your OS architecture (i.e. either 32-bit or 64-bit) for wget on the second line.

ME=$(whoami) ; sudo chown -R $ME /usr/local && cd /usr/local/bin #adding yourself to the group to access /usr/local/bin
mkdir _node && cd $_ && wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz -O - | tar zxf - --strip-components=1
ln -s "/usr/local/bin/_node/bin/node" .. # Making the symbolic link to node
ln -s "/usr/local/bin/_node/lib/node_modules/npm/bin/npm-cli.js" ../npm ## making the symbolic link to npm

Here is a shell script that downloads and installs all the components. If you use this script to install Node, you can use the uninstall script to uninstall it.

Installing Node

#! /bin/bash
# run it by: bash install-node.sh
read -p " which version of Node do you need to install: for example 8.11.4 (or any other valid version): " VERSIONNAME
read -p " Are you using a 32-bit or 64-bit operating system ? Enter 64 or 32: " ARCHVALUE
if [[ $ARCHVALUE = 32 ]]
    then
    printf "user put in 32 \n"
    ARCHVALUE=86
    URL=https://nodejs.org/dist/v${VERSIONNAME}/node-v${VERSIONNAME}-linux-x${ARCHVALUE}.tar.gz

elif [[ $ARCHVALUE = 64 ]]
    then
    printf "user put in 64 \n"
    ARCHVALUE=64
    URL=https://nodejs.org/dist/v${VERSIONNAME}/node-v${VERSIONNAME}-linux-x${ARCHVALUE}.tar.gz

else
    printf "invalid input expted either 32 or 64 as input, quitting ... \n"

    exit
fi

# setting up the folders and the the symbolic links
printf $URL"\n"
ME=$(whoami) ; sudo chown -R $ME /usr/local && cd /usr/local/bin #adding yourself to the group to access /usr/local/bin
mkdir _node && cd $_ && wget $URL -O - | tar zxf - --strip-components=1 # downloads and unzips the content to _node
cp -r ./lib/node_modules/ /usr/local/lib/ # copy the node modules folder to the /lib/ folder
cp -r ./include/node /usr/local/include/ # copy the /include/node folder to /usr/local/include folder
mkdir /usr/local/man/man1 # create the man folder
cp ./share/man/man1/node.1 /usr/local/man/man1/ # copy the man file
cp bin/node /usr/local/bin/ # copy node to the bin folder
ln -s "/usr/local/lib/node_modules/npm/bin/npm-cli.js" ../npm ## making the symbolic link to npm

# print the version of node and npm
node -v
npm -v

Uninstalling Node

#! /bin/bash
# run it by: ./uninstall-node.sh
sudo rm -rf /usr/local/bin/npm
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/local/lib/node_modules/
sudo rm -rf /usr/local/include/node/
sudo rm -rf /usr/local/share/man/man1/node.1
sudo rm -rf /usr/local/bin/_node/ 
like image 42
AJ Meyghani Avatar answered Nov 09 '22 08:11

AJ Meyghani


I had a problem like that, but with iojs. However it should be the same procedure:

(Assuming that you've got a file matching node-v*-linux-x64.tar.gz in your current directory):

# In case of iojs you need to replace the occurrences of 'node' with 'iojs'
# Extract the downloaded archive with the linux-x64 version of node
tar zxf node-v*-linux-x64.tar.gz
# Move the extracted folder (./node-v*-linux-x64/) to /opt/
mv ./node-v*-linux-x64/ /opt/

To make the binary files available in your shell, create some softlinks inside the /usr/bin/ directory:

# Create a softlink to node in /usr/bin/
ln -s /opt/node-v*-linux-x64/bin/node /usr/bin/node
# Create a softlink to npm  in /usr/bin/
ln -s /opt/node-v*-linux-x64/bin/npm  /usr/bin/npm
# Create a softlink to iojs in /usr/bin (this step can be omitted if you're using node)
ln -s /opt/node-v*-linux-x64/bin/iojs /usr/bin/iojs

Notice: If you'd like to access the cli of some globally installed node modules (for example bower, typescript or coffee-script), you're required to create a softlink to each of those executables in the /usr/bin/ directory.

Alternatively you could just add the bin directory of your node installation directory (e.g. /opt/node-v*-linux-x64/) to the PATH environment variable: (you should use the absolute path for this!)

# create a new .sh script in /etc/profile.d which adds the directory to PATH
echo "export PATH=$PATH:/opt/node-v0.12.3-linux-x64/bin" > /etc/profile.d/node-npm.sh

This change will take effect after logging out and in again.

Both methods worked for me (I use a linux desktop version of Ubuntu 14.04/15.04 with GNOME 3).

like image 12
machinateur Avatar answered Nov 09 '22 10:11

machinateur