Skip to main content
Solved

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

  • December 13, 2024
  • 8 replies
  • 37 views

Forum|alt.badge.img+3
  • New Contributor
  • 5 replies

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? 

Best answer by sdagley

@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 original
Did this topic help you find an answer to your question?

8 replies

sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3546 replies
  • Answer
  • December 13, 2024

@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.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 5 replies
  • December 13, 2024
sdagley wrote:

@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.


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
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3546 replies
  • December 14, 2024
blee wrote:

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


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
Forum|alt.badge.img+13
  • Honored Contributor
  • 223 replies
  • December 16, 2024

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 5 replies
  • December 16, 2024

@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?


Forum|alt.badge.img+19
  • Honored Contributor
  • 582 replies
  • December 16, 2024

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. 

 


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 5 replies
  • December 17, 2024
Tribruin wrote:

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. 

 


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!


Forum|alt.badge.img+19
  • Honored Contributor
  • 582 replies
  • December 17, 2024
blee wrote:

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!


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. 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings