Skip to main content

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​

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​

ModeContext ManagementToken ConsumptionSuitable Scenario
DesktopSession History AccumulationHigherDecision/Clarify Requirement
Desktop + WSLEngineering DrivenMediumHeavy Modification/Debug
CLIIndependent CallLowestBatch/Atomic patch
TUIVisualized CLILowHigh 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​

  1. Reduce Context: Use Task Snapshot to replace history
  2. Reduce Rounds: Sub-agent call at once
  3. 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)​

ShortcutFunction
Ctrl+b cNew Window
Ctrl+b nNext Window
Ctrl+b pPrevious Window
Ctrl+b %Vertical Split
Ctrl+b "Horizontal Split
Ctrl+b ArrowSwitch 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