Code Offline with an AI Agent: OpenCode and Ollama Setup
Job to be done: Set up and use an offline AI coding agent for development
🇳🇬 Ways to use this in Nigeria
Ideas to get you started, adapt to your situation.
- Student
Explain a complex Python function in your project code offline for a school assignment using OpenCode and Ollama.
- 9-5 employee
Debug a failing test case in your codebase without internet access by asking OpenCode to analyze the error.
- Entrepreneur
Refactor a small section of your app's code offline to improve readability using OpenCode and a local LLM.
What you’ll get
You will set up an AI coding agent that runs entirely on your laptop, allowing you to work on coding tasks without needing an internet connection. This approach combines OpenCode, an open-source agent, with Ollama, a tool that runs large language models (LLMs) locally, using an open-weight model like Qwen. This setup is perfect for explaining functions, investigating failing tests, or refactoring small pieces of code even when you are offline.
Tools you need
- OpenCode (free): An open-source AI agent that interacts with your project files and sends requests to a local AI model.
- Ollama (free): A tool that lets you download and run large language models (LLMs) like Qwen directly on your computer.
Steps
-
Install Ollama: Go to the official Ollama website and download the installer for your operating system (macOS, Windows, or Linux). Follow the on-screen instructions to complete the installation. You should see Ollama running in the background, often as an icon in your system tray or menu bar.
-
Download the Qwen model: Open your computer’s terminal (Command Prompt or PowerShell on Windows, Terminal on macOS/Linux). Type the following command to download the Qwen 9B model. This step requires an active internet connection and may take some time as the model is several gigabytes in size.
ollama run qwen:9bOllama will start downloading the model. Once downloaded, it will automatically load and run the model, and you will see a prompt from Ollama, indicating it’s ready.
-
Install OpenCode: OpenCode is a Python tool, so you will need Python and its package installer,
pip, installed on your computer. If you don’t have them, search online for ‘install Python’ for your operating system. Once Python andpipare ready, open your terminal again and type:pip install opencode-aiYou should see messages indicating that OpenCode and its dependencies are being downloaded and installed. Once complete, you will return to your terminal prompt.
-
Start OpenCode and connect to Ollama: Navigate to your coding project folder in the terminal using the
cdcommand (e.g.,cd /path/to/your/project). Then, start OpenCode, telling it to use the Qwen model running via Ollama:opencode --model ollama/qwen:9bOpenCode will start up and connect to the Qwen model running locally through Ollama. You should see OpenCode’s interface in your terminal, ready for your commands.
-
Give OpenCode a task (offline): Now you can disconnect from the internet. Type your coding task directly into the OpenCode prompt. The author doesn’t share their exact prompt, but here’s a starting point:
Explain the purpose and usage of the 'calculate_total' function in this project.OpenCode will read your project files and provide a response or propose changes. It will ask for your permission before running any commands or making edits, allowing you to review its actions.
Original source
This workflow is based on an article by Rittika Jindal titled ‘Coding From the Himalayas With an Offline AI Agent’, published on the Latent Space blog. The author shares their experience using these tools to maintain a coding workflow even in remote locations with unreliable internet.
Notes & variations
- Common Pitfall: Ensure you have enough RAM (memory) on your computer. Running large language models like Qwen 9B locally requires significant system resources. If your computer struggles, consider trying a smaller model (e.g.,
qwen:4bif available via Ollama) or closing other demanding applications. Also, remember that the model download in Step 2 requires a stable internet connection; you cannot do this step offline. - Tip for Better Results: Start with smaller, well-defined tasks for the AI agent, such as explaining a specific function or refactoring a small code block. This helps you understand how the agent works and allows you to review its suggestions more effectively.