15

I have little knowledge of Ubuntu 14.04.

I need to install Node.js. The Ubuntu I am using is a big system for an organization so I don't have sudo access, but I found that npm 1.3.10 is installed.

I am looking for a sequence of commands to install Node.js into my user directory. I have downloaded Node.js from here on nodejs.org (LTS version, 64 bit) in ~/Downloads/node-v8.9.1-linux-x64.tar.xz. What do I do next?

Zanna
  • 70,465
  • 3
    @MichaelBay Node.js can also be installed locally without having to use sudo and without having to contact the IT department in order to get authorization to install Node.js globally. – karel Dec 01 '17 at 05:29

3 Answers3

19

In order to install Node.js and npm locally without having to use sudo open the terminal and type:

echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
wget -c http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install 
wget -c https://www.npmjs.org/install.sh | sh  

The curl package is not installed in Ubuntu by default. If you don't have curl installed on your system, replace all instances of curl in the install.sh file with wget -c and save the changes to the install.sh file before running it.

This will install node-v9.2.0 which is a later version of Node.js than the file you already downloaded.

karel
  • 114,770
  • Thank you so so much, karel. I not only know how to install Node.js, but also learnt a general method to install a software into my user folder from source. Only two things to mention, 1) first, I have an existing ~/.bashrc, so I added the path in geditor manually and re-login. 2) I don't understand the 6th command so I run them separately: first wget ... second tar -xzf ... and finally cd into the unzipped folder. I don't understand the last command either. Does it mean I download install.sh from that url and run it? – user5280911 Dec 01 '17 at 08:15
  • What you did with existing ~/.bashrc is OK. Regarding the last command, it does download the install.sh file from the url and run it, in addition the install.sh file doesn't even need to have executable permissions because you are running the command as a regular user, not with sudo. – karel Dec 01 '17 at 08:35
  • 1
    I got it. Thank you. I'm sorry I cannot up-vote your answer because my reputation point is not enough, but I will do that once I can. Thank you again for your help. – user5280911 Dec 01 '17 at 09:09
  • Is there a way to achieve this when you don't have access to a C compiler - perhaps using the binaries? – Simon East Apr 01 '19 at 03:32
  • GitHub user isaacs the inventor of npm wrote a few scripts for techniques to install node and npm without having to use sudo: node-and-npm-in-30-seconds.sh. Note: npm >=0.3 is safer when using sudo. Please don't do this if you don't know what it does! – karel Apr 01 '19 at 03:51
  • doesn't work as new tar balls don't use gz compression. Use just straight up x instead of xz – Mattwmaster58 Sep 14 '21 at 03:45
  • @Mattwmaster58 I will try to update my answer if you provide me with a link to a new tarball that doesn't use gz compression so that I have something to test my suggested edit to this answer with. When I searched for a suitable file I found only the same original http://nodejs.org/dist/node-latest.tar.gz file. – karel Sep 14 '21 at 08:06
  • I had issues extracting it and assumed it wasn't gz compressed, it is strange how the latest is named what it is given that information. I fixed the problem by using tar x instead of tar xz – Mattwmaster58 Sep 14 '21 at 20:17
  • I downloaded the http://nodejs.org/dist/node-latest.tar.gz file and the extraction was completed successfully. – karel Sep 14 '21 at 23:38
3

I workout this way - in 2 steps.

Step 1: Download and extract nodejs binaries

# create a directory where you want to install node js
mkdir ~/nodejs-latest

# download and extract nodejs binaries into the created directory
cd ~/nodejs-latest
wget -c http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1


Step 2: Set PATH and source
# append the following lines to the ~/.bashrc file
export NODE_HOME=~/nodejs-latest
export PATH=$PATH:$NODE_HOME/bin

# refresh environment variables
source ~/.bashrc

You can then verify the nodejs installation with node --version and npm --version.

Ramvignesh
  • 1,812
  • 6
  • 20
  • 31
  • 1
    I think your command is downloading the Node source files (uncompiled). Perhaps you intend to download the binaries? – Simon East Apr 01 '19 at 03:45
  • Looks like the binaries can be downloaded via: https://nodejs.org/en/download/ e.g. https://nodejs.org/dist/v12.18.0/node-v12.18.0-linux-x64.tar.xz – Mr. Polywhirl Jun 04 '20 at 12:01
-1

I like to use ubuntu groups to achieve this. It's quite simple.

  1. First install nodejs and npm using apt-get

    sudo apt-get update && sudo apt-get install nodejs npm

  2. Figure out who is logged in i.e username, run following command to see it in terminal

    whoami

  3. You can see the list of groups you are assigned by using a very simple command, normally the first group is your username itself

    groups

  4. Run following to allow access to logged in user

    sudo chmod 777 -R /usr/local && sudo chgrp $(whoami) -R /usr/local

  5. Update npm and nodejs

    npm install -g npm

You are allset, your user can run npm commands without sudo

You can also refer to npm throws error without sudo.