DEV Community

Leny for BeCode

Posted on

You should never use sudo (while coding)!

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!

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

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!


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

The first one is to use 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).

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, 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

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

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!


Top comments (0)