Enabling Linux Services on WSL

Introduction

To run services like systemd on WSL, you need to ensure that your WSL environment is correctly configured. This guide provides step-by-step instructions for enabling Linux services on WSL.


Prerequisites

  • Windows 10 (version 2004 and higher) or Windows 11
  • WSL 2 installed and set as the default version
  • Administrative privileges to update WSL and edit configuration files

Step 1: Update WSL

Ensure your WSL installation is up to date to enable full systemd support.

  1. Open Command Prompt or PowerShell as Administrator.
  2. Run the following command to update WSL:
    wsl --update

Step 2: Verify WSL Version

Ensure that your Linux distribution is using WSL 2, as systemd support is only available in WSL 2.

  1. Check the version of your WSL distributions:

    wsl --list --verbose
  2. If your distribution is not using WSL 2, upgrade it:

    wsl --set-version <DistroName> 2

    Replace <DistroName> with the name of your Linux distribution (e.g., Ubuntu).


Step 3: Enable Systemd

Enable systemd to manage Linux services within WSL.

  1. Open your Linux distribution (e.g., Ubuntu) in WSL.

  2. Edit the WSL configuration file:

    sudo nano /etc/wsl.conf
  3. Add the following lines:

    [boot]
    systemd=true
  4. Save and close the file (Ctrl+O, Enter, Ctrl+X).

  5. Restart WSL to apply the changes:

    wsl --shutdown

    Then reopen your Linux distribution.


Step 4: Verify Systemd is Enabled

Confirm that systemd is active in your WSL distribution.

  1. Run the following command to check the status of systemd:
    systemctl --version

    If the command outputs the systemd version, it is successfully enabled.


Troubleshooting

Error: systemctl: command not found

  • Ensure you have edited the /etc/wsl.conf file correctly and added the [boot] section with systemd=true.

  • Restart WSL with:

    wsl --shutdown