11

I followed many methods on Ask Different as to how to change the $PATH environmental variable for GUI apps. Some of the methods may work for pre-Catalina macOS, some may work for Catalina, but none of them work for me on macOS Big Sur.

So here's the story - I installed go to /usr/local/bin/go with Homebrew, and VS Code cannot find it -

enter image description here

Methods I've tried to solve this issue:

  • sudo launchctl config user path "/usr/local/bin:$PATH", and reboot.
  • Edit /etc/paths so that it includes /usr/local/bin, and reboot.
  • Other launchctl tricks.

So what's the recommended way to set env vars for GUI apps on macOS Big Sur? Any help is appreciated.

3
  • 1
    Can you set the PATH (or at least the location of go) within VS Code directly?
    – nohillside
    Dec 7, 2020 at 13:10
  • 1
    @nohillside Yes, I think I can set the path of the go binary in the go extension settings, but the same problem exists for R and other extensions in VS Code - there must be a better way to do it.
    – Teddy C
    Dec 7, 2020 at 13:18
  • I'm having a similar issue trying to use SwiftBar. Running a python plugin, environment variables defined in bash/zsh/fish aren't available, and launchctl setenv KEY value doesn't work on Big Sur.
    – jcardinal
    Feb 16, 2021 at 19:59

1 Answer 1

7

The following worked for me to get an environment variable to be accessible from a GUI app (SwiftBar).

I created a plist file (eg; com.example.set-env-vars.plist) in ~/Library/LaunchAgents/ with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>setenv.MY_VARS</string>
  <key>ProgramArguments</key>
  <array>
    <string>sh</string>
    <string>-c</string>
    <string>launchctl setenv MY_VAR my_value</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
</dict>
</plist>

Then I rebooted for it to take effect. I suspect logging out and logging back in may be adequate, since stuff in ~/Library/LaunchAgents/ should be applied at login.

If anyone can improve this answer, I'd love to know why this works but running launchctl setenv MY_VAR my_value from a terminal does not.

Swap out MY_VAR and my_value as needed. Note, I'm not sure whether this would allow you to do something like launchctl setenv PATH "/usr/local/bin:$PATH", as I'm not sure whether $PATH would exist or expand properly.

1
  • For what it's worth, logging out wasn't enough for me, the variable only appeared after I actually rebooted.
    – anon
    Aug 2, 2021 at 8:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .