Skip to content

Instantly share code, notes, and snippets.

@thecraftman
Last active October 19, 2023 22:46
Show Gist options
  • Save thecraftman/de3f4e0d62a4a2b9d1289c74d084a693 to your computer and use it in GitHub Desktop.
Save thecraftman/de3f4e0d62a4a2b9d1289c74d084a693 to your computer and use it in GitHub Desktop.
Windows Subsystem for Linux

Windows Subsystem for Linux 2

Getting Started

Using WSL2 with Visual Studio Code

  • Steps
    1. Install the Remote development Extension pack on VS Code
    2. Once you install the extension, you will see a Remote Development extension icon at the bottom left corner of the VS Code editor.Click on the icon, you will get a pop up with a list of options. Click on the first option "Remote-WSL: New Window" for the default distro or select the "Remote-WSL: New Window using Distro" for a specific distro.
    3. Once installed, the VS Code of your Windows machine/desktop will communicate with VS Code server on the Linux side.

Troubleshooting

Troubleshooting docker-compose

  • Steps taken to fix WSL
    1. Install latest Windows updates
    2. Install WSL2 following the docs
  • Steps taken to use docker-compose in WSL
    1. Uninstalled and reinstalled docker desktop on windows 10
    2. Enable the WSL2 based engine in the settings on docker desktop.
    3. Enable integration with additional distributions under Resources on docker desktop.

Troubleshooting npm

Based on Fiqri Ismail's Medium article:

  1. Use the nodesource distribution installer:

    curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

    Find other versions in @nodesource/distributions/deb/

  2. Use apt-get to install nodejs:

    sudo apt-get install -y nodejs
  3. Verify that node is working:

    node --version

Troubleshooting PATH

If you hit this:

bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory

First try this:

If you find your pwd starts with /mnt/c/… and you should cd ~/ which will take you to your home folder inside WSL which you want to be using instead when hacking on code.

Then try this:

You are likely hitting a problem with importing your Windows PATH environment variable into WSL2.

See microsoft/WSL#1890.

Important Notice: Refer to @microsoft/WSL #1890 before making the following changes.

  1. Open ~/.bashrc in your editor:

    code ~/.bashrc
    
  2. Append the following line in the end:

    PATH=$(echo "$PATH" | sed -e 's/:\/mnt.*//g') # strip out problematic Windows %PATH%
  3. If you still run into the problem with some commands you might wan to consider this approach:

    ### remove unnecessary Win PATHs
    # This can prevent extension-less commands from bleeding into BASH.
    # (eg. "ng" would execute the Win bin if "@angular/cli" wasn't installed on Linux.)
    #
    function path_remove {
      # Delete path by parts so we can never accidentally remove sub paths
      PATH=${PATH//":$1:"/":"} # delete any instances in the middle
      PATH=${PATH/#"$1:"/} # delete any instance at the beginning
      PATH=${PATH/%":$1"/} # delete any instance in the at the end
    }
    
    path_remove '/mnt/c/Users/me/AppData/Roaming/npm'
    path_remove '/mnt/c/Users/me/AppData/Local/Yarn/bin'
    path_remove '/mnt/c/Program Files (x86)/Yarn/bin'
    path_remove '/mnt/c/Program Files/Git'
    path_remove '/mnt/c/Program Files/Git/cmd'
    path_remove '/mnt/c/Program Files/nodejs'
    path_remove '/mnt/c/OpenSSL-Win32/bin'
    path_remove '/mnt/c/Program Files (x86)/Python27'

    See this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment