“XDG_RUNTIME_DIR is invalid or not set” on Raspberry Pi 5

“XDG_RUNTIME_DIR is invalid or not set” on Raspberry Pi 5

Recently, while running Rasperry Pi OS (lite) on my sparkly new Raspberry Pi 5 I ran into an issue running the MPV media player. I had created two user accounts, one to log into the Pi with and another to run my program under. I then encountered a strange problem with MPV. MPV output the following error to the console:

error: XDG_RUNTIME_DIR is invalid or not set in the environment.
error: XDG_RUNTIME_DIR is invalid or not set in the environment.
Opening /dev/dri/renderD128 failed: Permission denied
error: XDG_RUNTIME_DIR is invalid or not set in the environment.
error: XDG_RUNTIME_DIR is invalid or not set in the environment.
Opening /dev/dri/renderD128 failed: Permission denied
error: XDG_RUNTIME_DIR is invalid or not set in the environment.
error: XDG_RUNTIME_DIR is invalid or not set in the environment.
libEGL warning: failed to open /dev/dri/renderD128: Permission denied

libEGL warning: failed to open /dev/dri/card0: Permission denied

So… what the heck is that? Can this new user not output to my screen? Sufferin’ suckatash…

Looking up the most prominent of those errors, it looks like XDG_RUNTIME_DIR is a user-specific directory that gets used by whatever Desktop (x11, wayland, etc) you happen to be running. I’m not running any specific desktop on this device (It’s the “Lite” version, so just a console for me), but apparently I still need to have this directory to run “desktoppy” things. Yes, “desktoppy” is clearly a word. Don’t question it.

So, I need a user-specific one of these. I used the following command to assign a directory and add it to my user’s BASH profile:

echo "export XDG_RUNTIME_DIR=/run/user/$(id -u)" >> ~/.bashrc

This uses my user ID number to create a directory in /run/user/ for my user to create desktoppy things in. In this case my directory turned out to be /run/user/1001 since it was the second user account that I created on the system.

Next we have the permission denied issues. I tested the command with my first user, and found that the command ran just fine. So, there’s clearly some kind of difference between my first user (the one that I created during system install) and my second user (the one that I created to run my app under). It’s likely that these directories were created to be used by users of the system, so I’m probably dealing with some group permissions that aren’t dished out by default. Let’s check those.

$ ls -al /dev/dri/renderD128 
crw-rw----+ 1 root render 226, 128 Mar  8 16:45 /dev/dri/renderD128

$ ls -al /dev/dri/card0 
crw-rw----+ 1 root video 226, 0 Mar  8 16:45 /dev/dri/card0

Now lets see what groups my two users are members of:

$ sudo groups myuser
myuser : myuser adm dialout cdrom sudo audio video plugdev games users input render netdev spi i2c gpio

$ sudo groups user2
user2 : user2

Well, there it is. Clearly I never added my new user to the “render” or “video” groups, because I didn’t know I needed to. Let’s add “user2” to those groups I guess:

$ sudo adduser user2 render
$ sudo adduser user2 video

Boom. Everything works smooth like butter. Wonder what other groups I might need to add “user2” to in order to make sure things work properly…? Rather than just blindly copying user ALL permissions from one user to the next, I’d rather just give a user access to what they need to run and nothing more.

Hope this helps someone.

Leave a Reply

Your email address will not be published. Required fields are marked *

20 + 5 =