# Set up LM Studio on macOS

This guide walks you through the main steps of setting up [LM Studio](https://lmstudio.ai/) for [use in GPT for Word](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-api-endpoints) on macOS.

This guide assumes that you use GPT for Word on the same machine that hosts LM Studio.

**Prerequisites**

- [System requirements](https://lmstudio.ai/docs/app/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 LM Studio on macOS:

1. [Install LM Studio and download a model.](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-api-endpoints/set-up-lm-studio-on-macos#install-lm-studio-and-download-a-model)
2. [Start and configure the LM Studio server.](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-api-endpoints/set-up-lm-studio-on-macos#start-and-configure-the-lm-studio-server)
3. (Optional) [Enable HTTPS for the LM Studio server.](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-api-endpoints/set-up-lm-studio-on-macos#enable-https-for-the-lm-studio-server)

## Install LM Studio and download a model

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

2. Run LM Studio.

3. On the welcome screen, click **Skip**.

4. In LM Studio, in the sidebar, select **Discover**.

5. Find and select the model you want to use, and click **Download**. For example, to get started with a small model, select [Llama 3.2 3B](https://huggingface.co/hugging-quants/Llama-3.2-3B-Instruct-Q8_0-GGUF).

   ![Selecting a model to download in LM Studio](https://autosheet.com/docs/article-images/servers/lm-studio-model-download.png)

   After the download completes, the model is available for prompting in LM Studio.

You have installed LM Studio and downloaded your first model. For more information about working with models, see the [LM Studio documentation](https://lmstudio.ai/docs/app/basics).

## Start and configure the LM Studio server

1. In LM Studio, in the sidebar, select **Developer**.

2. Click the **Status** toggle to change the status from **Stopped** to **Running**. You have started the LM Studio server.

   ![Starting the LM Studio server](https://autosheet.com/docs/article-images/servers/lm-studio-server-start.png)

3. Click **Settings**.

   ![Open LM Studio server settings](https://autosheet.com/docs/article-images/servers/lm-studio-server-settings.png)

4. Click the **Enable CORS** toggle to enable the setting.

   ![Enable CORS for the LM Studio server](https://autosheet.com/docs/article-images/servers/lm-studio-server-settings-cors.png)

   > **Info**
   >
   > By default, the LM Studio server only accepts [same-origin requests](https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Same-origin_policy). Since GPT for Word always has a different origin from the LM Studio server, you must enable [cross-origin resource sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) for the server.

5. To verify that the `/v1/models` endpoint of the LM Studio server works, open <http://127.0.0.1:1234/v1/models>.

   GPT for Word 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 `data` property listing all currently installed models:

   ![LM Studio /v1/models endpoint over HTTP in the browser](https://autosheet.com/docs/article-images/servers/lm-studio-models-endpoint-browser-http.png)

You have started and configured the LM Studio server.

> **What's next**
>
> You have completed the minimum setup required to access LM Studio from GPT for Word on the same machine. You can now set `http://127.0.0.1:1234` as the [endpoint URL in GPT for Word](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-api-endpoints#set-up-an-api-endpoint) provided the add-in is not running on Safari or on Microsoft Word for Mac.
>
> If you use GPT for Word on Safari or on Word for Mac, [enable HTTPS for the LM Studio server](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-api-endpoints/set-up-lm-studio-on-macos#enable-https-for-the-lm-studio-server).

## Enable HTTPS for the LM Studio server

The LM Studio server uses HTTP to serve models, while GPT for Word runs on HTTPS. By default, therefore, any request from GPT for Word 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 LM Studio server accessible over HTTPS if you use GPT for Word from:

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

You make the LM Studio 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 LM Studio 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 lm-studio.key \
        -out lm-studio.crt \
        -subj '/CN=127.0.0.1' \
        -extensions extensions \
        -config <(printf "[dn]\nCN=127.0.0.1\n[req]\ndistinguished_name=dn\n[extensions]\nsubjectAltName=DNS:localhost,IP:127.0.0.1\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
      ```

      The command generates two files in the current directory:

      - `lm-studio.crt`: Public self-signed certificate
      - `lm-studio.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 ./lm-studio.crt
      ```

3. Create a site configuration for the LM Studio 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       127.0.0.1:1235 ssl;
              server_name  127.0.0.1;

              http2 on;

              ssl_certificate      lm-studio.crt;
              ssl_certificate_key  lm-studio.key;

              location / {
                  proxy_pass  http://127.0.0.1:1234;

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

      The configuration uses port `1235` for HTTPS. Requests to `https://127.0.0.1:1235` are forwarded to the LM Studio server running at `http://127.0.0.1:1234`.

   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 `/v1/models` endpoint of the LM Studio server works over the HTTPS connection:

   ```shell
   curl -k https://127.0.0.1:1235/v1/models
   ```

   GPT for Word 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 `data` property listing all currently installed models:

   ```
   {
     "data": [
       {
         ...
       },
     ],
     "object": "list"
   }
   ```

You have enabled HTTPS for the LM Studio server.

> **What's next**
>
> You have completed the setup required to access LM Studio from GPT for Word on the same machine. You can now set `https://127.0.0.1:1235` as the [endpoint URL in GPT for Word](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-api-endpoints#set-up-an-api-endpoint), including when the add-in is running on Safari or on Microsoft Word for Mac.
