You should never use sudo (while coding)!
Tags: sudo, admin, shame
Published: 2020-01-14 13:49:02

Yeah, I know, _bold move_.

As a coder, I read a lot of _great_ articles on DEV, Medium, _any-other-place on the internet_. Those articles are filled with commands to type to "solve problem" (cargo cult, yeay!).

As a bootcamp coach, I see a lot of my learners typing those commands _"because you know, it can't be worse"_ (cargo cult, yeay!).

![Cargo cult, yeah!](https://media.giphy.com/media/SWdRKrNVg8YVz1w3EF/giphy.gif)

And most of those commands begin with `sudo`. Or, worse, I often hear this: "it didn't work without `sudo`, so... let's try to add it".

![that damned sudo](https://i.imgflip.com/3m2qr4.jpg)

It's **never** a good idea. Like, never, never.

I will focus on two common missuses of sudo when coding that you should avoid (and are easy to fix).

## Usecase one: npm (`npm install -g any-global-package`)

Most of those packages don't need the admin rights. But, by default, your system try to write the global npm package into a folder that requires admin right to be written. So... `sudo` it is. NO!

![NO!](https://media.giphy.com/media/d10dMmzqCYqQ0/giphy-downsized.gif)

You're lucky, you have two solutions for that. 

The first one is to use [`npx`](https://www.npmjs.com/package/npx), which will download and add to cache the latest version of the tool you need and then execute it. No need for admin rights. **BONUS MOVE**, `npx` will firstly look inside the current project and use the local version of the tool if it's installed. Damn, I love npx (thanks, [Kat Marchán](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b)).

The second one, but you _should_ use npx, is to indicate to npm to store the global package in your home folder. Just [follow the steps in this article](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally#manually-change-npms-default-directory), and you will never need to type `sudo` before an `npm install -g` command.

## Usecase two: docker (`docker run --way --too --many --options wonderful-image`)

I know, docker is all about _isolating_ things. And it's great. And since it's so great, why should we give it admin rights? You don't need to.

![You don't need this](https://media.giphy.com/media/4TcVFG7EYvp3Yvbu9j/giphy.gif)

When you install docker, the best way to try if your install is right, is to use this command:

	docker run hello-world

And then it didn't work, and you type `sudo docker run hello-world`, and you're happy. And somewhere, someone kills a baby koala. And you should be ashamed.  
Because you know what? It's in the _freakin'_ documentation! You know, the stuff we didn't read?! (Yeah, me too).

![We all should be ashamed](https://media.giphy.com/media/vX9WcCiWwUF7G/giphy.gif)

In the install procedure of docker, they ask you to type this command:

    sudo usermod -a -G docker $USER

This adds your user in the docker group, allowing you to use the docker command without admin rights. Which is, yeah... right what we wanted.

* * *

It's two simple examples. And I know, I shouldn't be ~~_so melodramatic_~~ _such a diva_. But, hey, I do what I want, and you shouldn't use sudo on daily basis.

Except if you are really sure of what you're doing. 

It's probably the only thing you need to remember from this article:

> If you don't know why you need `sudo` to run a command... don't do it. Or do it, break stuffs, learn things. YOLO!

![YOLOOOO!](https://media.giphy.com/media/SWjCswum5dc0E/giphy.gif)