Fix "could not connect to ollama" and connection refused on port 11434
Error: could not connect to ollama app, is it running?
- ConnectionError: Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download
- requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=11434): Max retries exceeded with url: /api/generate (Caused by NewConnectionError('Failed to establish a new connection: [Errno 111] Connection refused'))
- TypeError: fetch failed [cause]: Error: connect ECONNREFUSED 127.0.0.1:11434
- Error: connect ECONNREFUSED ::1:11434
The client (the ollama command, your script or your app) cannot reach the Ollama server on 127.0.0.1:11434. Start the server (Ollama app, systemctl start ollama or ollama serve), check with curl http://127.0.0.1:11434, then make sure the client targets the right address: from Docker it is not localhost but host.docker.internal or the service name.
What this error means
Ollama works in two parts: a server that loads models and exposes an HTTP API, by default on 127.0.0.1:11434, and clients (ollama run CLI, Python or JavaScript libraries, LangChain, Open WebUI…) that send requests to that address. “Connection refused” means nothing is listening at the target address and port: either the server is not running, or it listens elsewhere, or the client uses the wrong host. This is very common inside a Docker container, where localhost means the container itself and not your machine, and with Node.js, where localhost can resolve to ::1 (IPv6) while Ollama only listens on IPv4.
Common causes
- Ollama server stopped: app not started on macOS or Windows,
ollamaservice stopped or crashed on Linux, orollama serveclosed along with its terminal. - Client inside a Docker container:
http://localhost:11434targets the container, not the host running Ollama. - Ollama listens on loopback only: by default it listens on
127.0.0.1, so another machine, a VM or a Linux container cannot reach it. - Wrong address on the client side: an
OLLAMA_HOSTvariable pointing elsewhere, a changed port, orlocalhostresolved to IPv6::1by Node.js.
Step-by-step fix
-
Check whether the server answers
Query the API directly. An “Ollama is running” reply proves the server works: the problem is then on the client side. A connection error confirms the server is not running at that address.
curl http://127.0.0.1:11434 # Ollama is running curl http://127.0.0.1:11434/api/tags -
Start Ollama
On macOS and Windows, launch the Ollama app (icon in the menu bar or the notification area). On Linux, the installer creates a systemd service: start it and read its logs if it stops. You can also run the server in the foreground with
ollama serve.# Linux (systemd service) sudo systemctl start ollama systemctl status ollama journalctl -u ollama -e # any OS, in the foreground ollama serve -
Point the client to the right address
Use
127.0.0.1instead oflocalhostto avoid Node.js resolving to IPv6. The CLI and the Python library read theOLLAMA_HOSTvariable; you can also pass the host explicitly. For the OpenAI-compatible API, the base URL ishttp://127.0.0.1:11434/v1.from ollama import Client client = Client(host="http://127.0.0.1:11434") response = client.chat( model="llama3.2", messages=[{"role": "user", "content": "Hello"}], ) print(response.message.content) -
From Docker: target the host or a service
If Ollama runs on the host machine, use
http://host.docker.internal:11434(automatic with Docker Desktop; on Linux, addextra_hosts). The simplest option is often to run Ollama as a service in the samecompose.yamland call it by name, without publishing the port.services: app: build: . environment: OLLAMA_HOST: http://ollama:11434 depends_on: - ollama ollama: image: ollama/ollama volumes: - ollama:/root/.ollama # Ollama on the host instead (Linux): # app: # environment: # OLLAMA_HOST: http://host.docker.internal:11434 # extra_hosts: # - "host.docker.internal:host-gateway" volumes: ollama: -
Make Ollama listen beyond loopback (carefully)
For a Linux container, a VM or another machine to reach Ollama, make it listen on another interface with
OLLAMA_HOST=0.0.0.0:11434. Beware: the Ollama API has no authentication. Never expose port 11434 to the Internet; restrict it to the local network with the firewall, or put an authenticated reverse proxy in front. On macOS, uselaunchctl setenv OLLAMA_HOST "0.0.0.0:11434"then restart the app; on Windows, set theOLLAMA_HOSTuser environment variable, then quit and relaunch Ollama.# Linux: add an override to the service sudo systemctl edit ollama.service # [Service] # Environment="OLLAMA_HOST=0.0.0.0:11434" sudo systemctl daemon-reload sudo systemctl restart ollama
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Keep the Ollama URL in an environment variable (
OLLAMA_HOST) instead of hardcoding it. - Enable the service at boot on Linux with
sudo systemctl enable ollama. - In Docker Compose, add a
healthcheckon Ollama and wait for it to be healthy before starting the app.
Frequently asked questions
ollama serve says “address already in use”, what should I do?
The listen tcp 127.0.0.1:11434: bind: address already in use message means an Ollama server is already running (often the app or the systemd service). Do not start a second server: just use ollama run.
Why ECONNREFUSED ::1:11434 while Ollama is running?
Node.js resolved localhost to IPv6 ::1, while Ollama listens on 127.0.0.1. Replace localhost with 127.0.0.1 in the URL.
Is it risky to set OLLAMA_HOST=0.0.0.0?
Yes if the machine is reachable from the Internet: anyone could use your models and resources. Restrict access with a firewall or an authenticated reverse proxy, or connect containers through an internal Docker network.
Useful tools for this error
More AI & LLMs errors
- Fix error 429 "Rate limit reached" on OpenAI, Claude and Gemini APIs
- Fix "maximum context length" and "prompt is too long" (context_length_exceeded)
- Fix "CUDA out of memory" in PyTorch
- Fix error 401 "Incorrect API key provided" / "invalid x-api-key"
- Fix "Torch not compiled with CUDA enabled" and torch.cuda.is_available() returning False
- Fix "JSONDecodeError: Expecting value" when parsing an LLM response
- Fix "You are trying to access a gated repo" (401) on Hugging Face