Running Elixir Observer on wsl2

June 19, 2019

I’ve been using linux for over 20 years but last this year I needed to install windows to play some games. I also wanted to check if I could use WSL2 to for my work…I must say it’s working better than I expected. It’s actually a serious reason people should give up OSX and switch to Windows if they want to play games and work in a real Linux environment (I must admin… Microsoft did another great tool for developers).


Ok, let’ get to the point.

If you’ve tried to run :observer.start from IEx in WSL2, you’ve probably encountered this error:


iex(1)> :observer.start
09:15:51: Error: Unable to initialize GTK+, is DISPLAY set properly?
                                                                    {:error,
 {{:einval, 'Could not initiate graphics'},
  [
    {:wxe_server, :start, 1, [file: 'wxe_server.erl', line: 65]},
    {:wx, :new, 1, [file: 'wx.erl', line: 115]},
    {:observer_wx, :init, 1, [file: 'observer_wx.erl', line: 107]},
    {:wx_object, :init_it, 6, [file: 'wx_object.erl', line: 372]},
    {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}
  ]}}
iex(2)> :observer.start


It means you don’t have an X Server on Windows. There are projects like VcXSrv which seem to work (based on comments found in the Internet) but until I review and build the code myself I don’t trust compiled applications of unknown authors (I don’t mean there’s something wrong with VcXsrv, it’s just one of my principles I follow). Before WSL2 came to the game, most of windows users used Cygwin, so there’s a big chance you have it installed already. If you didn’t install it, download it from: https://cygwin.com/index.html and make sure you have xfce4 selected.


Once it’s installed. Follow these steps:

  1. In WSL2 console, set this export:

    export DISPLAY=localhost:0
    

    This will tell X11 programs where to display (:0 is the first local display that was started). I recommend you to add this line to your .bashrc (or any other shell), so you don’t have to remember next time you open WSL2.

  2. Open Cygwin terminal and run this command:

    startxfce4 -- -listen tcp
    

    This should open Xfce windows manager. Again, best to keep this as an alias. At that point you should see Xfce desktop.

  3. Go to Xfce desktop and open xterm (Programs -> run -> type xterm) and run:

    xhost +localhost
    

    This will allow any local programs to access X.

  4. Go back to WSL2 console and run IEx

    iex(1)> :observer.start
    :ok
    


voilà! You can use :observer now!


I hope that helps :)