"sudo: brew: command not found" error when trying to uninstall python3 in Homebrew

blee
New Contributor II

I currently have the script to try and uninstall python3 in Homebrew:

 

sudo brew uninstall python3

 

and keep getting the error "sudo: brew: command not found". I've also tried the command

/opt/homebrew/bin/brew uninstall python3

and run into the error "Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all build scripts full access to your system."

I'm certain that Homebrew and python3 (via Homebrew) is installed on the device I'm pushing this script to. Is there another way I should be running uninstall commands for Homebrew? 

2 ACCEPTED SOLUTIONS

sdagley
Esteemed Contributor II

@blee Have you tried running the script as the logged in user (see https://scriptingosx.com/2020/08/running-a-command-as-another-user/) If you're pushing the script via a policy that doesn't run it in the user context.

View solution in original post

Tribruin
Valued Contributor II

So it looks like you are running in to two issues at once:

1) brew does not want to run as root, which is how scripts run from Jamf. To run as the user, you need to use the trick that @sdagley  posted to run commands as user in a script. 

2) The reason you are not finding the brew command is that, even though you are running the command as the user, you are not loading the users configuration files (.zshrc, .zshenv, etc), so you are only getting the default PATH, and not any addtional paths that are added when you open terminal on the local computer. So, the command is not found, because it is not in the path of the shell session you are running. 

Try modifying your script to do the RunAs command and use the whole path to brew (/opt/homebrew/bin/brew) instead of just the brew command. That should work. 

 

View solution in original post

8 REPLIES 8

sdagley
Esteemed Contributor II

@blee Have you tried running the script as the logged in user (see https://scriptingosx.com/2020/08/running-a-command-as-another-user/) If you're pushing the script via a policy that doesn't run it in the user context.

blee
New Contributor II

Thanks for the suggestion! Unfortunately it leads to the same error of brew not being recognized. Seems like no matter what I try I either run into this error or the 'running Homebrew on root is extremely dangerous error' and Jamf refuses to continue with the script

sdagley
Esteemed Contributor II

Remove the sudo prefix from the uninstall command since you don't want to run it with elevated privileges. Also note that when running a script via a Jamf Pro policy you're already running with raised privileges so you wouldn't need a sudo prefix on any commands in the script unless it is being used to specify a different user context to run the command under.

Jason33
Contributor III

Have you run brew commands on that system before? I've only seen that error saying the brew command wasnt found if brew wasnt added to the path

blee
New Contributor II

@Jason33  @sdagley So I think I found what the root cause of the error is but not certain how to go about it. When I run 'which python3' on the device itself, I get /opt/homebrew/bin/python3, which is expected. I then wrote a script to run the same 'which python3' via Jamf, save the output as a text file on the device then email it to myself. I found that this result is '/usr/bin/python3'. I believe this is why I'm seeing the 'command not found: brew' error.

I was able to attempt changing which python3 it's using on the device itself but cannot find a way to do it via a script through Jamf. Any suggestions?

Tribruin
Valued Contributor II

So it looks like you are running in to two issues at once:

1) brew does not want to run as root, which is how scripts run from Jamf. To run as the user, you need to use the trick that @sdagley  posted to run commands as user in a script. 

2) The reason you are not finding the brew command is that, even though you are running the command as the user, you are not loading the users configuration files (.zshrc, .zshenv, etc), so you are only getting the default PATH, and not any addtional paths that are added when you open terminal on the local computer. So, the command is not found, because it is not in the path of the shell session you are running. 

Try modifying your script to do the RunAs command and use the whole path to brew (/opt/homebrew/bin/brew) instead of just the brew command. That should work. 

 

blee
New Contributor II

That was it! Combination of @sdagley 's suggestion to run as user and yours of using the entire path to brew /opt/homebrew/bin/brew. I can finally run brew's official python3 uninstaller via Jamf. Thank you everyone!

Tribruin
Valued Contributor II

One additional twist, if you have both Apple Silicon and Intel computers in your fleet. Homebrew installs its files in different directories between the two architecture. Homebrew installs in /usr/local/bin/brew on Intel. If you have a mix of computers you will want to use the arch command to check at the beginning of your script and adjust your path as appropriate.