如何在WSL中通过VS Code扩展配置Ruby on Rails调试器及解释器路径?
Fixing "Extension host terminated unexpectedly" & Configuring Ruby/Rails Debugger in WSL + VS Code
Hey there! I’ve run into this exact setup issue before, so let’s break this down into actionable steps to get your Ruby/Rails debugging working smoothly.
First: Fix the Extension Host Crash
The "Extension host terminated unexpectedly" error usually pops up when VS Code (running on Windows) tries to interact with your WSL Ruby environment without proper remote context. Here’s how to fix it:
- Use VS Code’s WSL Remote Window: Instead of opening your WSL project directly from Windows File Explorer, do this:
- Click the green/blue
<>icon in the bottom-left corner of VS Code. - Select
New WSL Window using Distro...and pick your WSL distro (e.g., Ubuntu). - Once the WSL window loads, use
File > Open Folderto navigate to your Rails project inside WSL (like/home/your-username/your-rails-app).
- Click the green/blue
- Update Your Ruby Extension: Uninstall any outdated Ruby extensions (like the old "Ruby" extension by Peng Lv) and install the official Ruby LSP extension (maintained by Shopify). It’s far more stable for WSL environments and plays nicely with modern Ruby tooling.
Step 1: Set the Ruby Interpreter Path
Now that you’re in the WSL remote window, setting the interpreter path is straightforward:
- Open VS Code settings by pressing
Ctrl+,(Windows/Linux) orCmd+,(Mac). - Search for
Ruby LSP: Ruby Path(if using Ruby LSP) orRuby: Path(for older extensions). - To get your exact Ruby path, run this in your WSL terminal:
This will output something likewhich ruby/home/your-username/.rbenv/shims/ruby(if using rbenv) or/usr/bin/ruby(system Ruby). Paste this path into the setting field.- Pro tip: If you use a version manager like rbenv or rvm, make sure the path points to the shim (not the raw Ruby binary) to ensure it picks up your project’s specific Ruby version.
Step 2: Configure Rails Debugger
To debug Rails apps, we’ll use the debug gem (the modern replacement for older debug tools like ruby-debug-ide):
- Add the debug gem to your
Gemfile:gem 'debug', platforms: [:mri, :mingw, :x64_mingw] - Run
bundle installin your WSL terminal to install it. - Create a debug configuration file:
- In VS Code, go to the Run and Debug tab (Ctrl+Shift+D).
- Click
create a launch.json fileand selectRubyas the environment. - Replace the default content with this Rails-specific config:
{ "version": "0.2.0", "configurations": [ { "name": "Rails Server", "type": "ruby", "request": "launch", "program": "${workspaceRoot}/bin/rails", "args": ["server"], "useBundler": true }, { "name": "Rails Console", "type": "ruby", "request": "launch", "program": "${workspaceRoot}/bin/rails", "args": ["console"], "useBundler": true }, { "name": "Debug Current File", "type": "ruby", "request": "launch", "program": "${file}", "useBundler": true } ] }
- Start debugging:
- Set breakpoints in your Rails code (click the gutter next to line numbers).
- From the Run and Debug tab, select
Rails Serverand click the green play button. - Your Rails server will start in debug mode, and execution will pause at your breakpoints.
Quick Troubleshooting Tips
- If you still get extension crashes, try disabling other extensions temporarily to rule out conflicts.
- Make sure your WSL distro is up to date: run
sudo apt update && sudo apt upgrade(for Debian/Ubuntu-based distros). - If the debugger doesn’t hit breakpoints, double-check that the
debuggem is installed and yourlaunch.jsonhasuseBundler: true.
内容的提问来源于stack exchange,提问作者Ankit Ardeshana
相关产品推荐
相关产品推荐

