Setting environment variables on OS X

后端 未结 30 3159
你的背包
你的背包 2020-11-21 05:15

What is the proper way to modify environment variables like PATH in OS X?

I\'ve looked on Google a little bit and found three different files to edit:

30条回答
  •  温柔的废话
    2020-11-21 05:41

    Update (2017-08-04)

    As of (at least) macOS 10.12.6 (Sierra) this method seems to have stopped working for Apache httpd (for both the system and the user option of launchctl config). Other programs do not seem to be affected. It is conceivable that this is a bug in httpd.

    Original answer

    This concerns OS X 10.10+ (10.11+ specifically due to rootless mode where /usr/bin is no longer writeable).

    I've read in multiple places that using launchctl setenv PATH to set the PATH variable does not work due to a bug in OS X (which seems true from personal experience). I found that there's another way the PATH can be set for applications not launched from the shell:

    sudo launchctl config user path 
    

    This option is documented in the launchctl man page:

    config system | user parameter value

    Sets persistent configuration information for launchd(8) domains. Only the system domain and user domains may be configured. The location of the persistent storage is an implementation detail, and changes to that storage should only be made through this subcommand. A reboot is required for changes made through this subcommand to take effect.

    [...]

    path

    Sets the PATH environment variable for all services within the target domain to the string value. The string value should conform to the format outlined for the PATH environment variable in environ(7). Note that if a service specifies its own PATH, the service-specific environment variable will take precedence.

    NOTE: This facility cannot be used to set general environment variables for all services within the domain. It is intentionally scoped to the PATH environment vari- able and nothing else for security reasons.

    I have confirmed this to work with a GUI application started from Finder (which uses getenv to get PATH). Note that you only have to do this once and the change will be persistent through reboots.

提交回复
热议问题