Fixing error installing @angular/cli

Tried to install latest Angular (10 in this case) the other day only to bump into this error:

$ npm install -g @angular/cli
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm ERR! code EACCES
npm ERR! syscall unlink
npm ERR! path /usr/bin/ng
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, unlink '/usr/bin/ng'
npm ERR! [OperationalError: EACCES: permission denied, unlink '/usr/bin/ng'] {
npm ERR! cause: [Error: EACCES: permission denied, unlink '/usr/bin/ng'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'unlink',
npm ERR! path: '/usr/bin/ng'
npm ERR! },
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'unlink',
npm ERR! path: '/usr/bin/ng'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

Obviously permission error. Culprit appears to be current installation of Node.js with sudo:

sudo apt install nodejs

Fiddling around with user permissions won’t do much good and might do more harm in future. Easiest way appears to be reinstalling Node.js without sudo or installing new version of Node.js also without sudo.

https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

Node Version Manager – nvm did the trick for Node.js:

https://github.com/nvm-sh/nvm

Follow install instructions for nvm:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash

Open new terminal or follow instructions from script (export NVM_DIR=...).

Install latest Node.js:

$ nvm install node
Downloading and installing node v15.0.1…

Now using node v15.0.1 (npm v7.0.3)
Creating default alias: default -> node (-> v15.0.1)

Check installation:

$ node -v
v15.0.1

Install latest Angular:

$ npm install -g @angular/cli

Check installation:

$ which ng
/home/user/.nvm/versions/node/v15.0.1/bin/ng

$ ng --version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/

Angular CLI: 10.2.0
Node: 15.0.1
OS: linux x64

Angular:
...
Ivy Workspace:

Package Version
------------------------------------------------------
@angular-devkit/architect 0.1002.0
@angular-devkit/core 10.2.0
@angular-devkit/schematics 10.2.0
@schematics/angular 10.2.0
@schematics/update 0.1002.0

That’s it. Just remember to avoid sudo while installing Node.js and npm packages.