How to Use Linux Tools in a Windows Environment

Fragmented memories that are hard to recall

Q&A

How to Use Linux Tools in a Windows Environment

Question

How to use Linux tools in a Windows environment

Answer

If you are a developer on Windows, you must have experienced the pain of being in a “dilemma”: needing Linux tools but being locked into the Windows ecosystem. πŸ–₯️

Of course, you can use a virtual machine or dual-boot system, but you will encounter various troubles: slow speed, poor experience, and many issues, often making it not worth the effort. 😀

At this point, WSL (Windows Subsystem for Linux) comes into play. Imagine running a complete Linux environment natively on your Windows machineβ€”no virtual machine, no extra configuration, just the full functionality of Linux without leaving your current work environment, and it’s a comprehensive solution. With WSL, you can enjoy the best of both Windows and Linux: flexible, powerful, and efficient.

What is WSL? πŸ€”

How to Use Linux Tools in a Windows Environment

Windows Subsystem for Linux (WSL) is a revolutionary feature of Windows that allows you to run a complete Linux environment directly on your Windows machine without a separate virtual machine or dual-boot system. 🐧✨ Whether you are developing software, testing servers, or need to use Linux command-line tools, WSL can greatly simplify your workflow. The best part is that you can have the powerful features of both Windows and Linux, all integrated into one system. πŸš€

WSL 1 is implemented through a compatibility layer that converts Linux system calls into Windows system calls. While it works, it feels a bit like a “black technology” solution, as it cannot run a complete Linux kernel, thus having certain limitations in performance and some specific Linux features.

WSL 2 is a groundbreaking upgrade. Microsoft introduced WSL 2 by bringing in a real Linux kernel (through virtualization technology), enhancing performance, system compatibility, and providing a more authentic Linux experience. πŸ”₯

WSL 2 runs a lightweight virtual machine (VM), with each Linux distribution running as an isolated container. The key difference is that each Linux distribution has its own independent environment, making it more secure and efficient than WSL 1.

Distributions in WSL 2 share some content (such as network namespaces, device trees, and memory), but each distribution has its own independent PID namespace, user namespace, mount namespace, and init process. This design brings higher flexibility, performance, and isolation, reducing conflicts and improving stability. πŸ› οΈ

  • Comparison of WSL 1 and WSL 2: A brief comparison of the differences in performance, kernel usage, and file system handling between the two, with WSL 2 being the clear superior choice. ⚑

How to Use Linux Tools in a Windows Environment

  • WSL Architecture Diagram: Demonstrates how WSL 2 uses a lightweight VM to run Linux containers and explains how each component interacts, helping to understand the magic behind it. πŸ§‘πŸ’»πŸ—ΊοΈ

How to Use Linux Tools in a Windows Environment

How to Install WSL πŸ”§πŸš€

How to Use Linux Tools in a Windows Environment

Installing WSL is very simple. Just follow the steps below, and you will be set up in no time. πŸ–₯️✨

Prerequisites πŸ”‘

  • Windows 10 version 2004 or higher (Build 19041+) or Windows 11 πŸͺŸ

Installation Steps πŸ› οΈ

Open PowerShell or Command Prompt with administrator privileges:

  • Right-click the “Start” button and select “Windows PowerShell (Admin)” or “Command Prompt (Admin)”.

  • You must run as administrator to install WSL. πŸ’Ό

Install WSL:

  • In PowerShell / Command Prompt, enter the following command: wsl –install

How to Use Linux Tools in a Windows Environment

  • By default, the Ubuntu distribution will be installed and set as WSL 2 (recommended). πŸŽ‰

Custom Installation (Optional):

  • Want to use a different Linux distribution? You can use the following command: wsl –install -d <Distribution Name>

  • Replace < Distribution Name > with your preferred Linux distribution, such as Debian, Kali, or Fedora.

  • To see the list of available Linux distributions, enter:wsl –list –online or wsl -l -o πŸ“œ

How to Use Linux Tools in a Windows Environment

Install from Microsoft Store (Optional):

  • You can also install Linux distributions directly from the Microsoft Store.

  • First, ensure that WSL is enabled in Windows Features settings:

    1) Open the “Start” menu, search for “Windows Features”, and check “Windows Subsystem for Linux”.

How to Use Linux Tools in a Windows Environment

2) Then, search for and install your preferred Linux distribution in the store. πŸ“¦

How to Use Linux Tools in a Windows Environment

Restart your computer:

After installation is complete, restart your computer to apply the changes. πŸ”„

Verify WSL Installation:

  • Open PowerShell or Command Prompt and enter:wsl -l -v to see the installed distributions and their versions. βœ”οΈ

How to Use Linux Tools in a Windows Environment

  • Set default version: wsl –set-default-version 2 You can switch to WSL 1, but WSL 2 is faster and more efficient. ⚑

Upgrading from WSL 1 to WSL 2:

  • If you previously installed WSL 1, upgrading to WSL 2 is also simple. Run:

    wsl –set-version <Distro Name> 2 Replace < Distro Name > with your Linux distribution name (e.g., Ubuntu). πŸ”§

Post-Installation Steps πŸš€

How to Use Linux Tools in a Windows Environment

Launch the Linux Distribution:

  • Open the “Start” menu, search for the Linux distribution you just installed (like Ubuntu), and launch it. πŸ”

How to Use Linux Tools in a Windows Environment

Set Username and Password:

  • On the first launch, you will be prompted to set a username and password for the WSL user. This will be your login credentials in the Linux environment. πŸ—οΈ

How to Use Linux Tools in a Windows Environment

Start Exploring Your Linux Environment! πŸ–₯οΈπŸ”

How to Use Linux Tools in a Windows Environment

