When is sudo needed?

Needless use of administrator privileges during install of a library or program is generally undesirable. On Unix-like systems including Linux, BSD and macOS, admin privileges are known as superuser, typically invoked for a single command by sudo. Casual use of “sudo” can goof up the operating system itself, create cybersecurity risks, and mess up the desired program installation. The install might do undesirable things like put itself under /root or make itself readable only by root etc.

In general we write procedures without invoking sudo, to avoid careless sudo invocation. That is we assume the user knows when it’s appropriate to invoke sudo. sudo is commonly used with commands installing from repositories.

Help avoid issues by installing without sudo under “~/.local/” as in these examples. Many Linux distros including Ubuntu are preconfigured to use a hierarchy like ~/.local/bin, ~/.local/lib and so on.

For CMake, set the default install location in “CMakeLists.txt”. Users can set their own install location like:

cmake -DCMAKE_INSTALL_PREFIX=~

Autotools uses the --prefix= option to specify the top-level install location, like:

./configure --prefix=~/.local

When using system Python it is often preferred to use the PEP 370 --user option like:

pip install --user matplotlib

which installs Python modules under ~/.local. Pip ≥ 20 makes pip default to --user when necessary There are some edge cases such as GTK3 where using system Python libraries can be useful, such as python-gi with Beamer.

A big mess can be made when installing with sudo pip – don’t do that in general. Also, when installing Python environments (e.g. Miniconda), which is typically recommended instead of using system Python, don’t use sudo either.