0

I have an install script to set up dependencies for a project, this means installing packages using pip and apt.

My prefered method is to run the script as sudo ./install but then how do I drop sudo for pip (do I need to with --user)?

#!/bin/sh

if [ $(id -u) -ne 0 ]; then
    echo Please run as root
    exit
fi
apt install pkg_a pkg_b pkg_c

# Is this fine? How do I drop sudo here?
pip install --user pkg_d pkg_e pkg_f

The alternative is to have sudo in the script but I prefer the first option if it's possible

#!/bin/sh

sudo apt install pkg_a pkg_b pkg_c
pip install --user pkg_d pkg_e pkg_f
Michael
  • 103
  • 1
  • 4
  • This might provide some insight. [How do I drop root privileges in shell scripts?](https://unix.stackexchange.com/questions/132663/how-do-i-drop-root-privileges-in-shell-scripts) – John WH Smith Aug 07 '20 at 10:55
  • @john was hoping there'd be something native but that's workable, I can use a flag to make sure it's originally run as sudo ty – Michael Aug 07 '20 at 11:20
  • Thing is, since you're running as root, all `pip --user` will do is install in `/root/.local` instead of `/usr/lib`. You really need the entire `pip` process to be run as the target user, and tools like `sudo -u` or `su` do just that. Many, of course, will (probably rightfully) argue that the script shouldn't be run as root in the first place, but that's an entirely different debate. – John WH Smith Aug 07 '20 at 11:25
  • @JohnWHSmith I was hoping `pip --user` would work because `echo $HOME` when run from a sudo script still points to my home directory. The reason I want to run the script as sudo is so it doesn't ask me for my password during execution (or multiple times) if there are long steps. Maybe it's a bad idea but it would be nice to invoke a script such that it can call sudo without a password during execution. Edit: So `pip --user` does install it to my home dir, but owned by root... – Michael Aug 07 '20 at 11:38

0 Answers0