Skip to content

No sound in wsl2  #4205

Closed
Closed

Description

@elswerky

i was following a tutorial to enable sound in wsl and was working

https://token2shell.com/howto/x410/enabling-sound-in-wsl-ubuntu-let-it-sing/

but this tutorial no longer works on wsl2

can you plz help me to make it work on wsl2 ??

Activity

  1. Gabrielcarvfer commented on Jun 20, 2019

    @Gabrielcarvfer

    Did you change the IP used in the tutorial? Try 0.0.0.0 instead of 127.0.0.1. The server will listen to any IPs instead of being restricted to the localhost. Should work just fine, but I wouldn't recommend using it in public networks.

    Edit: I've forgotten that you will need a script to get the Windows IP (running the pulseaudio server) and export it to the WSL environment so that the programs can find it. (e.g. powershell -Command "$ipv4 = Test-Connection -ComputerName (hostname) -Count 1 | Select -ExpandProperty IPV4Address; $ipv4.IPAddressToString")

  2. elswerky commented on Jun 21, 2019

    @elswerky
    Author

    Edit: I've forgotten that you will need a script to get the Windows IP (running the pulseaudio server) and export it to the WSL environment so that the programs can find it. (e.g. powershell -Command "$ipv4 = Test-Connection -ComputerName (hostname) -Count 1 | Select -ExpandProperty IPV4Address; $ipv4.IPAddressToString")

    i pasted this " $ipv4 = Test-Connection -ComputerName (hostname) -Count 1 | Select -ExpandProperty IPV4Address; $ipv4.IPAddressToString " in PowerShell and it gave me this 192.168.1.4
    how to export it to wsl
    for example
    export PULSE_SERVER=tcp:192.168.1.4 or what

    can you explain this plz ??

    also i was using this bat file to launch xfce4 desktop on arch wsl on x410 with pusleaudio enabled from a shortcut to this bat file on wsl1
    if you can tell me what to just change in this file to make pusle audio works
    in this screenshot i just changed DISPLAY from localhost to what in screenshot to make it take the new ip address in wsl2
    i want to do the same for pulse server to make it work on wsl2

    can you
    1

  3. Gabrielcarvfer commented on Jun 21, 2019

    @Gabrielcarvfer

    Change your bat file to include the following before of the command starting with arch.exe. It will create a local variable (MYIP) with your Windows IP. After that, you can call %MYIP% where you would have a server IP (e.g. export PULSE_SERVER=tcp:%MYIP%).

    @echo off FOR /F "delims=\" %%A IN ('powershell -Command "$ipv4 = Test-Connection -ComputerName (hostname) -Count 1 | Select -ExpandProperty IPV4Address; $ipv4.IPAddressToString"') DO SET "MYIP=%%A" ::echo %MYIP%

    P.S.: This is a workaround and if your computer IP changes during your use (e.g. connecting to a different WiFi network), you will probably need to stop the services and then run this script again to update the IP and restart the services.

  4. bigworld12 commented on Sep 4, 2019

    @bigworld12

    i am also facing the same issue, i even tried using ALSA to host an audio server, but it looks like it's not supported ?
    all the old guides apparently don't work on wsl 2

  5. JackTheMico commented on Nov 19, 2019

    @JackTheMico

    I think I have figure out one or two solutions for this issue.

    1. Check out your wsl's ip with this command for ubuntu:
      ifconfig eth0 | grep 'inet '
      you will get some thing like "inet 172.24.201.27 netmask 255.255.240.0 broadcast 172.24.207.255",
      and that ip after "inet" is your wsl' ip.
      Add it to pulseaudio /etc/pulse/default.pa line 61 auth-ip-acl and run pulseaudio.exe again, it should work now.

    2. According to this: archlinux-pulseaudio, you can add "auth-anonymous=1" to /etc/pulse/default.pa line 61.
      After you done that, restart pulseaudio.exe and wsl2 can use audio correctly.

    I tried both solutions out with cmus in wsl2, it works for me with ArchLinux.

  6. raekuul commented on Feb 13, 2020

    @raekuul

    Jack Deng (@JackTheMico) Is that a client-side change (inside WSL2) or a server-side one (inside Windows)?

    EDIT: Or am I thinking of this backwards and WSL is the server and Windows pa has to be the client?

  7. hemanta212 commented on Mar 21, 2020

    @hemanta212

    raekuul All changes are in windows side.

  8. k0pernicus commented on May 5, 2020

    @k0pernicus

    Hi all - I am facing the same issue described here, and I tried the solution from Jack Deng (@JackTheMico) but it did not worked as expected...

    So, to explain what I did:

    1. I setted up pulseaudio on Windows10 to match with the IP of my WSL2 image instance (let's call it $MY_IP) in /etc/pulse/default.pa (line 61) (+ other config in daemon.conf), and run it on Windows10.
    2. I also added "auth-anonymous=1" to /etc/pulse/default.pa, line 61.
    3. I installed pulseaudio on my Ubuntu 20.04 in WSL2, and exported the PULSE_SERVER to tcp:$MY_IP.

    Do I need to run the daemon on Ubuntu, or nothing more than what I already did?

    For now, using X410 as X11 server, I expected to have sound when launching Firefox (or whatever), but nothing more than the video :/

    I tried cmus as well, but I have the following error when playing a sound:

    Error: opening audio device: No such device
    
  9. k0pernicus commented on May 5, 2020

    @k0pernicus

    Ok, solved!

    So, I exported this environment variable in my Ubuntu:

    export PULSE_SERVER=tcp:$(grep nameserver /etc/resolv.conf | awk '{print $2}')
    

    and the result of the command ifconfig eth0 | grep 'inet ' in /etc/pulse/default.pa (line 61).

    I have now sound and video launching Firefox from Ubuntu, on Windows now 👍

  10. Adriankhl commented on May 16, 2020

    @Adriankhl

    I recommend running a script like this to configure and run PulseAudio .

    wsl_ip=$(ip a | grep "inet " | grep "eth0" | awk '{print $2}')  
    cygwin.exe -c "pactl load-module module-native-protocol-tcp auth-ip-acl=$wsl_ip"
    

    I installed PulseAudio with cygwin but I believe something similar should work with other installation methods.

  11. nkrapivin commented on May 23, 2020

    @nkrapivin

    I was able to install PulseAudio in WSL2 too, but the sound is very choppy.

    Any way to fix this?

  12. devcircus commented on May 24, 2020

    @devcircus

    There are many ways to be sure the wsl ip is set correctly each time it changes, but what I ended up doing for now, is creating shell script(setup-pulseaudio.sh) in /usr/profile.d that runs a script I made in /usr/bin named setup-pulseaudio like below:

    #!/bin/bash
    
    PULSE_FILE="/mnt/c/location/to/pulseaudio/etc/pulse/default.pa"
    PULSE_IP=$(ip a | grep "inet " | grep "eth0" | awk '{print $2}')
    REPLACE="load-module module-native-protocol-tcp auth-ip-acl=$PULSE_IP auth-anonymous=1"
    
    if test -f "$PULSE_FILE"; then
        sed -i "61s|.*|$REPLACE|" $PULSE_FILE
    fi

    The script will run each time your shell is started and it simply replaces line 61 of default.pa with the line assigned to REPLACE, which contains the current PULSE_ID.

    If you wanted, you could also check to see if pulseaudio.exe is running, kill it, delete any existing runtime pid file at C:\Users\<username>\.pulse, and start pulseaudio.exe. In addition, you could make sure your x-server is running. Personally, my x-server is always running and I don't mind starting pulseaudio manually since I don't use it very often.

  13. juancopi81 commented on Jun 3, 2020

    @juancopi81

    Ok, solved!

    So, I exported this environment variable in my Ubuntu:

    export PULSE_SERVER=tcp:$(grep nameserver /etc/resolv.conf | awk '{print $2}')
    

    and the result of the command ifconfig eth0 | grep 'inet ' in /etc/pulse/default.pa (line 61).

    I have now sound and video launching Firefox from Ubuntu, on Windows now 👍

    Hi k0pernicus,

    I am having problems with this. I already tried:

    export PULSE_SERVER=tcp:$(grep nameserver /etc/resolv.conf | awk '{print $2}')
    

    and write the result of ifconfig eth0 | grep 'inet ' in /etc/pulse/default.pa (line 61). But when I try: '.\pulseaudio.exe' I get: .pulseaudio.exe: command not found

    I do not know what I am missing. Any help would really appreciated .
    2020-06-02_2226

  14. Adriankhl commented on Jun 3, 2020

    @Adriankhl

    JuanCarlosPi (@juancopi81) do ./pulseaudio.exe instead

  15. juancopi81 commented on Jun 3, 2020

    @juancopi81

    Thanks Adriankhl for your answer... I was not understanding so actually I was doing the first steps inside Ubuntu (WSL 2) and not the powershell of windows. Now it is working, but sound it is really bad, any way to improve that?

  16. JDawgTechie commented on Jun 19, 2020

    @JDawgTechie

    Many thanks to JackTheMico. Could not get audio working with WSL2. Was thinking my Windows host IP had to be in that config file. Working good now!

  17. eamondo2 commented on Oct 9, 2020

    @eamondo2

    So I had attempted to get some of the suggestions from this thread, as well as from others I'd found while doing research and wasn't able to get them working reliably.

    Git Page

    This is an attempt to collate what ended up working, hopefully it will be of some help.

  18. timriker commented on Oct 21, 2020

    @timriker

    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
    
  19. JDawgTechie commented on Oct 21, 2020

    @JDawgTechie

    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
    

    Excellent suggestions, thank you! Although I couldn't get anything to work right by editing bashrc so I just updated my bat file instead:

    ubuntu2004.exe run "if [ -z \"$(pidof xfce4-session)\" ]; then export DISPLAY=$(ip route|awk '/^default/{print $3}'):0.0; export PULSE_SERVER=${PULSE_SERVER:-tcp:$(ip route|awk '/^default/{print $3}')}; startxfce4; pkill '(gpg|ssh)-agent'; taskkill.exe /IM pulseaudio.exe /F; taskkill.exe /IM vcxsrv.exe; fi;"

    Woot! No more having to change the IPs.

  20. timriker commented on Oct 22, 2020

    @timriker

    See my notes in #5816 which works for WSL1 as well.

  21. dcharlespyle commented on Feb 22, 2021

    @dcharlespyle

    If none of the above options works, as has happened to me since changing to WSL 2, you can use the following in your ~/.bashrc file to correct for deficiencies:

    # 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 DISPLAY=$HOST_IP:0.0
    export PULSE_SERVER=tcp:$HOST_IP
    

    It grabs the IP address of the Windows host, no matter what it is, and exports it to the WSL 2 environment.

  22. dcharlespyle commented on May 14, 2021

    @dcharlespyle

    Now using the built-in audio of WSLg. Works very well! No more need to redirect PULSE_SERVER to specific IP address. Just works out of the box in WSL2 in Windows 10 Preview builds higher than 21364. What doesn't work is theming for QT and GTK2 apps. Cannot launch elevated GUI programs anymore. These will require adjusting command lines and some experimentation. But sound works great! Have full GPU acceleration, too! Transparency in Gnome-Terminal functions as intended.

    image

    And did I say that sound works really well for a preview?

  23. FirefoxSystem commented on Sep 6, 2021

    @FirefoxSystem

    Hello guys,

    after days of unsuccessful attempts I solved the no sound problem by inserting in the file /etc/pulse/client.conf the following line:

    default-server = 192.168.1.2

    obviously the IPv4 LAN address of your computer not that of the virtual card 'vEthernet (WSL)'

    Happy listening :)
    Hello

  24. singhdilveer1 commented on Dec 12, 2022

    @singhdilveer1

    I came here for a solution but installing pulseaudio on the ubuntu instance solved the issue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions