Skip to content

Properly Setting Environment Variables on macOS Using LaunchAgents

Introduction

Setting environment variables correctly on a macOS can be crucial for developers and power users alike. Whether you're configuring a development environment or customizing your system's behavior, understanding how to set environment variables properly is essential. In this blog post, we'll explore a reliable method for setting environment variables on macOS using LaunchAgents.

The Problem with Traditional Methods

Traditionally, many users set environment variables by adding export statements to their shell configuration files (like .bash_profile or .zshrc). While this works for terminal sessions, it falls short when you need these variables to be available to GUI applications or system-wide services.

Another common approach is using the launchctl setenv command directly. However, this method only sets the variable for the current session and doesn't persist across reboots or logins.

The Solution: Using LaunchAgents

LaunchAgents provide a robust way to set environment variables that persist across system restarts and are available to all applications, including GUI apps. Let's walk through the process of setting up a LaunchAgent to set an environment variable.

Step-by-Step Guide

In this example, we'll set the OLLAMA_ORIGINS environment variable, but you can adapt this method for any variable you need.

1. Create the LaunchAgent Configuration File

a. Open Terminal.

b. Create the necessary directory (if it doesn't exist):

mkdir -p ~/Library/LaunchAgents

c. Create and open a new file named environment.plist in this directory:

nano ~/Library/LaunchAgents/environment.plist

d. Copy and paste the following XML content into the file:

xml
<?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.OLLAMA_ORIGINS</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/launchctl</string>
    <string>setenv</string>
    <string>OLLAMA_ORIGINS</string>
    <string>*</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

e. Save the file and exit the editor. In nano, press Ctrl+X, then Y, and finally Enter.

2. Load the LaunchAgent

Run this command to load your new LaunchAgent:

launchctl load ~/Library/LaunchAgents/environment.plist

3. Verify the Setting

Check if the environment variable is set correctly:

launchctl getenv OLLAMA_ORIGINS

If set correctly, this should display *.

4. Restart Affected Services

If you're setting this variable for a specific service or application (like Ollama in this case), make sure to restart that service for the changes to take effect.

Why This Method Works

This approach leverages macOS's built-in LaunchAgent system to ensure that:

  1. The environment variable is set early in the boot process.
  2. It persists across system restarts and user logins.
  3. The variable is available to all applications, including GUI apps.

Conclusion

Setting environment variables using LaunchAgents is a powerful and reliable method for macOS users. It ensures that your variables are set consistently and are available system-wide. While we used OLLAMA_ORIGINS as an example, you can adapt this method for any environment variable you need to set.

Remember, proper environment variable management can significantly improve your development workflow and system configuration. Happy coding!


Do you have any experiences with setting environment variables on Mac? Share your thoughts and questions in the comments below!