X11 Error on Windows


After installation, when I start AFNI on WSL2 (Ubuntu 20.04), it gives me the following error:

Initializing X11 Error: Can't open display: 0.0
++AFNI is detached from terminal

I’ve launched VcXsrv before starting terminal and I’ve tried switching to WSL1 too (as advised in other threads).

The output of afni_system_check.py -check_all is following:

root@DESKTOP-0TMQH6V:~# afni_system_check.py -check_all
-------------------------------- general ---------------------------------
architecture:         64bit ELF
system:               Linux
release:              5.4.72-microsoft-standard-WSL2
version:              #1 SMP Wed Oct 28 23:40:43 UTC 2020
distribution:         ubuntu 20.04 focal
number of CPUs:       8
apparent login shell: bash
shell RC file:        .bashrc (exists)

--------------------- AFNI and related program tests ---------------------
which afni           : /root/abin/afni
afni version         : Precompiled binary linux_ubuntu_16_64: May 29 2021
                     : AFNI_21.1.09 'Domitian'
AFNI_version.txt     : AFNI_21.1.09, linux_ubuntu_16_64, May 29 2021
which python         : /usr/bin/python
python version       : 3.8.5
which R              : /usr/bin/R
R version            : R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
which tcsh           : /usr/bin/tcsh

instances of various programs found in PATH:
    afni    : 1   (/root/abin/afni)
    R       : 1   (/usr/bin/R)
    python  : 1   (/usr/bin/python3.8)
    python2 : 0
    python3 : 1   (/usr/bin/python3.8)

** have python3 but not python2

testing ability to start various programs...
    afni                 : success
    suma                 : success
    3dSkullStrip         : success
    uber_subject.py      : success
    3dAllineate          : success
    3dRSFC               : success
    SurfMesh             : success
    3dClustSim           : success
    3dMVM                : success

checking for R packages...
    rPkgsInstall -pkgs ALL -check : success

R RHOME : /usr/lib/R

checking for $HOME files...
    .afnirc                   : found
    .sumarc                   : found
    .afni/help/all_progs.COMP : found

------------------------------ python libs -------------------------------
** failed to load module PyQt4
-- PyQt4 is no longer needed for an AFNI bootcamp

++ module loaded: matplotlib.pyplot
   module file : /usr/lib/python3/dist-packages/matplotlib/pyplot.py

-------------------------------- env vars --------------------------------
PATH = /root/abin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.Ubuntu20.04onWindows_2004.2021.222.0_x64__79rhkp1fndgsc:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Users/akif/AppData/Local/Microsoft/WindowsApps:/snap/bin

R_LIBS = /root/R


------------------------------ data checks -------------------------------
data dir : found AFNI_data6   under $HOME
           top history: 20 Feb 2020 [rickr]: updated FT_analysis examples
data dir : found AFNI_demos   under $HOME
           top history: ...ct 2020 [taylorp]: updated scripts under FATCAT_DEMO
data dir : found suma_demo    under $HOME
           top history: ...s_New/data/Build_tmp on Mon Mar  4 11:56:45 EST 2013
data dir : found afni_handouts under $HOME
atlas    : found TT_N27+tlrc  under /root/abin

------------------------------ OS specific -------------------------------
which apt-get        : /usr/bin/apt-get
apt-get version      : apt 2.0.4 (amd64)

have Ubuntu system: ubuntu 20.04 focal
have Ubuntu afni  : Precompiled binary linux_ubuntu_16_64: May 29 2021

=========================  summary, please fix:  =========================
*  just be aware: login shell 'bash', but our code examples use 'tcsh'


Okay, after trying random stuff advised on message boards, I’ve figured it out. It works now. Sorry if I caused any inconvenience.

No inconvenience. But what worked for you, if I might ask?

WSL2 has been a fast moving target; we don’t really have a full set of recommendations for it yet. The interaction with the X-servers seems like it is getting closer to being much easier (almost don’t need to start something secondarily), but is actually harder to get working than in WSL1. And, again, changing…


Well, as you said before: Windows: the gift that keeps on giving (headaches)…

This line of code seems to be the problem:

export DISPLAY="$(/sbin/ip route | awk '/default/ { print $3 }'):0"

I have to run it every time I open the terminal (just like XServer).

Is there a way to make it global(ish) so that I don’t have to copy paste it every time?


You should be able to add that line to your .bashrc in your home directory.