WSL + OpenCode Environment Manual
1. Basic Systemβ
- OS: Windows 10 Pro
- WSL: WSL2
- Linux Distro: Ubuntu 22.04 LTS
- User: ollama
2. WSL Installation and Entryβ
Install Ubuntuβ
wsl --install -d Ubuntu-22.04
Download size about 500-700MB, occupies about 1.2-1.6GB after installation
Enter WSLβ
wsl -d Ubuntu-22.04 -u ollama
View Installed Distributionsβ
wsl -l -v
3. Node / Package Managementβ
- Node.js: v20.x (Linux Native Install)
- Corepack: Enabled
- pnpm: Corepack managed (pnpm@latest)
- PNPM_HOME:
~/.local/share/pnpm - PATH already includes PNPM_HOME
Node Installation Commandβ
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
Enable pnpmβ
sudo corepack enable
sudo corepack prepare pnpm@latest --activate
pnpm setup
source ~/.bashrc
4. OpenCode Installationβ
Recommended Method (Official Binary)β
curl -fsSL https://opencode.ai/install | bash
source ~/.bashrc
Verifyβ
which opencode # Should be /usr/local/bin/opencode
opencode --version
5. OpenCode Serverβ
- Docs: windows-wsl
- Start Command:
opencode serve --hostname 0.0.0.0 --port 4096 - Usage: Provide Desktop / Web Client remote access
6. OpenCode Usage Mode Comparisonβ
| Mode | Context Management | Token Consumption | Suitable Scenario |
|---|---|---|---|
| Desktop | Session History Accumulation | Higher | Decision/Clarify Requirement |
| Desktop + WSL | Engineering Driven | Medium | Heavy Modification/Debug |
| CLI | Independent Call | Lowest | Batch/Atomic patch |
| TUI | Visualized CLI | Low | High Frequency Execution |
Workflow Suggestionsβ
- Desktop: Used for "Thinking clearly what to change"
- CLI/TUI: Used for "Changing code correctly"
- Use Snapshot to replace history, do one thing per call
7. Token Optimization Strategyβ
Core Principlesβ
- Reduce Context: Use Task Snapshot to replace history
- Reduce Rounds: Sub-agent call at once
- Structured Output: Forbid explanation, only give diff
Low Token Prompt Templateβ
Task Snapshot:
- Project: Spring Boot 2.7
- Goal: [Clear Goal]
- Constraints: [Constraints]
Output:
- Only unified diff
- No explanation
8. Python Environmentβ
sudo apt install -y python3 python3-pip python3-venv
pip3 install --user virtualenv ipython black isort pylint mypy
# Use tee to add environment variable (no need to enter editor)
echo 'export PATH="$HOME/.local/bin:$PATH"' | tee -a ~/.bashrc
source ~/.bashrc
9. Java Environmentβ
sudo apt install -y openjdk-17-jdk maven
- JAVA_HOME: Configured
- Usage: OpenCode code understanding and execution
10. Auxiliary Toolsβ
sudo apt install -y git build-essential tree ripgrep fd-find curl wget unzip
11. tmux Terminal Multiplexerβ
Installβ
sudo apt install -y tmux
Common Commandsβ
# Create new session
tmux new -s dev
# List sessions
tmux ls
# Attach to session
tmux attach -t dev
# Detach session (Inside tmux)
Ctrl+b d
# Close window (Inside tmux)
Ctrl+d
# Kill session
tmux kill-session -t dev
Window Operations (Inside tmux)β
| Shortcut | Function |
|---|---|
Ctrl+b c | New Window |
Ctrl+b n | Next Window |
Ctrl+b p | Previous Window |
Ctrl+b % | Vertical Split |
Ctrl+b " | Horizontal Split |
Ctrl+b Arrow | Switch Panel |
Set Container Scrollingβ
Write to tmux config file
Edit:
~/.tmux.conf
Add a line:
echo 'set -g mouse on' | tee -a ~/.tmux.conf
echo 'bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "clip.exe"' | tee -a ~/.tmux.conf
Use with OpenCodeβ
# Create dev session and start opencode server in it
tmux new -s dev -d 'opencode serve --hostname 0.0.0.0 --port 4096'
# Attach to view
tmux attach -t dev