Sudo: bench: command not found

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$

How do I get bench command work with sudo?

1 Like

Hi @ServerOK ,

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.

Hope this will help you out.
Thank you.

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$

Hi,

See if: sudo pip3 install frappe-bench helps.

1 Like

Hi, I think This is Useful

  1. bench init frappe-bench --verbose --frappe-branch version-13
  2. cd frappe-bench
  3. bench get-app --branch version-13 erpnext

if you want to create a site Lets continue:

  1. bench new-site site.itanand.com
  2. bench --site site.itanand.com
    install-app erpnext​

I am also experiencing this issue now. Anybody got any ideas??

Like @smino said,

sudo pip3 install frappe-bench

You may want to first activate your bench env then run the command above

source ~/<your-bench-dir>/env/bin/activate

Also good practice to setup production (if you’re in production mode) after this to ensure everything is in order

sudo bench setup production <your-frappe-user>

This answer from Chatgpt worked for me.

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 Administrator without sudo, 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.

:white_check_mark: 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