Skip to content

Instantly share code, notes, and snippets.

@huycozy
Last active October 7, 2024 09:28
Show Gist options
  • Save huycozy/c2b7a355a4ba0fd5cfce143d93b2a768 to your computer and use it in GitHub Desktop.
Save huycozy/c2b7a355a4ba0fd5cfce143d93b2a768 to your computer and use it in GitHub Desktop.
Guideline on how to build and run Linux app on WSL2 with VS Code on Windows
  1. Make sure you installed VS Code here: https://code.visualstudio.com/
  2. Open VS Code, find WSL from Extensions and install it
  3. Click on Open a remote windows:

image

  1. Click on Connect to WSL using Distro... By default, there is only Ubuntu (wsl1), you can use it if you want. But if you want to use WSL2, need to install another distro

4.1. List all available distros:

C:\WINDOWS\system32>wsl -l --online
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_7_9                        Oracle Linux 7.9
OracleLinux_8_7                        Oracle Linux 8.7
OracleLinux_9_1                        Oracle Linux 9.1
openSUSE-Leap-15.5                     openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5           SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed                    openSUSE Tumbleweed

4.2. Install the newer Ubuntu: Ubuntu-22.04

wsl --install -d Ubuntu-22.04

4.3. Check installed distros on your system

C:\WINDOWS\system32>wsl -l --all
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
Ubuntu-22.04

Once you have new distro version, go back VS Code and select Ubuntu-22.04. After logged in, you will see:

image

  1. Open Terminal from VS Code and interact with Ubuntu distro as usual (assume you are familier with Linux)
  2. Install Flutter for Ubuntu: https://docs.flutter.dev/get-started/install/linux
  3. Run Linux app
flutter create demo_flutter
cd demo_flutter
flutter run -d linux

image

Troubleshoting

You may hit error: /usr/bin/env: ‘bash\r’: no such file or directory when executing flutter CLI, to solve this:

sudo apt-get install dos2unix
find . -type f -exec dos2unix {} \;

Then restart WSL2 with wsl --shutdown Done.

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