Reputation: 8172
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
Upvotes: 57
Views: 49241
Reputation: 1
For linux Run this command: sudo mkdir -p /usr/local/n && sudo chown -R $(whoami) /usr/local/n/
then: n stable
you will get a message on your terminal that says:
Note: the node command changed location and the old location may be remembered in your current shell. old : /usr/bin/node new : /usr/local/bin/node If "node --version" shows the old version then start a new shell, or reset the location hash with: hash -r (for bash, zsh, ash, dash, and ksh) rehash (for csh and tcsh)
you can run the hash -r for these shells (bash, zsh, ash, dash, and ksh), or rehash for these (csh and tcsh).
Upvotes: 0
Reputation: 865
I also encountered this problem, in fact, the current user does not have permissions to the global directory.
Trying to use the solution in Ignacio Lago's answer didn't work, and in the end it was solved perfectly with the following method:
brew install node
npm install -g n
n doctor
It will list what the problem is, just refer to the help suggestion in the error list.
Finally, remember to uninstall the node installed with brew, and it will be solved perfectly.
brew uninstall node
Perfect!
Upvotes: 1
Reputation: 894
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
Upvotes: 9
Reputation: 131
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
Upvotes: 2
Reputation: 2572
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
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)
Allow wheel
members writing permission on that folder:
sudo chmod -R g+w /usr/local/n/
You would need to change where n
stores node
versions ownership:
sudo mkdir -p /usr/local/n && sudo chown -R $(whoami) /usr/local/n/
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
Create a folder, ie: $HOME/.n
mkdir $HOME/.n
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
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
Upvotes: 77
Reputation: 181
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
Upvotes: 18