Ollama for Coding
What this article covers
- Which models work best for code generation.
- How to connect Ollama with editors and IDEs.
- Tips for prompting code tasks effectively.
- FIM and Fill-In-the-Middle for code completion.
- Use cases: refactoring, testing, and explanations.
Introduction: Ollama for Coding
Local coding assistants built with Ollama are now highly practical. Specialized code models can generate functions, explain code, find bugs, suggest refactorings, and write tests. Since everything runs locally, your source code stays in your own environment. This matters especially for business-critical or sensitive code.
This article shows which Ollama models suit coding work, how to set them up, and how to use them most effectively.
Key Terms
- Code model: Language model specialized in programming code.
- FIM: Fill-In-the-Middle, completing gaps in code.
- Completion: Code autocompletion.
- Refactoring: Improving code without changing its behavior.
- Unit test: Small test for a single function.
- Prompt engineering: Strategic formulation of tasks.
- IDE: Integrated development environment.
Suitable Models
These models are recommended for coding:
- qwen2.5-coder: Strong coding model with good language support.
- codellama: Code-optimized Llama variants.
- codegemma: Lightweight and fast models from Google.
- starcoder2: Specialized in source code.
- deepseek-coder: Good performance in larger sizes.
To download:
ollama pull qwen2.5-coder:14b
ollama pull codellama:13b
Connecting with Your IDE
Continue
Continue is a popular IDE extension for VS Code and JetBrains. You can connect it to Ollama:
{
"models": [
{
"title": "Ollama Qwen",
"model": "qwen2.5-coder:14b",
"provider": "ollama"
}
]
}
Open WebUI for Coding
Open WebUI also works with Ollama coding models. It’s especially handy for longer explanations or refactoring tasks.
Prompts for Coding
Good prompts are specific and include context:
Writing a Function
Write a Python function that sorts a list of numbers and returns the 10 largest values.
Explaining Code
Explain this code in three sentences:
```python
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
### Refactoring
```text
Refactor this code to make it clearer and more error-resistant.
Writing Tests
Write unit tests for the following function using pytest.
FIM and Fill-In-the-Middle
Some models support FIM to complete code at a specific point. This is useful for IDE autocompletion. The request includes prefix and suffix:
<FIM_PREFIX>
def greet(name):
print(<FIM_SUFFIX>)
<FIM_MIDDLE>
Not all models natively support FIM. Your IDE extension must also support it.
Useful Workflows
- Code completion: Model fills in open lines.
- Function generation: Prompt with input, output, and examples.
- Code explanation: Understand complex code sections.
- Refactoring: Improve code quality.
- Testing: Automatically generate test cases.
- Documentation: Write comments and docstrings.
Tips for Better Results
- Name the specific programming language.
- Specify input, output, and examples.
- Mention desired libraries and coding conventions.
- Include context from files when possible.
- Use larger models for complex tasks.
- Always manually review generated code.
Common Pitfalls
- Wrong model: General chat models often underperform compared to specialized coder models.
- Insufficient context: The model doesn’t know the file.
- FIM not supported: Your model or IDE lacks this feature.
- Code quality: Generated code requires review.
- Security issues: Generated code may contain bugs or vulnerabilities.
- Slow inference: Large models consume more VRAM.
Further Resources
- BotServ.de Ollama Commands
- BotServ.de Ollama Performance
- BotServ.de Ollama Model Lifecycle
- BotServ.de Continue
- BotServ.de Local Coding Models
FAQ: Ollama for Coding
Which model is best for coding?
qwen2.5-coder and codellama are solid starting points.
Can I use Ollama in VS Code? Yes, through extensions like Continue.
Is generated code secure? No, always review and test generated code.
How large should the model be? 13B to 34B models deliver good coding results. Smaller models work fine for simple tasks.
What is FIM? Fill-In-the-Middle, a technique for completing gaps in code.
Sources and Further Reading
- Qwen2.5 Coder: https://qwenlm.github.io/
- Code Llama: https://ai.meta.com/blog/code-llama-large-language-models-coding/
- Continue: https://continue.dev/
Summary: Ollama for Coding
Ollama is excellent for running local coding assistants. Coder models like qwen2.5-coder and codellama deliver solid results for code generation, explanation, refactoring, and testing. With extensions like Continue, you can integrate Ollama directly into your IDE. The key ingredients are concrete prompts, sufficient context, and manual review of generated output. Used properly, you get a privacy-friendly assistant that genuinely improves your workflow.


