Skip to content

Instantly share code, notes, and snippets.

@Mluckydwyer
Last active April 8, 2024 09:14
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Mluckydwyer/8df7782b1a6a040e5d01305222149f3c to your computer and use it in GitHub Desktop.
Save Mluckydwyer/8df7782b1a6a040e5d01305222149f3c to your computer and use it in GitHub Desktop.
Install OpenGL on Ubuntu in WSL

How to Install OpenGL in Ubuntu in WSL2

These steps have been tested on Windows 10 with WSL2 running Ubuntu.

1. Dependencies

First install the dependencies:

apt install mesa-utils libglu1-mesa-dev freeglut3-dev mesa-common-dev

There are more than we need, but also include GLut and Glu libraries to link aginst during compilation for application development (these can be removed if that functionality is not required).

2. Window Server

Installing

Then, you will need to setup a window server. Windows 11 should have this built in and work out of the box (skip to step 3). In Windows 10 you will need to install one VcXsrv is a popular option (also known as XLaunch).

Configuration

Once installed, then you will need to make a configuration. You want the following settings:

  • First Screen: Multi-Window, Display=0

  • Second Screen: Start No Client

  • Third Screen: Clipboard=True, Primary Selection=True, Native OpenGL=False, Disable Access Control=True

I recomend you save your configuration file somewhere so that each time you start you window server you can open it instead of rebuilding it. Then start your window server.

WSL Display

Once you have setup and started your Window server, you need to tell Ubuntu/WSL where your display is, we can do this by running the following command:

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=0

You will need to do this for every new shell you open. Alternatively, you can add this to your .bashrc file to have your shell do it for you an start up.

3. Running a Test

After the following configuration you should be able to run the test application below and see some multi-colored gears spinning:

glxgears
@itakatz
Copy link

itakatz commented Dec 21, 2022

Great explanation, works and saved me time. Thanks

@pauloud
Copy link

pauloud commented Apr 19, 2023

With wsl2 windows10 and ubuntu I followed the tutorial, but when I try to run an opengl application I have no window (and the program doesn't exit).
Did you assume somme prerequisite ?
Does anybody have a solution please ?
EDIT : Ok it was a problem with the firewall, this solved my issue microsoft/WSL#6430 (comment)

@Mluckydwyer
Copy link
Author

With wsl2 windows10 and ubuntu I followed the tutorial, but when I try to run an opengl application I have no window (and the program doesn't exit).
Did you assume somme prerequisite ?
Does anybody have a solution please ?
EDIT : Ok it was a problem with the firewall, this solved my issue microsoft/WSL#6430 (comment)

Happy to hear you found a solution and included it here.

@shreyas0924
Copy link

Thank you for the solution

@arin-r
Copy link

arin-r commented Jul 13, 2023

On windows 11 with WSL2, on running "glxgears", a black window opens. I can expand / minimize & close the window but the content is all black. I don't see any multi-colored gears spinning.

Does anyone have a similar issue? Can someone suggest a fix?

@bjarthur
Copy link

on windows 10 with WSL2 i don't see anything with glxgears. no window, not even black.

@Mluckydwyer
Copy link
Author

On windows 11 with WSL2, on running "glxgears", a black window opens. I can expand / minimize & close the window but the content is all black. I don't see any multi-colored gears spinning.

Does anyone have a similar issue? Can someone suggest a fix?

It is possible you have some sort of driver issue. If you see a window your windowing system is setup. Maybe just double check your window manager settings. I found I got black screens when I changed settings from what is above. Specifically, Native OpenGL.

@Mluckydwyer
Copy link
Author

on windows 10 with WSL2 i don't see anything with glxgears. no window, not even black.

Do you see any errors about being unable to find a display/the DISPLAY environment variable? Not seeing a window would suggest to me it cannot find your window manager.

@bjarthur
Copy link

bjarthur commented Jul 28, 2023

window manager? you mean vcXsrv?? if i wait long enough, glxinfo eventually says it's unable to find open the display. i was thinking a driver issue too, but not sure how to fix or check that.

@Mluckydwyer
Copy link
Author

window manager? you mean vcXsrv?? if i wait long enough, glxinfo eventually says it's unable to find the display. i was thinking a driver issue too, but not sure how to fix or check that.

@bjarthur Yes, vcXsrv is your window manager in this case. If you are seeing the "Unable to find a display" error, that means your window manager is either not setup correctly, not running, or your DISPLAY environment variable is not pointing correctly to your window manager. If you run echo $DISPLAY, what is printed?

@bjarthur
Copy link

DISPLAY was set to an IPaddress, but the wrong one. i changed it to be the same as the one in network settings and it now works. thanks!

@Mluckydwyer
Copy link
Author

DISPLAY was set to an IPaddress, but the wrong one. i changed it to be the same as the one in network settings and it now works. thanks!

Great to hear! Did you use export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 in your terminal/RC file, or was it set some other way? Just want to check if you hit a case where this doesn't work.

@bjarthur
Copy link

bjarthur commented Jul 28, 2023

this command above didn't work for me: export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0. i had to lookup my correct ipaddr in network settings and set the envvar in .bashrc manually.

@Jamms224
Copy link

On windows 11 with WSL2, on running "glxgears", a black window opens. I can expand / minimize & close the window but the content is all black. I don't see any multi-colored gears spinning.

Does anyone have a similar issue? Can someone suggest a fix?

I'm encountering the exact same issue on Windows 11 with WSL2. Upon running glxgears, the native Windows Server for Windows 11 launches, but it only displays a black screen. Whilst the terminal outputs values like "frames in 5.0 seconds," there's no actual visual output. I decided to test VcXsrv to see if it would yield different results, but unfortunately, the same problem persists - this time with an even worse frame rate for the black screen.

Comments suggest that this could potentially stem from a driver problem, given that a window does appear, or that it might be related to window manager settings. However, I'm unsure how to address these concerns within the built-in Windows 11 Windows Server.

Does anyone have any suggestions or recommendations for troubleshooting this?

@BassamAbdellatif
Copy link

BassamAbdellatif commented Dec 3, 2023

I got this window
image

I got also this flickering window when I ran an application from within Ubuntu/wsl

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