Headless installation does not work

I am trying to set up mne to work on an headless server, I am not an expert and it is the first time that I do it so I may be facing a stupid issue. Anyway, I created a new environment on the server using conda following these instructions:
Advanced setup β€” MNE 1.7.1 documentation

I ran this:
conda create --override-channels --channel=conda-forge --name=mne mne "vtk>=9.2=*osmesa*" "mesalib=21.2.5" -y

Activated the environmet and ran a very simple code snipped just to test it

import mne
from mne.io import read_raw_egi
from pathlib import Path

mff_file = Path('RawData/33/PDGS_02_33_01_01_20190201_111423.mff')

raw = read_raw_egi(mff_file, preload=True)

raw.compute_psd(fmax=50).plot(picks="data", exclude="bads", amplitude=False)
raw.plot(duration=5, n_channels=30)

Error:

Using qt as 2D backend.                                                                                                                                                                                         
Traceback (most recent call last):                                                                                                                                                                              
  File "/mnt/S/edoardoStorage/EEG/test1.py", line 100, in <module>                                                                                                                                              
    raw.plot(duration=5, n_channels=30)                                                                                                                                                                         
  File "/home/edoardo/miniconda3/envs/mne/lib/python3.9/site-packages/mne/io/base.py", line 1856, in plot
    return plot_raw(
  File "<decorator-gen-179>", line 12, in plot_raw
  File "/home/edoardo/miniconda3/envs/mne/lib/python3.9/site-packages/mne/viz/raw.py", line 409, in plot_raw
    fig = _get_browser(show=show, block=block, **params)
  File "/home/edoardo/miniconda3/envs/mne/lib/python3.9/site-packages/mne/viz/_figure.py", line 698, in _get_browser
    fig = backend._init_browser(**kwargs)
  File "/home/edoardo/miniconda3/envs/mne/lib/python3.9/site-packages/mne_qt_browser/_pg_figure.py", line 5173, in _init_browser
    out = _init_mne_qtapp(pg_app=True, **app_kwargs)
  File "/home/edoardo/miniconda3/envs/mne/lib/python3.9/site-packages/mne/viz/backends/_utils.py", line 162, in _init_mne_qtapp
    raise RuntimeError("Cannot connect to a valid display")
RuntimeError: Cannot connect to a valid display

mne sys_info

Platform             Linux-3.10.0-1160.el7.x86_64-x86_64-with-glibc2.17
Python               3.9.18 | packaged by conda-forge | (main, Aug 30 2023, 03:49:32)  [GCC 12.3.0]
Executable           /home/edoardo/miniconda3/envs/mne/bin/python3.9
CPU                  x86_64 (32 cores)
Memory               251.3 GB

Core
β”œβ˜‘ mne               1.7.1 (latest release)
β”œβ˜‘ numpy             1.26.4 (OpenBLAS 0.3.27 with 32 threads)
β”œβ˜‘ scipy             1.13.1
β””β˜‘ matplotlib        3.9.1 (backend=agg)

Numerical (optional)
β”œβ˜‘ sklearn           1.5.1
β”œβ˜‘ numba             0.60.0
β”œβ˜‘ nibabel           5.2.1
β”œβ˜‘ nilearn           0.10.4
β”œβ˜‘ dipy              1.9.0
β”œβ˜‘ openmeeg          2.5.6
β”œβ˜‘ pandas            2.2.2
β”œβ˜‘ h5io              0.2.4
β”œβ˜‘ h5py              3.9.0
β””β˜ unavailable       cupy

Visualization (optional)
β”œβ˜‘ pyvista           0.44.1 (OpenGL 3.3 (Core Profile) Mesa 21.2.5 via softpipe)
β”œβ˜‘ pyvistaqt         0.11.1
β”œβ˜‘ vtk               9.2.6
β”œβ˜‘ qtpy              2.4.1 (None=None)
β”œβ˜‘ pyqtgraph         0.13.7
β”œβ˜‘ mne-qt-browser    0.6.3
β”œβ˜‘ ipywidgets        8.1.3
β”œβ˜‘ trame_client      3.2.1
β”œβ˜‘ trame_server      3.0.3
β”œβ˜‘ trame_vtk         2.8.9
β”œβ˜‘ trame_vuetify     2.6.2
β””β˜ unavailable       ipympl

Ecosystem (optional)
β”œβ˜‘ eeglabio          0.0.2-4
β”œβ˜‘ edfio             0.4.3
β”œβ˜‘ mffpy             0.9.0
β”œβ˜‘ pybv              0.7.5
β””β˜ unavailable       mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo

How can I set it up? I don’t find many more info on the internet

To visualize the plot - how are you logged into the headless server? You would need to pass the visualization from the headless server and there are a couple of ways to do it. On loging to the headless server use ssh -X username@servername which will provide you X- forwarding that will display the plot on your local machine. This generally is slow because it is passing uncompressed plots through the network.

If you use nomachine or VNC to create a virtual display on the headless server, this will provide image compression before sending it over the network. It also provides you with a persistent desktop to work from in case the network gets disrupted. From the NoMachine or VNC display launch python/mne and the plot should work. 2D rendering seems to work well - but 3D visualizations (coreg/activation renderings) are tough on a headless system.

Hope that helps.

–Jeff

Thank you very much for your reply.

Today I tried some time more, so I would like to avoid installing other things on the server so I went with two moethods, one is trying to use jupyter notebooks, like specified here: Using MNE with 3D visualization on a headless server - :speech_balloon: Support & Discussions - MNE Forum which kled me here: Little tour of MNE features with the ipyvtk backend (github.com). I adjusted my code according to what is described in this file (added these imports)

import mne
from mne.io import read_raw_egi
from pathlib import Path

%matplotlib inline
mne.viz.set_3d_backend('notebook')  # set the 3d backend

And performed the required steps to connect.

With this it simpy crashes, the last line printed is: Using qt as 2D backend.

The log is:

11:29:53.118 [info] Started session for kernel startUsingPythonInterpreter:.jvsc74a57bd066a4a0c7835b3b395ee935744816daaccaca5621985777d7a7ca94a222dfad18./home/~/miniconda3/envs/mne/python./home/~/miniconda3/envs/mne/python.-m#ipykernel_launcher
11:29:53.350 [info] Process Execution: ~/miniconda3/envs/mne/bin/python /home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/pythonFiles/printJupyterDataDir.py
11:30:01.626 [error] Error in waiting for cell to complete [Error: Canceled future for execute_request message before replies were done
	at Qr.dispose (/home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/dist/node_modules/@jupyterlab/services.js:11:22379)
	at /home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/dist/node_modules/@jupyterlab/services.js:11:41058
	at Map.forEach (<anonymous>)
	at n._clearKernelState (/home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/dist/node_modules/@jupyterlab/services.js:11:41044)
	at /home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/dist/node_modules/@jupyterlab/services.js:11:43455
	at process.processTicksAndRejections (node:internal/process/task_queues:95:5)]
11:30:01.627 [warn] Cell completed with errors {
  message: 'Canceled future for execute_request message before replies were done'
}

The other method is using X- forwarding. With theis method I keep getting the same error described in the post.

Would it be possible for you to elaborate on how I can connect to it using the -X option when using ssh? Or do you have any Idea on why the jupyter kernel crashes?

Thank you very much in advance,