Buy @ Amazon

Accessing Windows Host from WSL2 Linux using Hostname

Problem Statement

I have my AI Agent development done in WSL2 Ubuntu environment on my Windows machine. And I have my Ollama running on my Windows host machine for simplicity and better performance with LLM model leveraging the GPU hardware directly on the host machine. The problem however is that my AI Agent in WSL2 can't access the Ollama service in Windows host, over http using localhost domain name. 

Inside WSL2, localhost refers to the WSL2 VM itself, not your Windows host. This is one of the key differences between WSL1 and WSL2 architectures.

Solution

Option 1: Use Windows Host IP

This is exactly what you've been doing—and it's the officially recommended approach from Microsoft
```bash
# From WSL2, get the Windows host IP
ip route show | grep default | awk '{print $3}'
# Returns something like 172.18.0.1
```
Then in your code you can access the windows host service using its IP got in the above step like below:
```python
model = Ollama(host="http://172.18.0.1:11434")
```

Option 2: Get Windows Host IP on WSL2. Map it to WINHOST DNS in its hosts file. Use that host name for convenience.

Add the below script to the end of your .bashrc or .zshrc file in your wsl2.
-- --
Then save the file and reload your terminal by running the command below:
`source .zshrc`

You can now access the windows service in your python code like below:
```python
model = Ollama(host="http://winhost:11434")
```