Intro

Ollama is a lightweight, extensible framework for building and running language models on a local machine.
It supports Llama, DeepSeek-R1, Phi-4, Mistral, Gemma 3, and other models.

Start Ollama

  1. Download from the official website and run:
./ollama serve
  1. Run with Docker:
docker run -d \
  -v ollama:/root/.ollama \
  -p 11434:11434 \
  --name ollama ollama/ollama

(Run docker pull ollama/ollama first if needed)

After this, a local Ollama server daemon is running.

Usage of Ollama

Run and chat with a model:

ollama run llama3

Run model inside Docker:

docker exec -it ollama ollama run llama3

REST API

Generate text:

curl http://localhost:11434/api/generate -d '{
  "model": "llama3.2",
  "prompt": "Why is the sky blue?"
}'

Chat API:

curl http://localhost:11434/api/chat -d '{
  "model": "llama3.2",
  "messages": [
    { "role": "user", "content": "why is the sky blue?" }
  ]
}'

Other Libraries

Python

Install:

pip install ollama

Javascript

Install:

npm i ollama

Other Commands

List models:

ollama list

Show model info:

ollama show llama3.2

List loaded models:

ollama ps

Stop a running model:

ollama stop llama3.2

Start Ollama:

ollama serve

Python and JavaScript SDKs provide equivalent functions (e.g. ollama.list()).

A Sample

Python

Chat with local Ollama in Python

from ollama import Client

client = Client(
    host='http://localhost:11434',
    headers={'x-some-header': 'value-zhuzhu'}
)

response = client.chat(
    model='llama3.2',
    messages=[
        {
            'role': 'user',
            'content': 'Why is the sky blue?',
        },
    ]
)

print(response)
print(response.message.content)

Output:

model='llama3.2' created_at='2025-03-19T11:55:43.926128969Z' done=True done_reason='stop' total_duration=1114604658758 load_duration=22662584 prompt_eval_count=31 prompt_eval_duration=5412000000 eval_count=321 eval_duration=1109162000000 message=Message(role='assistant', content="The sky appears blue because of a phenomenon called scattering, which occurs when sunlight interacts with the tiny molecules of gases in the Earth's atmosphere.\n\nHere's what happens:\n\n1. Sunlight enters the Earth's atmosphere and consists of a spectrum of colors, including all the colors of the visible light spectrum.\n2. When sunlight passes through the atmosphere, it encounters tiny molecules of gases such as nitrogen (N2) and oxygen (O2).\n3. These molecules scatter the shorter (blue) wavelengths of light more than the longer (red) wavelengths. This is known as Rayleigh scattering, named after the British physicist Lord Rayleigh, who first described the phenomenon in the late 19th century.\n4. The blue light is scattered in all directions by the atmospheric molecules, while the longer wavelengths of light, such as red and orange, continue to travel in a straight line and reach our eyes from a more direct path.\n5. Since the Earth's atmosphere scatters shorter wavelengths (like blue) more than longer wavelengths (like red), the sky appears blue to our eyes because of this scattering effect.\n\nIt's worth noting that the color of the sky can change under different conditions, such as:\n\n* During sunrise and sunset, when the light has to travel through more of the Earth's atmosphere, which scatters the shorter wavelengths even more, making the sky appear reddish.\n* At high altitudes or in areas with low atmospheric pressure, where there are fewer molecules for the light to scatter off, the sky can appear more blue.\n\nSo, that's the reason why the sky is blue!", images=None, tool_calls=None)

Javascript

Chat with local Ollama in JS

import ollama from 'ollama'

const response = await ollama.chat({
  model: 'llama3.1',
  messages: [{ role: 'user', content: 'Why is the sky blue?' }],
})

console.log(response.message.content)

References

  1. Ollama Official Website
  2. Ollama GitHub
  3. Ollama Docker Image
  4. ollama-python
  5. ollama-js


blog comments powered by Disqus

Published

21 March 2025

Tags