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​

source ~/.bashrc
which opencode # Should be ~/.opencode/bin/opencode or /usr/local/bin/opencode
opencode --version
Actual Installation Location

When installed using the official script, opencode is installed at ~/.opencode/bin/opencode, and PATH is automatically added to ~/.bashrc.

5. OpenCode Server​

  • Docs: windows-wsl
  • Start Command: opencode serve --port 4096 --hostname 0.0.0.0
Note

Arguments must be passed separately: --port and --hostname are independent parameters, cannot be combined.

  • Usage: Provide Desktop / Web Client remote access
  • Access Address: http://0.0.0.0:4096 (accessible from other devices on the same network)

5.1 oh-my-opencode Installation & Configuration​

Install oh-my-opencode​

# Global installation (requires root)
sudo npm install -g oh-my-opencode

Copy Configuration (if you already have config)​

Copy configuration from Windows to WSL:

# Copy opencode configuration
cp /mnt/c/Users/YOUR_WINDOWS_USERNAME/.config/opencode/opencode.json ~/.config/opencode/
cp /mnt/c/Users/YOUR_WINDOWS_USERNAME/.config/opencode/oh-my-opencode.json ~/.config/opencode/

# Copy plugins (if any)
cp -r /mnt/c/Users/YOUR_WINDOWS_USERNAME/.config/opencode/plugins ~/.config/opencode/

# Set permissions
chown -R ollama:ollama ~/.config/opencode
Notes
  • Absolute paths in Windows config files (like E:\project\) need to be manually changed to WSL paths (like /mnt/e/project/)
  • Windows batch file paths in MCP configuration need to be changed to WSL shell script paths

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 configuration file:

# Method 1: Direct append
echo 'set -g mouse on' >> ~/.tmux.conf
echo 'bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "clip.exe"' >> ~/.tmux.conf

# Method 2: Using tee (requires root)
sudo tee -a ~/.tmux.conf << 'EOF'
set -g mouse on
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "clip.exe"
EOF

Use with OpenCode​

# Method 1: Start server in tmux (recommended)
tmux new -s opencode -d 'opencode serve --port 4096 --hostname 0.0.0.0'

# Check server status
tmux capture-pane -t opencode -p

# Attach to view
tmux attach -t opencode

# Detach session
Ctrl+b d