# When to use sudo in macOS?

I saw some command line with sudo for using on macOS. I have seen it even used for NPM installs. I am under the impression that using sudo is discouraged for macOS.

When do I use sudo in macOS?

• Sudo runs the command as super user, so it's used when the command needs super user privileges. Is this what you are asking? – NoahL Apr 2 '17 at 20:42

sudo is not discouraged on MacOS it is much to be preferred to the alternative which is to su to another user especially root. It is discouraged to set up root so you can login or su to it.

Normally you can only write to files in directories you have write acces to.

If you need to write to a file that is not ina directory owned by you your will not usually have permission. e.g. /usr/local/bin can only be written to by root.

If you need to copy a file to a protected directory - as the npm install does you have to ask for more privileges. Adding sudo before a command means that the command is run as if root was running it anmd so the command can write to /usr/local/bin

sudo can be used to run as any user by using the -u parameter.

sudo originally meant superuser do (and then got generalized to "substitute user do"), so what you are asking is when do I have to use the admin account.

Well, the answer is simple, when the installation doesn't work without extended access rights (primarily because files need to get written into directories not writable by standard users), just type sudo !! to rerun the command but with the admin account.

PS: Some npm package will need sudo, some won't, so there is no clear answer to your question, but just use it when you think the admin right will be needed

Using sudo is not discouraged especially on Macs.

In general, you should not use sudo when not absolutely necessary. This goes for all platforms, not just Macs.

sudo is used to execute one or more commands with elevated privileges - perhaps even super user (root) privileges. Used sparingly as needed it is a vital tool for every systems administrator.