When I run sudo bench setup production, I get the error “sudo: bench: command not found”. Without sudo, the bench command works correctly.
erpnext@ubuntu-s-1vcpu-2gb-amd-blr1-01:~/erpnext$ bench setup production
WARN: superuser privileges required for this command
erpnext@ubuntu-s-1vcpu-2gb-amd-blr1-01:~/erpnext$ sudo bench setup production
sudo: bench: command not found
erpnext@ubuntu-s-1vcpu-2gb-amd-blr1-01:~/erpnext$ which bench
/home/erpnext/.local/bin/bench
erpnext@ubuntu-s-1vcpu-2gb-amd-blr1-01:~/erpnext$ sudo /home/erpnext/.local/bin/bench setup production
Traceback (most recent call last):
File "/home/erpnext/.local/bin/bench", line 5, in <module>
from bench.cli import cli
ModuleNotFoundError: No module named 'bench'
erpnext@ubuntu-s-1vcpu-2gb-amd-blr1-01:~/erpnext$
The bench commend only works on your frappe-bench installed directory. By default, the folder name will be frappe-bench. So, try to find that folder, get into it, and run all your bench commends.
I used the following command to create the frappe-bench
bench init --frappe-branch version-13 erpnext
Since erpnext is specified at the end of the command, it created folder “erpnext”, I am inside this folder, and bench command works properly, the only problem is when running with the sudo command.
See “bench --site all list-apps” works properly.
erpnext@ubuntu-s-1vcpu-2gb-amd-blr1-01:~/erpnext$ bench --site all list-apps
frappe
erpnext
erpnext@ubuntu-s-1vcpu-2gb-amd-blr1-01:~/erpnext$
You’re seeing this issue because the bench command is not found when you use sudo. This is a common problem related to how environment variables (like the path to bench) are handled differently by sudo.
Here’s what’s happening:
When you run bench setup production Administratorwithoutsudo, it finds the bench command in your virtual environment, but it complains that superuser privileges are needed.
When you run it with sudo, the environment changes and sudo doesn’t know where to find bench.
Solution
You need to tell sudo where to find the bench command.
Option 1: Use full path to bench
First, find the full path to the bench command:
which bench
For example, it might return:
/home/user/.local/bin/bench
Then run:
sudo /home/user/.local/bin/bench setup production Administrator