# Set up Ollama on macOS

This guide walks you through the main steps of setting up an [Ollama server](https://ollama.com/) for [use in GPT for Excel – Autosheet](https://autosheet.com/docs/gpt-for-excel/setup/manage-models/connect-to-ollama) on macOS.

This guide assumes that you use Autosheet on the same machine that hosts Ollama.

**Prerequisites**

- Local machine with enough processing power and memory to run LLMs (see the [Ollama documentation](https://docs.ollama.com/gpu) for hardware support information)

- macOS Sonoma (version 14) or newer (see the [Ollama documentation](https://docs.ollama.com/macos) for detailed system requirements)

- Mac user account with administrator (sudo) privileges

- Installed software:

  - [curl](https://curl.se/) (ships with macOS)
  - [Homebrew](https://brew.sh/)
  - [OpenSSL](https://openssl-library.org/) (ships with macOS)

To set up an Ollama server on macOS:

1. [Install the server.](https://autosheet.com/docs/gpt-for-excel/setup/manage-models/connect-to-ollama/set-up-ollama-on-macos#install-an-ollama-server)
2. [Install a model on the server.](https://autosheet.com/docs/gpt-for-excel/setup/manage-models/connect-to-ollama/set-up-ollama-on-macos#install-a-model-on-the-ollama-server)
3. [Enable CORS for the server.](https://autosheet.com/docs/gpt-for-excel/setup/manage-models/connect-to-ollama/set-up-ollama-on-macos#enable-cors-for-the-ollama-server)
4. (Optional) [Enable HTTPS for the server.](https://autosheet.com/docs/gpt-for-excel/setup/manage-models/connect-to-ollama/set-up-ollama-on-macos#enable-https-for-the-ollama-server)

## Install an Ollama server

1. Download and run the [macOS installer](https://ollama.com/download). Follow the on-screen instructions to complete the installation.

   The installer starts the Ollama server in the background and sets the server to start automatically on system boot. The installer also installs the Ollama desktop application for easily starting and stopping the server.

   > **Tip**
   >
   > You can manually start a new instance of the Ollama server in a terminal by running `ollama serve`. However, to avoid port conflicts, make sure no other Ollama server instances are running at the same time.

2. Open Terminal and verify that the server is running:

   ```shell
   curl http://localhost:11434
   ```

   Response if the server is running:

   ```
   Ollama is running
   ```

You have installed the Ollama server.

For more information about installing and managing the server on macOS, see the [Ollama documentation](https://docs.ollama.com/macos).

## Install a model on the Ollama server

1. Install a model from the [Ollama library](https://ollama.com/search):

   ```shell
   ollama pull <model-name>
   ```

   For example, to install the [Meta Llama 3.2 3B](https://ollama.com/library/llama3.2) model:

   ```shell
   ollama pull llama3.2
   ```

   After the installation completes, the model is available for prompting on the Ollama server.

2. Verify that the model works:

   ```shell
   curl http://localhost:11434/api/generate -d '{ "model": "llama3.2", "prompt": "Who are you?", "stream": false }'
   ```

   The above request asks the server to generate a response for the specified prompt with the specified model and to return the response in a single reply. If the model works, the server returns a JSON object containing the response and metadata about the response.

You have installed the model on the Ollama server. For more information about working with models, see the [Ollama documentation](https://docs.ollama.com/cli).

## Enable CORS for the Ollama server

By default, the Ollama server only accepts [same-origin requests](https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Same-origin_policy). Since GPT for Excel – Autosheet always has a different origin from the Ollama server, you must enable [cross-origin resource sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) for the server using the `OLLAMA_ORIGINS` environment variable.

To enable CORS for the Ollama server:

1. Set `OLLAMA_ORIGINS` with the origins that are allowed to access the server:

   ```shell
   # Set a single origin
   launchctl setenv OLLAMA_ORIGINS "<ORIGIN>"

   # Set multiple origins
   launchctl setenv OLLAMA_ORIGINS "<ORIGIN_1>, <ORIGIN_2>, ..."
   ```

   For example:

   - Allow any origin to make requests to the server:

     ```shell
     launchctl setenv OLLAMA_ORIGINS "*"
     ```

   - Allow only Autosheet to make requests to the server:

     ```shell
     launchctl setenv OLLAMA_ORIGINS "https://excel-addin.gptforwork.com"
     ```

2. Restart the server for the variable to take effect.

3. Verify that the [`/api/tags`](https://github.com/ollama/ollama/blob/main/docs/api.md#list-local-models) endpoint of the server works:

   ```shell
   curl http://localhost:11434/api/tags
   ```

   Autosheet uses the endpoint to fetch a list of models installed on the server. If the endpoint works, the server returns a JSON object with a `models` property listing all currently installed models:

   ```
   {
     "models": [
       {
         ...
       }
     ]
   }
   ```

You have enabled CORS for the Ollama server.

> **What's next**
>
> You have completed the minimum setup required to access the Ollama server from Autosheet on the same machine. You can now set `http://localhost:11434` as the [endpoint URL in Autosheet](https://autosheet.com/docs/gpt-for-excel/setup/manage-models/connect-to-ollama#set-up-the-api-endpoint-for-an-ollama-server) provided the add-in is not running on Safari or on Microsoft Excel for Mac.
>
> If you use Autosheet on Safari or on Excel for Mac, [enable HTTPS for the Ollama server](https://autosheet.com/docs/gpt-for-excel/setup/manage-models/connect-to-ollama/set-up-ollama-on-macos#enable-https-for-the-ollama-server).

## Enable HTTPS for the Ollama server

The Ollama server uses HTTP to serve models, while GPT for Excel – Autosheet runs on HTTPS. By default, therefore, any request from Autosheet to the server is a [mixed-content request](https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content) (HTTP vs. HTTPS). Modern web browsers do not allow mixed content, as a rule. The only exceptions are mixed-content requests to `http://127.0.0.1` and `http://localhost`, which most browsers treat as safe and therefore allow. Safari, however, blocks mixed-content requests even on the current machine.

To avoid mixed content, you must make the Ollama server accessible over HTTPS if you use Autosheet from:

- Excel for the web on Safari
- Excel for Mac (uses Safari)

You make the Ollama server accessible over HTTPS by setting up a [reverse proxy](https://en.wikipedia.org/wiki/Reverse_proxy) that hides the server behind an HTTPS interface. This guide uses [nginx](https://nginx.org/) to set up the reverse proxy.

> **Tip**
>
> You can also use [Cloudflare Tunnel](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/), [ngrok](https://ngrok.com/), or a similar cloud-based tunneling service to set up HTTPS with minimal configuration. Note that with a cloud-based service your traffic will be routed through an external service over the internet.

To set up the reverse proxy:

1. Install nginx:

   ```shell
   brew install nginx
   ```

2. Create a self-signed SSL certificate for the Ollama server:

   1. Change to the nginx configuration directory (varies based on your Mac version):

      ```shell
      # Mac with an Apple silicon processor
      cd /opt/homebrew/etc/nginx

      # Mac with an Intel processor
      cd /usr/local/etc/nginx
      ```

   2. Generate the certificate:

      ```shell
      openssl req \
        -x509 \
        -newkey rsa:2048 \
        -nodes \
        -sha256 \
        -days 365 \
        -keyout ollama.key \
        -out ollama.crt \
        -subj '/CN=localhost' \
        -extensions extensions \
        -config <(printf "[dn]\nCN=localhost\n[req]\ndistinguished_name=dn\n[extensions]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
      ```

      The command generates two files in the current directory:

      - `ollama.crt`: Public self-signed certificate
      - `ollama.key`: Private key used to sign the certificate

   3. Add the certificate to trusted root certificates:

      ```shell
      sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ./ollama.crt
      ```

3. Create a site configuration for the Ollama server:

   1. Open the `nginx.conf` file in your preferred text editor. The file is in the nginx configuration directory:

      ```shell
      # Mac with an Apple silicon processor
      /opt/homebrew/etc/nginx/nginx.conf

      # Mac with an Intel processor
      /usr/local/etc/nginx/nginx.conf
      ```

   2. Replace the content of the file with the following configuration:

      ```nginx
      events {}

      http {
          server {
              listen       11435 ssl;
              server_name  localhost;

              http2 on;

              ssl_certificate      ollama.crt;
              ssl_certificate_key  ollama.key;

              location / {
                  proxy_pass  http://localhost:11434;

                  # Keep connection to backend alive.
                  proxy_http_version  1.1;
                  proxy_set_header    Connection '';
              }
          }
      }
      ```

      The configuration uses port `11435` for HTTPS. Requests to `https://localhost:11435` are forwarded to the Ollama server running at `http://localhost:11434`.

   3. Save and close the file.

   4. Test the nginx configuration:

      ```shell
      sudo nginx -t
      ```

      Response if the configuration is valid:

      ```
      nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok
      nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful
      ```

   5. Start nginx:

      ```shell
      sudo nginx
      ```

4. Verify that the [`/api/tags`](https://github.com/ollama/ollama/blob/main/docs/api.md#list-local-models) endpoint of the Ollama server works over the HTTPS connection:

   ```shell
   curl -k https://localhost:11435/api/tags
   ```

   Autosheet uses the endpoint to fetch a list of models installed on the server. If the endpoint works, the server returns a JSON object with a `models` property listing all currently installed models:

   ```
   {
     "models": [
       {
         ...
       }
     ]
   }
   ```

You have enabled HTTPS for the Ollama server.

> **What's next**
>
> You have completed the setup required to access the Ollama server from Autosheet on the same machine. You can now set `https://localhost:11435` as the [endpoint URL in Autosheet](https://autosheet.com/docs/gpt-for-excel/setup/manage-models/connect-to-ollama#set-up-the-api-endpoint-for-an-ollama-server), including when the add-in is running on Safari or on Microsoft Excel for Mac.
