Some programs need the $PATH environment variable, because they launch command-line programs. That is obviously true of Terminal, but other programs may need it, too. One example is Emacs.
And if you have command line programs in non-standard places, you need to change the PATH before you launch those programs. In my case, I use MacPorts, which means I have command line programs in /opt/local/bin and /opt/local/sbin. But how do you set the PATH in such a way that other programs see it?
The Terminal programs runs a shell, such as Bash or Zsh, and you can modify the PATH variable in the file ~/.bashrc (for Bash), ~/.zshrc (for Zsh) or a similar file. That sets the PATH for the shell itself and for all programs launched from the shell. But what about programs that are launched directly from the Finder or the Dock?
In previous versions of MacOS (before version 15, Sequoia), you could type the following in the Terminal, or, better, put it in ~/.bashrc or ~/.zshrc:
launchctl setenv PATH "$PATH"
and that would set the PATH variable in the environment of all programs to the same value as used by the shell (at least for programs launched afterwards). But in MacOS 15, that no longer works. It still works for other environment variables, but not for PATH.
I tried various other ways to set the PATH. Some programs, such as Emacs, have their own configuration that you can change. You can also try to wrap each program in a shell script (e.g., using Automator) that first loads .bashrc or just sets the PATH. But a generic solution that works for all programs would be better.
I ended up setting the default path for all users with
sudo launchctl config user path /opt/local/bin:/opt/local/sbin:/usr/X11/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
(I'm also using XQuartz, which has commands in /usr/X11/bin. If you're not using that, you can omit that part, together with the colon (:) after it.)
You need to be root to use this command, hence the ‘sudo’.
This only takes effect at the next reboot, but after that it stays in effect.
Disadvantage, of course, is that it not easy to change. If you change the path, maybe temporarily, you need to become root again, issue a new ‘launchctl’ command and reboot.
Bert Bos