[GH-ISSUE #2955] Is there guidance to run Ollama as a background "Daemon" on MacOS pre-login? #63854

Closed
opened 2026-05-03 15:13:37 -05:00 by GiteaMirror · 10 comments
Owner

Originally created by @dukekautington3rd on GitHub (Mar 6, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/2955

I would really like Ollama to run as a service on my Mac or at least set the appropriate listening variable before it starts.
Today I have to launchctl setenv OLLAMA_HOST 0.0.0.0:8080 and restart Ollama any time there is a reboot.

And I must be logged in in-order for Ollama to be serving up the LLM.

I've tried automatically setting the variables many ways:
[
/etc/launchd.conf
/etc/rc.common
~/.zprofile # Only works after terminal is opened
plist file for /Library/LaunchAgents # Only works @ login
plist file in /Library/LaunchDaemons # Can't get to work
above plist files calling [launchctl, automator app, script file]
]

My first milestone was to get the env OLLAMA_HOST 0.0.0.0:8080 set before Ollama automatically launches
then... get Ollama to launch and continuously run in the background pre-login.

I've been spinning my wheels on this for a while now. It just seems like something that should be so easy.
Before someone suggests it, I know I could easily run in linux. I have an extensive k8s deployment that runs most of my things, but I want to leverage the Macos NPU(Maybe I have a false sense of value here) for performance.

Originally created by @dukekautington3rd on GitHub (Mar 6, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/2955 I would really like Ollama to run as a service on my Mac or at least set the appropriate listening variable before it starts. Today I have to `launchctl setenv OLLAMA_HOST 0.0.0.0:8080` and restart Ollama any time there is a reboot. And I must be logged in in-order for Ollama to be serving up the LLM. I've tried automatically setting the variables many ways: [ /etc/launchd.conf /etc/rc.common ~/.zprofile # Only works after terminal is opened plist file for /Library/LaunchAgents # Only works @ login plist file in /Library/LaunchDaemons # Can't get to work above plist files calling [launchctl, automator app, script file] ] My first milestone was to get the env `OLLAMA_HOST 0.0.0.0:8080` set before Ollama automatically launches then... get Ollama to launch and continuously run in the background pre-login. I've been spinning my wheels on this for a while now. It just seems like something that should be so easy. Before someone suggests it, I know I could easily run in linux. I have an extensive k8s deployment that runs most of my things, but I want to leverage the Macos NPU(Maybe I have a false sense of value here) for performance.
Author
Owner

@easp commented on GitHub (Mar 6, 2024):

FYI, Ollama doesn't use the Apple Neural Engine/NPU, just the GPU. Ultimately it's memory bandwidth bound, so the only thing the ANE would likely do is reduce power consumption. It's also possible that the ANE has less memory bandwidth than the GPU and so would perform worse.

As for your questions, I don't know my way around launchd, at all. I suspect that you'll have the best results if you try launching the command line binary, rather than Ollama.app -- easiest thing might be a shell script that sets the environment as needed and then runs ollama serve. Have you referred to https://launchd.info? There is also a launchd overview here which references two commercial utilities that make launchd easier to configure.

If you figure out something that works I imagine that there will be other people interested in your solution.

<!-- gh-comment-id:1981443325 --> @easp commented on GitHub (Mar 6, 2024): FYI, Ollama doesn't use the Apple Neural Engine/NPU, just the GPU. Ultimately it's memory bandwidth bound, so the only thing the ANE would likely do is reduce power consumption. It's also possible that the ANE has less memory bandwidth than the GPU and so would perform worse. As for your questions, I don't know my way around launchd, at all. I suspect that you'll have the best results if you try launching the command line binary, rather than Ollama.app -- easiest thing might be a shell script that sets the environment as needed and then runs `ollama serve`. Have you referred to https://launchd.info? There is also a launchd overview [here](https://eclecticlight.co/2021/09/13/running-software-automatically-using-launchd/) which references two commercial utilities that make launchd easier to configure. If you figure out something that works I imagine that there will be other people interested in your solution.
Author
Owner

@dukekautington3rd commented on GitHub (Mar 6, 2024):

@easp That was super helpful. You confirmed what I suspected, which was that Ollama doesn't use ANE, so I did have a false sense of value. Although the GPU isn't bad either.

Thanks for the additional info around launchd. I will share in this thread if I make progress!

Thanks!

<!-- gh-comment-id:1982008584 --> @dukekautington3rd commented on GitHub (Mar 6, 2024): @easp That was super helpful. You confirmed what I suspected, which was that Ollama doesn't use ANE, so I did have a false sense of value. Although the GPU isn't bad either. Thanks for the additional info around launchd. I will share in this thread if I make progress! Thanks!
Author
Owner

@dukekautington3rd commented on GitHub (Mar 6, 2024):

whoops, accidental reopen

<!-- gh-comment-id:1982009033 --> @dukekautington3rd commented on GitHub (Mar 6, 2024): whoops, accidental reopen
Author
Owner

@brenc commented on GitHub (Mar 16, 2024):

@dukekautington3rd were you ever able to figure this out? I figured out how to get ollama to reliably start listening on all interfaces but only on login.

<!-- gh-comment-id:2002082226 --> @brenc commented on GitHub (Mar 16, 2024): @dukekautington3rd were you ever able to figure this out? I figured out how to get ollama to reliably start listening on all interfaces but only on login.
Author
Owner

@dukekautington3rd commented on GitHub (Mar 16, 2024):

I have. I just need a little time to write it up fully.

The summary is:

I can't get anything to execute prelogin, I think this has to do with disk encryption/(decryption upon login). I have created a launch daemon that points to a script which sets the variable and subsequently serves up Ollama.

<!-- gh-comment-id:2002083229 --> @dukekautington3rd commented on GitHub (Mar 16, 2024): I have. I just need a little time to write it up fully. The summary is: I can't get anything to execute prelogin, I think this has to do with disk encryption/(decryption upon login). I have created a launch daemon that points to a script which sets the variable and subsequently serves up Ollama.
Author
Owner

@mbentley commented on GitHub (Apr 18, 2024):

Just in case anyone would like an example of a LaunchDaemon that does not require the use of an external script to set variables:

<?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</string>
    <key>StandardOutPath</key>
    <string>/Users/mbentley/.ollama/launchd.stdout.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/mbentley/.ollama/launchd.stderr.log</string>
    <key>EnvironmentVariables</key>
    <dict>
        <key>OLLAMA_HOST</key>
        <string>0.0.0.0:11434</string>
    </dict>
    <key>ProgramArguments</key>
    <array>
      <string>/opt/homebrew/bin/ollama</string>
      <string>serve</string>
    </array>
    <key>UserName</key>
    <string>mbentley</string>
    <key>GroupName</key>
    <string>staff</string>
    <key>ExitTimeOut</key>
    <integer>30</integer>
    <key>Disabled</key>
    <false />
    <key>KeepAlive</key>
    <true />
  </dict>
</plist>

In my case, my username is mbentley so update accordingly along with the path to ollama. I created mine as ollama.plist and copied it to the correct location using sudo cp ollama.plist /Library/LaunchDaemons/ and then I can start/stop using sudo launchctl load /Library/LaunchDaemons/ollama.plist and sudo launchctl unload /Library/LaunchDaemons/ollama.plist and it should start on boot.

<!-- gh-comment-id:2064862349 --> @mbentley commented on GitHub (Apr 18, 2024): Just in case anyone would like an example of a LaunchDaemon that does not require the use of an external script to set variables: ```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>ollama</string> <key>StandardOutPath</key> <string>/Users/mbentley/.ollama/launchd.stdout.log</string> <key>StandardErrorPath</key> <string>/Users/mbentley/.ollama/launchd.stderr.log</string> <key>EnvironmentVariables</key> <dict> <key>OLLAMA_HOST</key> <string>0.0.0.0:11434</string> </dict> <key>ProgramArguments</key> <array> <string>/opt/homebrew/bin/ollama</string> <string>serve</string> </array> <key>UserName</key> <string>mbentley</string> <key>GroupName</key> <string>staff</string> <key>ExitTimeOut</key> <integer>30</integer> <key>Disabled</key> <false /> <key>KeepAlive</key> <true /> </dict> </plist> ``` In my case, my username is `mbentley` so update accordingly along with the path to `ollama`. I created mine as `ollama.plist` and copied it to the correct location using `sudo cp ollama.plist /Library/LaunchDaemons/` and then I can start/stop using `sudo launchctl load /Library/LaunchDaemons/ollama.plist` and `sudo launchctl unload /Library/LaunchDaemons/ollama.plist` and it should start on boot.
Author
Owner

@liliang-cn commented on GitHub (Jun 23, 2024):

it's a bad design for this HOST thing, why not just read from env or somewhere, or why do we have this restriction at all?

<!-- gh-comment-id:2184866149 --> @liliang-cn commented on GitHub (Jun 23, 2024): it's a bad design for this HOST thing, why not just read from env or somewhere, or why do we have this restriction at all?
Author
Owner

@qdrddr commented on GitHub (Jun 28, 2024):

Added feature request to add CoreML backend to be able to utilize Apple Neural Engine (ANE) alongside GPU and CPU
https://github.com/ollama/ollama/issues/3898

<!-- gh-comment-id:2197627332 --> @qdrddr commented on GitHub (Jun 28, 2024): Added feature request to add CoreML backend to be able to utilize Apple Neural Engine (ANE) alongside GPU and CPU https://github.com/ollama/ollama/issues/3898
Author
Owner

@Sylvance commented on GitHub (Nov 19, 2024):

Based on @mbentley's response you update your .zshrc with the below so that you can start/stop ollama with ollama:start and ollama:stop;

alias ollama:start='sudo launchctl load /Library/LaunchDaemons/ollama.plist'
alias ollama:stop='sudo launchctl unload /Library/LaunchDaemons/ollama.plist'
<!-- gh-comment-id:2484946208 --> @Sylvance commented on GitHub (Nov 19, 2024): Based on @mbentley's response you update your .zshrc with the below so that you can start/stop ollama with `ollama:start` and `ollama:stop`; ```zsh alias ollama:start='sudo launchctl load /Library/LaunchDaemons/ollama.plist' alias ollama:stop='sudo launchctl unload /Library/LaunchDaemons/ollama.plist' ```
Author
Owner

@aghea commented on GitHub (Dec 4, 2024):

You already use plist file.Just need delay ollama 10sec.

Use Script Editor create new create the simple AppleScript file

delay 10
tell application "Ollama" to run

In the File menu choose Export, and then export it as type “Application” and name it “LaunchOllamaDelay”. Save it to your user Applications folder.

In System Settings go to Login Items and add the LaunchOllamaDelay application to the startup items. Also remove any existing Ollama startup item.

Now when you restart and sign in, Ollama will launch after 10 seconds which should be enough time for the Launch Agent to have executed.

It's work on sequoia

<!-- gh-comment-id:2516202054 --> @aghea commented on GitHub (Dec 4, 2024): You already use plist file.Just need delay ollama 10sec. Use Script Editor create new create the simple AppleScript file _delay 10 tell application "Ollama" to run_ In the File menu choose Export, and then export it as type “Application” and name it “LaunchOllamaDelay”. Save it to your user Applications folder. In System Settings go to Login Items and add the LaunchOllamaDelay application to the startup items. Also remove any existing Ollama startup item. Now when you restart and sign in, Ollama will launch after 10 seconds which should be enough time for the Launch Agent to have executed. It's work on sequoia
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#63854