Getting sound to work on WSL2

I’ve been trying to get sound to work. It’s challenging for the same reasons listed in the graphical case (no localhost/127.0.0.1 available)

I got it working following the wiki steps but with some tweaks

the ‘auth-ip-acl=127.0.0.1’ parameter needs to use the IP from the WSL instance though (‘ip a’ on the WSL command line to get it)
(unfortunately the WSL IP change after a reboot so the config needs to be re-edited…)

  • start bin/pulseaudio

  • Disable Windows Defender Firewall for Guest or public Network (a better way would be to add firewall rules to allow only what is needed but I didn’t figure out how to do that yet, help welcome if anyone knows?)

  • in WSL

    • install libpulse0 if you don’t have it yet
    • export the environmenet
$ export PULSE_SERVER=tcp:$(grep nameserver /etc/resolv.conf | awk '{print $2}');

Sound should be working then!

2 Likes

Re the ‘auth-ip-acl=127.0.0.1’ parameter needs to use the IP from the WSL instance though.

I have edited my default.pa file to include acl auth-ip-acl=172.16.0.0/12 because WSL2 seems to choose any random address from this range. The only issue with this is that you may get other machines behind your router trying to send pulseaudio sound packets to your computer???

1 Like

I can’t make it work, been trying for 2 days. I have tried everything but no sound.
Windows ver. 2004 (build 19041.84), WSL2.

For me work the configurations as described above, but with a change:
in pulseaudio configuration in the file default.pa the line is
load-module module-native-protocol-tcp auth-anonymous=1

works fine on windows 10 build 2004 / wsl2 / ubuntu 20.4

I went through a whole lot of attempts to get this to work reliably, I ended up collating my best effort at a relatively painless setup.

Git Page

Hope this helps someone else, I’m working on eventually getting it mostly automated through bash scripting.

I’ve set in etc/pulse/default.pa this:

load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;172.16.0.0/12

This will authorize all local connections for WSL1 and the RFC-1918 block 172.16.0.0/12 which is the default space for WSL2.

I would not recommend auth-anonymous=1 as this would allow any remote machine to connect. I use this inside my .bashrc:

if grep -q "microsoft" /proc/version &>/dev/null; then
  # WSL2
  export DISPLAY="$(ip route|awk '/^default/{print $3}'):0.0"
  export PULSE_SERVER="${PULSE_SERVER:-tcp:$(ip route|awk '/^default/{print $3}')}"
fi

Changes in WSL2 made my configuration stop working, too. I had to add the following to my ~/.bashrc file to make it work again:

# Get the IP Address of the Windows 10 Host and use it in Environment.
HOST_IP=$(host `hostname` | grep -oP '(\s)\d+(\.\d+){3}' | tail -1 | awk '{ print $NF }' | tr -d '\r')
export LIBGL_ALWAYS_INDIRECT=1
export DISPLAY=$HOST_IP:0.0
export NO_AT_BRIDGE=1
export PULSE_SERVER=tcp:$HOST_IP

After doing that, any BASH prompt should enable sound, regardless of your Host IP address or WSL IP address. You’ll use similar in command lines in shortcuts to Linux GUI programs to get sound to work with various programs, as needed. Below is a screenshot of Linux and Windows programs working together, while watching a video on YouTube, using Google-Chrome on Linux in WSL.

It was kind of tricky... well, I only got to make it work out by:
  1. using this specific line in the pulseaudio\etc\pulse\default.pa @ Windows: (
    load-module module-native-protocol-tcp auth-anonymous=1
    )
    I'm a little bit worried about my security, but it's just cool to use Rhythmbox as my music player, so I think I'll give it a try until WSLg is out (commented in the end). Can anyone explain us how dangerous it is?
  2. and by appending these lines to my ~/.bashrc @ Ubuntu: (

    HOST_IP=$(host hostname | grep -oP ‘(\s)\d+(.\d+){3}’ | tail -1 | awk ‘{ print $NF }’ | tr -d ‘\r’)
    export LIBGL_ALWAYS_INDIRECT=1
    export DISPLAY=$HOST_IP:0.0
    export NO_AT_BRIDGE=1
    export PULSE_SERVER=tcp:$HOST_IP

    )

Luckily, next big update for WSL will include full audio I/O and graphic card support, as it's shown here:
  • https://devblogs.microsoft.com/commandline/whats-new-in-the-windows-subsystem-for-linux-september-2020/#gui-apps
  • and here:

  • https://www.youtube.com/watch?v=f8_nvJzuaSU

You can already try it out - the WSLg (WSL GUI), if you are inscribed into the Windows Insider program.

It's really hard to fully leave Windows, some commercial softwares are just too good. As a gamer and graphic softwares user, I got stuck here... My mom, my dad and one of my sisters, who rarely touch bash/terminal are full Ubuntu users... LOL

My WSL2 changed from 172.x.x.x to 192.168.x.x networks. I edited system.pa to contain:

load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;172.16.0.0/12;192.168.0.0/16

so that it includes both rfc1918 networks. This is more secure than allowing anonymous, but could still potentially allow anyone on one of these IPs to play or record audio from your windows host.

I’m using the PulseAudio installer from here:

https://pgaskin.net/pulseaudio-win32/