Once WSL is set up, you can directly enter the Linux command line, try using Linux commands, install packages, or run simple scripts, and enjoy the development experience! πŸš€

How to Use Linux Tools in a Windows EnvironmentHow to Use Linux Tools in a Windows Environment

Connecting Spring Boot to Containerized Services in WSL 🐳

If you want to run a Spring Boot project on Windows while running MySQL, RabbitMQ, and Redis services in WSL through Docker, you can absolutely do it! No need for Docker Desktop, just WSL and Docker, running smoothly and stably. πŸš€

  • Step 1: Run Docker in WSL πŸ–₯️

    Before connecting anything, let’s make sure Docker is up and running in WSL.

    1) Install Docker: Open the WSL terminal (like Ubuntu) and run the following command to install Docker:

How to Use Linux Tools in a Windows Environment

2) Start Docker:

How to Use Linux Tools in a Windows Environment

3) Verify if it started successfully

How to Use Linux Tools in a Windows Environment

Now Docker has started successfully! πŸŽ‰

  • Step 2: Set Up Services in Docker πŸ‹

Use docker-compose to start MySQL, RabbitMQ, and Redis:

1) Create a docker-compose.yml file:

Example configuration:

How to Use Linux Tools in a Windows Environment

2) Run the containers:

How to Use Linux Tools in a Windows Environment

3) Verify

How to Use Linux Tools in a Windows EnvironmentHow to Use Linux Tools in a Windows Environment

Connecting Spring Boot to Docker Services 🌐

  • Configure in application.properties:

How to Use Linux Tools in a Windows Environment

  • Start the Spring Boot project

    In this example, I use JPA and Hibernate to automatically generate database tables based on the properties in the entity classes.JPA and Hibernate will automatically create the necessary tables in MySQL based on the entity classes in your Spring Boot project.

How to Use Linux Tools in a Windows Environment

Access RabbitMQ Management Interface and Redis Insight 🌐

Once your Spring Boot application is running and the related services are started in WSL , you can access the management interfaces of RabbitMQ and RedisInsight through a Windows browser.

RabbitMQ Management Interface:

How to Use Linux Tools in a Windows EnvironmentHow to Use Linux Tools in a Windows Environment

Log in with the default username and password:

  • Username: guest

  • Password: guest

Redis Insight:

Similarly, access Redis Insight

How to Use Linux Tools in a Windows EnvironmentHow to Use Linux Tools in a Windows Environment

Using IntelliJ IDEA with Projects in WSL 🌐

For many developers, handling both backend and frontend technologies is the norm. In this example, you will use Java (Spring Boot) for the backend and React for the frontend.

With WSL (Windows Subsystem for Linux), you can easily set up both development environments without frequently switching operating systems.

Next, we will explore how to use IntelliJ IDEA (for Java development) and VS Code (for React/JS development) together with WSL to develop and collaborate on backend and frontend projects in a unified and consistent development environment.

Developing Java (Backend) in IntelliJ IDEA πŸ–₯️

If your backend is developed based on Spring Boot (Java), then IntelliJ IDEA is likely your IDE of choice.

IntelliJ IDEA works seamlessly with WSL, allowing you to run backend Java applications directly in a Linux-based environment while working on your Windows machine.

How to Achieve This via WSL:

  • Install IntelliJ IDEA:

    If you haven’t installed it yet, you can download and install IntelliJ IDEA from the JetBrains website.

  • Open the Project in IDEA:

    Launch IDEA

    In the Welcome Screen, click Open, then navigate to the Spring Boot project directory located in the WSL file system.

How to Use Linux Tools in a Windows Environment

  • Configure Java SDK:

    Ensure that IntelliJ IDEA is configured with the correct JDK in the WSL environment.

    Do the following:

    Β· Go to File > Project Structure.

    Β· On the SDKs page, confirm that the Java SDK version matches that of WSL.

How to Use Linux Tools in a Windows Environment

  • Run the Spring Boot Project

How to Use Linux Tools in a Windows Environment

Running VS Code Projects in WSL πŸ’»

How to Use Linux Tools in a Windows Environment

In this section, we will explore how to use VS Code in WSL to develop JavaScript and React projects while maintaining consistency and efficiency in the development environment.

Configuring VS Code for JavaScript (React) Projects in WSL πŸ–₯️

If your frontend is built using React (JavaScript), VS Code is an excellent choice for developing and managing React applications. With WSL, you can create a smooth and efficient development experience in a Linux-based environment, even if you are using an IDE on Windows.

How to open and develop React projects in VS Code via WSL:

  • Install VS Code:

    Download and install VS Code from the VS Code website.

  • Install the Remote – WSL Extension:

    RunVS Code.

    Open the Extensions Marketplace and search for “Remote – WSL

    Install the Remote – WSL extension. This plugin allowsVS Code to open files directly from the WSL environment.

  • Open Your React Project in VS Code via WSL:

    Press Ctrl + Shift + P in VS Code to open the command palette.

    Type and select Remote-WSL: Connect to WSL to open a new window directly in WSL.

How to Use Linux Tools in a Windows Environment

  • Now, open the React project located in the WSL file system

How to Use Linux Tools in a Windows Environment

  • Run npm install and npm run dev

How to Use Linux Tools in a Windows Environment

  • Open the browser via the pop-up window or access it using the port, such as localhost:3000

How to Use Linux Tools in a Windows EnvironmentHow to Use Linux Tools in a Windows Environment

END

How to Use Linux Tools in a Windows EnvironmentHow to Use Linux Tools in a Windows Environment

Author: Fatih Khoiri

Reviewer: Shan Zhongdi

Editor: Zhu Sicong

Leave a Comment