How to create a shortcut in Windows 10 to launch a WSL2 GUI app in VcXsrv

3 min read 22-10-2024
How to create a shortcut in Windows 10 to launch a WSL2 GUI app in VcXsrv

If you're a developer or a Linux enthusiast using Windows 10 with Windows Subsystem for Linux 2 (WSL2), you might want to run GUI applications from your Linux environment seamlessly. To achieve this, you can use VcXsrv, an X server that allows you to run Linux GUI applications on Windows. This article will guide you through the process of creating a shortcut that allows you to easily launch a WSL2 GUI app through VcXsrv.

Problem Scenario

Imagine you have installed a GUI application on your WSL2 environment and every time you want to run it, you have to manually start VcXsrv and input command lines in the terminal. This can be tedious and time-consuming. Thus, the goal is to simplify this process by creating a desktop shortcut.

Original Code for the Problem

Below is the original command to run a GUI app in WSL2 with VcXsrv:

DISPLAY=:0 your-gui-app

This command can be cumbersome to execute repeatedly. Let’s turn it into a user-friendly shortcut.

Steps to Create a Shortcut

Step 1: Install VcXsrv

If you haven't already, download and install VcXsrv from the official VcXsrv website. During installation, make sure to configure it to allow access from your localhost.

Step 2: Prepare Your WSL2 Environment

  1. Open your WSL2 terminal (for example, Ubuntu).

  2. Export the display variable by running the following command:

    export DISPLAY=$(ip route | awk '/default/ {print $3}'):0
    
  3. You may want to add this command to your .bashrc or .bash_profile file to ensure it's set up each time you open a terminal.

Step 3: Create a Shortcut

  1. Right-click on your Desktop and select New > Shortcut.

  2. In the location field, enter the following command (make sure to modify it according to your WSL installation path):

    C:\Windows\System32\wsl.exe -e bash -c "export DISPLAY=$(ip route | awk '/default/ {print $3}'):0 && your-gui-app"
    

    Replace your-gui-app with the actual command to run your application.

  3. Click Next and name your shortcut (e.g., "Launch My GUI App").

  4. Click Finish.

Step 4: Customize Your Shortcut (Optional)

  • Change Icon: Right-click on the new shortcut, go to Properties, and click on Change Icon. You can choose an icon that represents your application.
  • Run as Administrator: In the Properties dialog, check the box for Run this program as an administrator if necessary.

Testing the Shortcut

Double-click on your newly created shortcut. It should launch VcXsrv and then start your GUI application in WSL2 without having to open the terminal manually.

Additional Tips

  • VcXsrv Configuration: Make sure VcXsrv is configured to allow public access, or else your GUI app might not render correctly.
  • Automate Starting VcXsrv: If you want to automate launching VcXsrv along with your application, you may need to create a batch file that first starts VcXsrv and then runs your WSL command.
  • Firewall Settings: Sometimes, the Windows firewall might block connections. Ensure that VcXsrv is allowed through the firewall.

Conclusion

Creating a shortcut to launch a WSL2 GUI app through VcXsrv is a straightforward process that can save you time and enhance your workflow. By following these steps, you’ll simplify how you interact with Linux applications on your Windows environment. If you’re looking to enhance your development experience, consider exploring other GUI applications available in your WSL2 setup.

Useful Resources

By following this guide, you now have a useful tool to enhance your productivity while working with WSL2 on Windows 10. Happy coding!