Setting Up Environment Variables on MacOS Yosemite Using Launch Agent

In previous releases of macOS (Mavericks, Mountain Lion, Lion, …), configuring environment variables required editing the /etc/launchd.conf file. However, starting from macOS Yosemite, this method is no longer effective. To successfully configure environment variables on Yosemite and later versions, follow these steps:

  1. Create a Launch Agent Property List (plist) File: Create a plist file named my.startup.plist in the ~/Library/LaunchAgents/ directory. This directory is specific to user-based launch agent configurations.

  2. Define the Plist Structure: The content of the my.startup.plist file should adhere to the XML-based property list format. This format is used to specify various attributes that dictate the behavior of launch agents.

  3. Configure Plist Keys and Values:

    • Use the <key> and <string> elements to define keys and string values within the plist.
    • Utilize the Label key to assign a unique identifier to the launch agent, such as my.startup.
    • Under the ProgramArguments key, provide an array of strings representing the shell command to be executed.
      • The first string, sh, indicates that the subsequent command should be executed by the shell.
      • The second string, -c, signals that a command will follow.
      • The third string contains the actual command responsible for setting the environment variable using the launchctl setenv command.
  4. Set Environment Variables: Use the launchctl setenv command to set environment variables. This command requires two arguments: the name of the variable ($VARIABLE_NAME) and its corresponding value ($VARIABLE_VALUE). The shell will execute this command when the Launch Agent is loaded.

  5. Enable Execution at Login: Set the RunAtLoad key to <true/> within the plist. This ensures that the Launch Agent is executed when the user logs in.

Please keep in mind that environment variable management methods on macOS have evolved over time. While the provided plist configuration is suitable for macOS Yosemite, newer versions may favor different approaches. Always exercise caution when modifying system configurations to prevent unintended consequences.

0%