47

I want to install latest Node.js version

n latest

  installing : node-v14.2.0
       mkdir : /usr/local/n/versions/node/14.2.0
mkdir: cannot create directory ‘/usr/local/n’: Permission denied

  Error: sudo required (or change ownership, or define N_PREFIX)

Something is wrong with ownership. I tried to fix this

sudo chown -R $(whoami) /usr/local/n

Same error again.

n latest
cp: cannot create directory '/usr/local/lib/node_modules': Permission denied
cp: cannot create regular file '/usr/local/bin/node': Permission denied
cp: cannot create symbolic link '/usr/local/bin/npm': Permission denied
cp: cannot create symbolic link '/usr/local/bin/npx': Permission denied

Permissions in n

/usr/local/n$ ll
total 12
drwxrwxr-x  3 miki root 4096 мај  8 13:29 ./
drwxr-xr-x 11 root root 4096 мај  8 13:29 ../
drwxrwxr-x  3 miki miki 4096 мај  8 13:29 versions/

SOLVED

sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
Richard Rublev
  • 7,718
  • 16
  • 77
  • 121
  • You tried change owner for `~/.npm`, but it says that it can not create directory in `/usr/local/n`. – Eugene Obrezkov May 08 '20 at 11:25
  • So what should I do? – Richard Rublev May 08 '20 at 11:27
  • 1
    Well, obviously, change the owner for `usr/local/n`... `sudo chown -R $(whoami) /usr/local/n` – Eugene Obrezkov May 08 '20 at 11:28
  • 1
    [Never](https://stackoverflow.com/questions/10075990/upgrading-node-js-to-latest-version/42690662#42690662) [do](https://stackoverflow.com/questions/8191459/how-do-i-update-node-js/19584407#19584407) `sudo n latest`! It will mess with permissions and break udpates for the rest of your global npm packages. `/usr/local/n` is a bad choice from a user perspective. Use a [3rd-party installer](https://github.com/tj/n#third-party-installers) to get a working setup out of the box. – cachius Jun 02 '23 at 08:20

4 Answers4

68

Context

n command downloads and installs to /usr/local by default, creating the /usr/local/n folder with these permissions by default:

drwxr-xr-x root  wheel  .
drwxr-xr-x root  wheel  ..
drwxr-xr-x root  wheel  versions

Possible solutions

A. Add yourself to the group and grant write permission (safer):

  1. Add yourself to the wheel group.

    1.1 macOS:

     sudo dseditgroup -o edit -a $(whoami) -t user wheel
    

    1.2 GNU/Linux:

     sudo usermod -a -G wheel $(whoami)
    
  2. Allow wheel members writing permission on that folder:

     sudo chmod -R g+w /usr/local/n/
    

B. Change ownership directly to your user (quicker):

You would need to change where n stores node versions ownership:

sudo mkdir -p /usr/local/n && sudo chown -R $(whoami) /usr/local/n/

C. Change the folder where n saves node binaries, etc.

The n command downloads and installs to /usr/local by default, but you may override this location by defining N_PREFIX.

Source: https://github.com/tj/n#optional-environment-variables

  1. Create a folder, ie: $HOME/.n

     mkdir $HOME/.n
    
  2. Define the environment variable N_PREFIX adding to your shell initialisation file this line:

    2.1. bash (~/.bashrc) or zsh (~/.zshrc):

     export N_PREFIX=$HOME/.n
    

    2.2. fish (~/.config/fish/config.fish):

     set -x N_PREFIX $HOME/.n
    
  3. Add the new binary path to your environment's $PATH:

    3.1. bash/zsh:

     export PATH=$N_PREFIX/bin:$PATH
    

    3.2. fish:

     set -x PATH $N_PREFIX/bin:$PATH
    
Ignacio Lago
  • 2,432
  • 1
  • 27
  • 33
15

you just need to define the N_PREFIX.

  • First create the folder where the n data will be saved

    mkdir $HOME/.n

  • then export the N_PREFIX in your .bashrc file

    export N_PREFIX=$HOME/.n

if you use fishshell, add this to your .config.fish file: set -x N_PREFIX $HOME/.n

jsusmm
  • 151
  • 1
  • 2
5

A preferred solution should be to override the default location to a directory that doesn't require admin privileges, by defining N_PREFIX.

For updating the directory to your user directory, use the following:

export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH

Doc ref: https://github.com/tj/n#optional-environment-variables

Remy
  • 822
  • 12
  • 18
0

Nodejs the proper way without using sudo. Update existing proper installation:

npm i -g n
N_PREFIX=$HOME/.local n stable

Reference: https://guillermo.at/update-node-proper-way

siva hari
  • 93
  • 1
  • 5