DEV Community

Lucas Luan de Melo
Lucas Luan de Melo

Posted on

How to Set Up Solargraph in VS Code with WSL2

Introduction

Recently, I faced an issue while trying to set up Solargraph in VS Code using WSL2 and ASDF for managing Ruby versions. The legacy projects I was working on used Docker, causing conflicts with Ruby versions and resulting in errors when initializing the server. After much research and trial and error, I managed to solve the problem. Here is a step-by-step guide to help other developers who might be facing the same issue.

Disclaimer

The easiest way to resolve Ruby version issues would be to modify the project's .tool-versions file to use a Ruby version compatible with Solargraph. However, in legacy environments, this modification may not be possible due to specific project dependencies or restrictions imposed by the development team. Therefore, this guide provides an alternative solution that does not require changes to the .tool-versions file.

Step-by-Step Solution

Prerequisites

  1. WSL2 installed: Follow the official Microsoft instructions to set up WSL2.
  2. VS Code installed: Download and install VS Code.
  3. Linux distribution on WSL2: For example, Ubuntu.

Step 1: Set Up Ruby on WSL2 with ASDF

  1. Install ASDF: In the WSL2 terminal, run the following commands:

    git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.0
    echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
    echo '. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc
    source ~/.bashrc

  2. Add the Ruby plugin and install a Ruby version:

    asdf plugin-add ruby https://github.com/asdf-vm/asdf-ruby.git
    asdf install ruby 3.3.2 # Replace with the necessary version
    asdf global ruby 3.3.2

  3. Verify the Ruby installation:

    ruby -v

Step 2: Install Solargraph

  1. Install Solargraph:

    gem install solargraph

  2. Verify the Solargraph installation:

    which solargraph

Step 3: Configure VS Code to Use WSL2

  1. Install the "Remote - WSL" Extension: In VS Code, open the Extensions Marketplace and install the Remote - WSL extension.

  2. Open VS Code in WSL2: In the WSL2 terminal, navigate to your project directory and open VS Code:

    code .

Step 4: Configure Solargraph in VS Code

  1. Add the configuration in settings.json: In VS Code opened in the WSL2 environment, open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P), search for "Preferences: Open Settings (JSON)" and add the following configuration:

    {
        "solargraph.commandPath": "/home/your_user/.asdf/shims/solargraph",
        "solargraph.useBundler": false,
        "solargraph.diagnostics": true,
        "solargraph.formatting": true
    } 
    

    Replace /home/your_user/.asdf/shims/solargraph with the path returned by the which solargraph command.

  2. Restart VS Code: Restart VS Code to apply the new settings.

Troubleshooting

  • Check permissions and paths: Ensure that the path specified for Solargraph in settings.json is correct and accessible.

  • VS Code Logs: Check the VS Code logs for more details on the error. Access the logs through the Command Palette (Ctrl+Shift+P -> "Output") and select "Solargraph" from the output menu.

Conclusion

With these steps, you should be able to set up Solargraph in VS Code using the Ruby version managed by ASDF in the WSL2 environment, without the need to modify the project's .tool-versions file. I hope this guide helps solve similar issues you might encounter in your Ruby development environment.


I hope this guide has been helpful! If you have any questions or suggestions, leave a comment below. 🚀

Top comments (0)