1

I'm trying to automate the deployment of my Python-Flask app on Ubuntu 18.04 using Bash by going through the motion of preparing all the necessary files/directories and cloning the source code from Github followed by creating the virtual environment, installing the pre-requisite modules and etc.

Now because I have to execute my Bash script using sudo, this means that the entire script will be executed as root except where I specify otherwise using sudo -u myuser and when it comes to activating my virtual environment, I get the following output: sudo: source: command not found and my subsequent pip installs are all installed outside of the virtual environment. Excerpts of my code below:

#!/bin/bash
...
sudo -u "$user" python3 -m venv .env
sudo -u $SUDO_USER source /srv/www/www.mydomain.com/.env/bin/activate
sudo -u "$user" pip install wheel
sudo -u "$user" pip install uwsgi
sudo -u "$user" pip install -r requirements.txt
...

Now for the life of me, I can't figure out how to activate the virtual environment in the context of the virtual environment if this makes any sense.

I've scoured the web and most of the questions/answers I found revolves around how to activate the virtual environment in a Bash script but not how to activate the virtual environment as a separate user within a Bash script that was executed as sudo.

2
  • 1
    Do you know what source does? Commented Jul 18, 2019 at 8:01
  • Anyway even if source would work the virtualenv deactivates when sudo finishes. The entire script must be run under one sudo session. Commented Jul 18, 2019 at 13:56

1 Answer 1

2

That's because source is not an executable file, but a built-in bash command. It won't work with sudo, since the latter accepts a program name (i.e. executable file) as argument.

P.S. It's not clear why you have to execute the whole script as root. If you need to execute only a number of commands as root (e.g. for starting/stopping a service) and run a remaining majority as a regular user, you can use sudo only for these commands. E.g. the following script

#!/bin/bash

# The `whoami` command outputs the current username. Unlike `source`, this is
# a full-fledged executable file, not a built-in command
whoami
sudo whoami
sudo -u postgres whoami

on my machine outputs

trolley813
root
postgres

P.P.S. You probably don't need to activate an environment as root.

Sign up to request clarification or add additional context in comments.

3 Comments

The reason I'm running the script as root is because some of the commands I'm running in the script requires sudo like stopping and starting services unless I'm getting confused about sudo. I failed to mentioned that I've also tried not using source and here's the line that I tried: Python sudo -u "$user" . /srv/www/www.domain.com/.env/bin/activate As I understand, the . is just an alias for source.
@JeremyTan If you use sudo only for the desired commands (running the entire script as regular user), they would still be executed as root (or any user you specify as argument to sudo).
@JeremyTan I've updated the answer, adding an example to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.