[GH-ISSUE #11264] macOS global environment variable #69484

Open
opened 2026-05-04 18:14:34 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @chllei on GitHub (Jul 2, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/11264

On macOS, the method suggested in the current documentation to modify environment variables using launchctl is temporary and will be lost upon system restart. It is hoped that the Ollama app could provide an option to change default environment variables.

Originally created by @chllei on GitHub (Jul 2, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/11264 On macOS, the method suggested in the current documentation to modify environment variables using `launchctl` is temporary and will be lost upon system restart. It is hoped that the Ollama app could provide an option to change default environment variables.
GiteaMirror added the feature request label 2026-05-04 18:14:34 -05:00
Author
Owner

@jmkraus commented on GitHub (Jul 2, 2025):

Environment variables are not persistent by design.

You could either set your environment variables in a zsh .zprofile:

Edit or create a ~/.zprofile and add something like this at the end:
EXPORT <variable name>=<value>

Or you can wait for something like this (if it will ever be implemented): https://github.com/ollama/ollama/issues/11076

<!-- gh-comment-id:3028517255 --> @jmkraus commented on GitHub (Jul 2, 2025): Environment variables are not persistent by design. You could either set your environment variables in a zsh .zprofile: Edit or create a ~/.zprofile and add something like this at the end: EXPORT \<variable name\>=\<value\> Or you can wait for something like this (if it will ever be implemented): https://github.com/ollama/ollama/issues/11076
Author
Owner

@chllei commented on GitHub (Jul 3, 2025):

Environment variables are not persistent by design.

You could either set your environment variables in a zsh .zprofile:

Edit or create a ~/.zprofile and add something like this at the end: EXPORT =

Or you can wait for something like this (if it will ever be implemented): #11076

In previous versions of macOS, I ensured that Ollama could use updated environment variables during startup by following the method below.


To fully control the startup sequence using LaunchAgent, it is necessary to address a common issue with standard LaunchAgent configurations: a race condition. This occurs because there is no guarantee that environment variable setup will precede the application's startup, leading to potential failures or inconsistent behavior. To resolve this, the application's native startup permission must be revoked, and control must be transferred to a custom script for unified management.

Operational Procedure:

Step 1: Disable Ollama's Native Startup

  • Click on the Ollama icon in the top menu bar.
  • Uncheck "Start on login" (or "Login to start").
  • Purpose: Eliminate the race condition by transferring full control of the startup process to our script.

Step 2: Create and Edit the LaunchAgent Configuration File

  • Execute the following command in the terminal to create and open a .plist file:
    nano ~/Library/LaunchAgents/ollama_env.plist
    (The filename can be customized, but it is recommended to use reverse domain notation. The file must be placed in the correct location.)

Step 3: Write the Configuration Content
Paste the following XML content completely into the nano editor. This script will first set environment variables and then start the application, ensuring a strict execution sequence.

<?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>ollama_env.launcher</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/sh</string>
        <string>-c</string>
        <string>
        launchctl setenv OLLAMA_HOST "0.0.0.0";
        launchctl setenv OLLAMA_KEEP_ALIVE "-1";
        launchctl setenv OLLAMA_MAX_LOADED_MODELS "2";
    	launchctl setenv OLLAMA_ORIGINS "*";
	launchctl setenv OLLAMA_FLASH_ATTENTION "1";
    	launchctl setenv OLLAMA_KV_CACHE_TYPE "q8_0";
	launchctl setenv OLLAMA_NUM_CTX "16384";

	open -a Ollama
        </string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Step 4: Load Configuration and Verify

  • Execute launchctl load ~/Library/LaunchAgents/ollama_env.plist to load the configuration immediately.
  • Final verification: Restart the Mac. If Ollama starts successfully and its behavior aligns with the environment variable settings, the configuration has been successfully applied.

The methods work well with older versions of Ollama. However, the self-launch mechanism for the native Ollama app on macOS has changed. After a computer restart, environment variables may not take effect, requiring manual restart of the self-launching Ollama to ensure the environment variables are applied.

I hope the Ollama official can provide support for permanently setting custom environment variables, similar to @jmkraus's reference #11076.

<!-- gh-comment-id:3030509531 --> @chllei commented on GitHub (Jul 3, 2025): > Environment variables are not persistent by design. > > You could either set your environment variables in a zsh .zprofile: > > Edit or create a ~/.zprofile and add something like this at the end: EXPORT <variable name>=<value> > > Or you can wait for something like this (if it will ever be implemented): [#11076](https://github.com/ollama/ollama/issues/11076) In previous versions of macOS, I ensured that Ollama could use updated environment variables during startup by following the method below. --- To fully control the startup sequence using LaunchAgent, it is necessary to address a common issue with standard LaunchAgent configurations: a race condition. This occurs because there is no guarantee that environment variable setup will precede the application's startup, leading to potential failures or inconsistent behavior. To resolve this, the application's native startup permission must be revoked, and control must be transferred to a custom script for unified management. Operational Procedure: Step 1: Disable Ollama's Native Startup - Click on the Ollama icon in the top menu bar. - Uncheck "Start on login" (or "Login to start"). - Purpose: Eliminate the race condition by transferring full control of the startup process to our script. Step 2: Create and Edit the LaunchAgent Configuration File - Execute the following command in the terminal to create and open a .plist file: `nano ~/Library/LaunchAgents/ollama_env.plist` (The filename can be customized, but it is recommended to use reverse domain notation. The file must be placed in the correct location.) Step 3: Write the Configuration Content Paste the following XML content completely into the nano editor. This script will first set environment variables and then start the application, ensuring a strict execution sequence. ``` <?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>ollama_env.launcher</string> <key>ProgramArguments</key> <array> <string>/bin/sh</string> <string>-c</string> <string> launchctl setenv OLLAMA_HOST "0.0.0.0"; launchctl setenv OLLAMA_KEEP_ALIVE "-1"; launchctl setenv OLLAMA_MAX_LOADED_MODELS "2"; launchctl setenv OLLAMA_ORIGINS "*"; launchctl setenv OLLAMA_FLASH_ATTENTION "1"; launchctl setenv OLLAMA_KV_CACHE_TYPE "q8_0"; launchctl setenv OLLAMA_NUM_CTX "16384"; open -a Ollama </string> </array> <key>RunAtLoad</key> <true/> </dict> </plist> ``` Step 4: Load Configuration and Verify - Execute `launchctl load ~/Library/LaunchAgents/ollama_env.plist` to load the configuration immediately. - Final verification: Restart the Mac. If Ollama starts successfully and its behavior aligns with the environment variable settings, the configuration has been successfully applied. --- The methods work well with older versions of Ollama. However, the self-launch mechanism for the native Ollama app on macOS has changed. After a computer restart, environment variables may not take effect, requiring manual restart of the self-launching Ollama to ensure the environment variables are applied. I hope the Ollama official can provide support for permanently setting custom environment variables, similar to @jmkraus's reference #11076.
Author
Owner

@pdevine commented on GitHub (Jul 3, 2025):

@chllei which environment variable are you trying to set? Is it covered in the new settings menu from the tray app? We'll be adding more settings in there over time.

<!-- gh-comment-id:3032857786 --> @pdevine commented on GitHub (Jul 3, 2025): @chllei which environment variable are you trying to set? Is it covered in the new settings menu from the tray app? We'll be adding more settings in there over time.
Author
Owner

@chllei commented on GitHub (Jul 4, 2025):

@chllei which environment variable are you trying to set? Is it covered in the new settings menu from the tray app? We'll be adding more settings in there over time.

@pdevine At this time, I need to set the following environment variables (the configuration for Windows, macOS, and Linux versions is consistent):

OLLAMA_HOST = 0.0.0.0
OLLAMA_KEEP_ALIVE = -1
OLLAMA_MAX_LOADED_MODELS = 2
OLLAMA_ORIGINS = *
OLLAMA_FLASH_ATTENTION = 1
OLLAMA_KV_CACHE_TYPE = q8_0
OLLAMA_NUM_CTX = 16384

The current settings menu in the tray application does not provide the aforementioned environment variables. If these variables could be directly set within the Ollama graphical interface, that would be highly beneficial.

<!-- gh-comment-id:3034263597 --> @chllei commented on GitHub (Jul 4, 2025): > [@chllei](https://github.com/chllei) which environment variable are you trying to set? Is it covered in the new settings menu from the tray app? We'll be adding more settings in there over time. @pdevine At this time, I need to set the following environment variables (the configuration for Windows, macOS, and Linux versions is consistent): ``` OLLAMA_HOST = 0.0.0.0 OLLAMA_KEEP_ALIVE = -1 OLLAMA_MAX_LOADED_MODELS = 2 OLLAMA_ORIGINS = * OLLAMA_FLASH_ATTENTION = 1 OLLAMA_KV_CACHE_TYPE = q8_0 OLLAMA_NUM_CTX = 16384 ``` The current settings menu in the tray application does not provide the aforementioned environment variables. If these variables could be directly set within the Ollama graphical interface, that would be highly beneficial.
Author
Owner

@aaronpliu commented on GitHub (Jul 9, 2025):

how to ensure ollama use these variables setup after configure it with launchctl on MacOS

<!-- gh-comment-id:3050855511 --> @aaronpliu commented on GitHub (Jul 9, 2025): how to ensure ollama use these variables setup after configure it with launchctl on MacOS
Author
Owner

@chllei commented on GitHub (Jul 9, 2025):

how to ensure ollama use these variables setup after configure it with launchctl on MacOS

@aaronpliu A simple way to verify is to restart ollama and run a downloaded model, then check the output of the ollama ps command. If flash attention is enabled and kv_cache is set, the model's GPU memory usage will show a significant reduction.

<!-- gh-comment-id:3050870156 --> @chllei commented on GitHub (Jul 9, 2025): > how to ensure ollama use these variables setup after configure it with launchctl on MacOS @aaronpliu A simple way to verify is to restart ollama and run a downloaded model, then check the output of the `ollama ps` command. If flash attention is enabled and kv_cache is set, the model's GPU memory usage will show a significant reduction.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#69484