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?

1
  • 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 1, 2017 at 5:29

3 Answers 3

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.

9
  • 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? Dec 1, 2017 at 8: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 1, 2017 at 8: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. Dec 1, 2017 at 9: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 1, 2019 at 3: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 1, 2019 at 3:51
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.

2
-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.

2
  • 1
    Modifying the standard permissions of the file system should be the last action. Feb 3, 2019 at 23:30
  • 2
    The question explicitly asked for how to do this without sudo so this would fail
    – Simeon
    Apr 24, 2020 at 9:36

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.