This guide shows how I run a local language model inside Emacs with gptel and Ollama, and how to do the same with LM Studio, which is what I used when I first wrote this article. gptel is a small Emacs package that talks to local or remote models from any buffer. For a wider look at the Emacs AI tools around it, see Emacs AI coding workflows.
Updated October 2026: my setup has moved from LM Studio to Ollama, so the Ollama configuration below is now the main one. The LM Studio section is kept, with two corrections: it needs an explicit model list, and the key to send a prompt is C-c RET, not C-c C-c.
What You Need
- Emacs 27.1 or later. That's gptel's own minimum.
- gptel, installed from MELPA.
- A local model server: Ollama or LM Studio.
- curl. gptel works without it, but it only streams responses when it can use curl.
Step 1: Install gptel
M-x package-refresh-contents
M-x package-install RET gptel RET
Then load it in your config:
(use-package gptel
:ensure t)
Step 2: Run Ollama and Pull a Model
Ollama runs as a local server with an HTTP API on port 11434 by default. Mine runs on my Ubuntu install, on the same machine as Emacs, so gptel reaches it on 127.0.0.1. Pull the model you want once:
ollama pull qwen3-coder:30b
ollama list shows what you have locally. The names it prints are the names gptel needs in the next step. I use qwen3-coder:30b, a coding model; pick whatever fits your hardware, since smaller models load faster and need less memory.
Step 3: Configure gptel for Ollama
This is the configuration I run:
(setq gptel-backend
(gptel-make-ollama "Ollama"
:host "127.0.0.1:11434"
:models '(qwen3-coder:30b)
:protocol "http"
:key "not-needed"))
(setq gptel-use-curl t
gptel-stream nil)
Notes on each part:
gptel-make-ollamadefaults tolocalhost:11434and Ollama's/api/chatendpoint, so:hostonly matters if Ollama runs somewhere else, such as another machine on your network.:modelshas to list the models you've pulled, using Ollama's names exactly asollama listshows them. The backend has no default list.- Ollama doesn't use an API key, so the
:keyline can be left out. gptel-use-curlis already on by default when curl is installed, so that line only restates the default.gptel-stream nilis a real change: gptel streams by default, and with this setting the whole response arrives at once instead of token by token.
Alternative: LM Studio
LM Studio provides an OpenAI-compatible local server, which gptel talks to through its generic OpenAI backend.
- Open LM Studio and load a model (I used a Qwen model).
- Start the local server. By default it listens on
http://localhost:1234/v1.
Then point gptel at it:
(setq gptel-backend
(gptel-make-openai "LM Studio"
:host "localhost:1234"
:protocol "http"
:endpoint "/v1/chat/completions"
:key "not-needed"
;; Use the model name LM Studio shows for the model you loaded.
:models '(your-model-name)))
The :models line wasn't in the first version of this article, and it matters: gptel-make-openai defaults to OpenAI's own model list, so without it gptel offers OpenAI model names that LM Studio doesn't have. The :key is a placeholder, since LM Studio doesn't require one by default.
This block is still in my config, commented out above the Ollama one.
Step 4: Start Chatting
M-x gptelopens a chat buffer connected to your backend.- Type a prompt and press
C-c RETto send it. The response appears below it, and you continue the conversation by typing underneath. - In any other buffer, select a region and run
M-x gptel-sendto send it as the prompt.
To switch backend or model, or set other options for a single request, use M-x gptel-menu.
Tips
- Multiple backends: you can define more than one, for example Ollama locally and a cloud API for harder questions, and switch between them in
gptel-menu. Emacs AI coding workflows covers setting up a cloud backend. - Org mode: gptel works well in Org buffers, for notes, code explanations, or drafting text without leaving Emacs.
- Code review: select a function, run
gptel-send, and ask for improvements; the answer is inserted in the buffer, where you can compare it with your code.
Conclusion
With gptel and a local server like Ollama or LM Studio, you get a language model inside Emacs without sending your code to a cloud API. The two setups differ by only a few lines of configuration, so moving from one to the other mostly means changing the backend definition.
Leave a comment
Comments are reviewed before they appear.