Back to Blog

Running DeepSeek Models Locally with Ollama: Complete Guide

TheDBAdmin Team4 min read
Running DeepSeek Models Locally with Ollama: Complete Guide

Learn how to run powerful DeepSeek AI models locally on your computer using Ollama. This guide covers installation, configuration, model management, and practical usage examples.

Running powerful AI models locally offers privacy, reduced costs, and lower latency. This guide will show you how to run DeepSeek models on your computer using Ollama, a tool designed for managing and running AI models locally.

Table of Contents

  1. Prerequisites
  2. Installing Ollama
  3. Setting Up DeepSeek Models
  4. Usage Examples
  5. Performance Optimization
  6. Troubleshooting
  7. Best Practices

Prerequisites

Before starting, ensure your system meets these requirements:

  • Hardware Requirements:

    • Minimum 16GB RAM (32GB recommended)
    • NVIDIA GPU with 8GB+ VRAM (for GPU acceleration)
    • 50GB+ free disk space
  • Software Requirements:

    • Windows 10/11, macOS, or Linux
    • NVIDIA drivers (if using GPU)
    • Docker (optional, but recommended)

Installing Ollama

1. Windows Installation

powershell
# Using Windows Package Manager (winget)
winget install Ollama.Ollama

# Or download and run the installer from:
# https://ollama.ai/download/windows

2. macOS Installation

bash
# Using Homebrew
brew install ollama

# Or using curl
curl -fsSL https://ollama.ai/install.sh | sh

3. Linux Installation

bash
# Using curl
curl -fsSL https://ollama.ai/install.sh | sh

# For Ubuntu/Debian
curl -fsSL https://ollama.ai/install.sh | sudo bash

Setting Up DeepSeek Models

1. Available DeepSeek Models

bash
# List available DeepSeek models
ollama list

# Pull DeepSeek models
ollama pull deepseek-coder:6.7b
ollama pull deepseek-coder:33b
ollama pull deepseek-llm:7b

2. Custom Model Configuration

yaml
# modelfile
FROM deepseek-coder:6.7b
PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER num_ctx 8192

# System prompt configuration
SYSTEM """You are an AI programming assistant. Write code that is:
- Well-documented
- Efficient
- Following best practices
"""

3. Building Custom Model

bash
# Build custom model from modelfile
ollama create deepseek-custom -f modelfile

# Verify model creation
ollama list

Usage Examples

1. Basic Interaction

bash
# Start interactive session
ollama run deepseek-coder:6.7b

# Or use single command
echo "Write a Python function to calculate fibonacci numbers" | ollama run deepseek-coder:6.7b

2. Code Generation

python
# Example response from DeepSeek Coder
def fibonacci(n):
    """
    Calculate the nth Fibonacci number using dynamic programming.
    
    Args:
        n (int): The position in the Fibonacci sequence (0-based)
        
    Returns:
        int: The nth Fibonacci number
    """
    if n <= 1:
        return n
        
    fib = [0] * (n + 1)
    fib[1] = 1
    
    for i in range(2, n + 1):
        fib[i] = fib[i-1] + fib[i-2]
        
    return fib[n]

3. API Integration

python
# Using Python with Ollama API
import requests

def query_deepseek(prompt):
    response = requests.post('http://localhost:11434/api/generate',
        json={
            'model': 'deepseek-coder:6.7b',
            'prompt': prompt,
            'stream': False
        })
    return response.json()['response']

# Example usage
code = query_deepseek("Write a function to sort a list in Python")
print(code)

Performance Optimization

1. GPU Acceleration

bash
# Check GPU availability
nvidia-smi

# Enable GPU in Ollama
export OLLAMA_CUDA=1
export OLLAMA_GPU_LAYERS=35

2. Memory Management

bash
# Configure memory limits
export OLLAMA_HOST=0.0.0.0
export OLLAMA_MODELS=/path/to/models
export OLLAMA_MEMORY_LIMIT=16GB

3. Context Window Optimization

yaml
# Modelfile with optimized parameters
FROM deepseek-coder:6.7b
PARAMETER num_ctx 4096  # Adjust based on your RAM
PARAMETER num_gpu 1
PARAMETER num_thread 8

Troubleshooting

1. Common Issues

bash
# Check Ollama status
ollama serve status

# View logs
tail -f ~/.ollama/logs/ollama.log

# Reset Ollama
ollama kill
ollama serve

2. Memory Issues

bash
# Monitor memory usage
ps aux | grep ollama
nvidia-smi -l 1  # GPU memory monitoring

# Clear model cache
rm -rf ~/.ollama/models/*
ollama pull deepseek-coder:6.7b  # Re-download model

Best Practices

1. Model Management

bash
# Regular maintenance
ollama list  # Check installed models
ollama rm unused-model  # Remove unused models
ollama pull model:latest  # Update models

2. Production Setup

yaml
# Docker compose configuration
version: '3.8'
services:
  ollama:
    image: ollama/ollama
    container_name: ollama-deepseek
    ports:
      - "11434:11434"
    volumes:
      - ./models:/root/.ollama/models
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]

3. Security Considerations

bash
# Secure API access
export OLLAMA_HOST=localhost  # Restrict to local access
export OLLAMA_ORIGINS=https://your-app.com  # CORS configuration

# Use environment variables for sensitive settings
export OLLAMA_API_KEY="your-api-key"

Conclusion

Running DeepSeek models locally with Ollama provides a powerful way to leverage AI capabilities while maintaining control over your data and resources. Key takeaways:

  • Choose appropriate model size based on your hardware
  • Optimize performance using GPU acceleration
  • Monitor resource usage and adjust configurations
  • Follow security best practices for production deployments

Additional Resources

Written by

TheDBAdmin Team

KEEP LEARNING WITH THEDBADMIN

Turn this guide into a skill you can prove

Keep reading

Featured course · New batch

NextGen DBA + AI Engineer Bootcamp

Instructor-led, lab-first training for production DBAs: PostgreSQL, MongoDB, Docker, Kubernetes and AI.

  • Live labs on real execution plans
  • Backup, PITR & HA in production
  • pgvector, RAG & AI for DBAs

🛒 FROM THE SHOP

AWS CLI for PostgreSQL DBA

PDF · 34 pages

₹50₹50090% off

View product