How to beginner · 3 min read

How to run a model with Ollama

Quick answer
To run a model with Ollama, install the ollama Python package or use the CLI to send prompts to a local or remote model. Use ollama run <model> in the terminal or the Python client’s client.chat() method to interact with the model.

PREREQUISITES

  • Python 3.8+
  • pip install ollama
  • Ollama CLI installed and configured
  • Access to an Ollama model (local or remote)

Setup

Install the Ollama Python client and ensure the Ollama CLI is installed on your system. Set up your environment to access the desired model.

bash
pip install ollama
output
Collecting ollama
  Downloading ollama-x.x.x-py3-none-any.whl (xx kB)
Installing collected packages: ollama
Successfully installed ollama-x.x.x

Step by step

Use the Ollama CLI or Python client to run a model. Below is a complete Python example that sends a prompt to the llama2 model and prints the response.

python
import ollama

response = ollama.chat(
    model="llama2",
    messages=[{"role": "user", "content": "Hello, Ollama!"}]
)

print(response['choices'][0]['message']['content'])
output
Hello! How can I assist you today?

Common variations

You can run models via the CLI with ollama run <model> and interactively input prompts. The Python client supports streaming responses and different models by changing the model parameter.

bash
## CLI example
# Run the llama2 model interactively
ollama run llama2

## Python streaming example
import ollama

for chunk in ollama.chat_stream(
    model="llama2",
    messages=[{"role": "user", "content": "Stream response example."}]
):
    print(chunk['choices'][0]['delta']['content'], end='')

Troubleshooting

  • If you see Model not found, verify the model name and that it is installed locally or accessible remotely.
  • If the CLI command hangs, check your Ollama daemon is running.
  • For authentication errors, ensure your API keys or access tokens are correctly configured.

Key Takeaways

  • Use the Ollama CLI or Python client to run models locally or remotely.
  • Install the ollama Python package for easy integration in scripts.
  • Change the model parameter to switch between different Ollama models.
  • Streaming responses are supported via the Python client’s chat_stream method.
  • Check model availability and daemon status if you encounter errors.
Verified 2026-04 · llama2
Verify ↗