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
pip --user
will do is install in/root/.local
instead of/usr/lib
. You really need the entirepip
process to be run as the target user, and tools likesudo -u
orsu
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:25pip --user
would work becauseecho $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: Sopip --user
does install it to my home dir, but owned by root... – Michael Aug 07 '20 at 11:38