Set up Ollama, NGROK, and LangChain
Source: Dev.to
This post shows how to quickly set up Ollama with ngrok and use it in LangChain.
Ollama
You can easily download Ollama for Linux, macOS, and Windows:
👉
Test if it’s running
curl http://localhost:11434; echo # should output “Ollama is running”
Note: If nothing is returned, start the server with
ollama serve.
Pull your preferred models
ollama pull phi4-mini
ollama pull nomic-embed-text:v1.5


Ngrok
Download ngrok for Linux, macOS, and Windows:
👉
Note: Remember to add your authtoken!
ngrok config add-authtoken
Expose Ollama with ngrok (basic auth)
ngrok http 11434 \
--host-header="localhost:11434" \
--basic-auth="username:password"

The command generates a public URL, e.g.:
https://09c6b3946ddd.ngrok-free.app
You can test it in a browser:

After successful authentication you’ll see “Ollama is running”:

You can also verify the models via the /api/tags endpoint:

✨ The two models pulled earlier are now ready to use.
LangChain & Ollama
Install the required Python packages:
pip install langchain langchain-core langchain-ollama
Use Ollama (LLM & Embeddings) from another network
import base64
from langchain_ollama.llms import OllamaLLM
from langchain_ollama.embeddings import OllamaEmbeddings
BASE_URL = "https://09c6b3946ddd.ngrok-free.app"
user_pass = b"username:password"
auth_header = base64.b64encode(user_pass).decode("utf-8")
client_kwargs = {
"headers": {
"Authorization": f"Basic {auth_header}"
}
}
llm = OllamaLLM(
model="phi4-mini:latest",
base_url=BASE_URL,
client_kwargs=client_kwargs,
)
embeddings = OllamaEmbeddings(
model="nomic-embed-text:v1.5",
base_url=BASE_URL,
client_kwargs=client_kwargs,
)
# Test the LLM
print(llm.invoke("Hello, Ollama LLM =)"))
# Test the embeddings
print(embeddings.embed_query("Hello, Ollama Embeddings =)"))
Result example:

