GLFW in a Google Cloud Platform VM server


I want to use a C++ project that I adapted from github on a Google Cloud Platform Linux VM with Linux Debian 11 (bullseye) distribution installed.

The problem I am facing is that the function glfwCreateWindow () is returning NULL and my code doesn’t work.

After having this problem, I searched a lot and first I was thinking that it was a problem related with not having a X11 server.

First of all: I know that a “X11” server runs on machines that have a monitor/display. Is that right?

Secondly: do a regular google cloud platform VM has a X11 server and could use GLFW?

I am really lost about this, because I am not an expert in Linux neither have any experience with GLFW.


You can run X11 server without any monitor attached. X11 is needed if you want to use GLX to access OpenGL on your system. (just be sure to run real x11 server, something like Xvfb won’t work). You can always install X11 server on any cloud machine you have root access to.

If you want GPU accelerated OpenGL then you can look into using EGL over DRM to create OpenGL context. That will not require running any display server.

Here’s a pull request that adds EGL support over DRM: Add EGLDevice backend by kchandra22134 · Pull Request #786 · glfw/glfw · GitHub
Here’s a pull request that adds EGL support to render to pbuffer: EGL headless mode by christopher-hesse · Pull Request #1608 · glfw/glfw · GitHub
They are not merged to main codebase.

There is another way if your EGL supports EGL_KHR_surfaceless_context extension - that allows to create EGL context without any surface (window or pbuffer). There’s nothing for this in glfw, you would need to create patch to glfw (should be very similar to #1608 pull request).

Alternative is OSMesa that is supported already now: GLFW: Release notes
But that won’t give you accelerated GL context, it will provide software implemented OpenGL context (rendering on CPU).

Are you sure you want to use glfw for this? Because if you have EGL support and you have surfaceless extension (which you most likely do if drivers are reasonably recent), then creating EGL context can be done in very few lines, and no need for glfw at all.

For example code see my gist here: setting up and using modern OpenGL 4.5 core context on X11 with EGL · GitHub (just ignore lines 182 to 196 for surface creation)

1 Like

@mmozeiko Thanks for the quick reply!

I read your answer, but I am not able to find a solution based on it yet, because I lack background on many topics that you mentioned (sorry about that).

If you could please help me, let me just detail a little more what I want to achieve and maybe you can give me a way of finding the solution.

I want to run a part of this code at git hub DLR-RM/3DObjectTracking/tree/master/RBGT.

In a backend. More specifically, the GenerateModel function, which calls this RendererGeometry → Init function, that uses GLFW and gives an error in this glfwCreateWindow call.

So, my question is: to run this kind of code, can I use a server in the cloud, without a monitor?

This code just renders 2052 views of a 3D model and save it in a special kind of file.

Do I need to install a X server to run this code and maybe there is no X server installed in the server, that is why it is not creating the glfw window?

Thanks again.

I will register here what I try to solve my problem:

  1. As mentioned, I am new to the field of X11 / OpenGL / GLFW. What I know is that X11 is like a “display service” and that GLFW creates a context to run OpenGL code.

  2. First I thought: I need to make this C++ code that uses GLFW run in a google cloud server. But it gives error in the line glfwCreateWindow.

  3. Then I started google for more than 10 days now: glfw gcp, glfw headless, glfw X11, what is X11, start X11 linux, etc etc etc.

  4. After installing some packages like “mesa” and another stuff that I googled, I faced the error:

GLX extension not found .

Now I am thinking:

Is there any X server running in my cloud Virtual Machine?

Am I the only person that does not have a X server running and this is a trivial stuff, like, i just need to run a command and the X server will just start?

Is the problem like a missing OpenGL library?

I will continue to share my discoveries in this thread.

That code is calling functions like glfwCreateWindow because that is only way how to create GL context with glfw library. glfw library creates window and GL context at the same time. If you do not need to display anything there is no reason to create window, but that is not possible with current version of glfw.

I would suggest you to forget about glfw, and simply create GL context with whatever platform API you have on that machine, most likely EGL. This will simplify everything, no need to install and configure display server. Just need to do minor adjustments to code that uses glfw to not use it - that is not a lot of work, especially if glfw is not used for things like input processing. That RBGT code use glfw only for creating GL context.

You can run X11 server in virtual machine as long as GPU of your host cloud machine is accessible from that VM. You just need to make sure X11 server is configured to use GPU and does not fallback to some generic software blitter or virtual framebuffer. Check if you have gpu drivers installed and check X11 log file of what it tries to use and how it configures itself. This really is out of scope of glfw library.

Ok, then I continue to struggle to make this project in Google Cloud Platform VM work with GLFW…

I tried a suggestion of @mmozeiko , using the GLFW_OSMESA as context creation API.

First it complained something about OSMESA not available… then I installed the package libosmesa6 and finally it gave me the error “OSMesa: Foward-compatible contexts not supported”
This lead me to this page after googling for this error:

In this issue of another package, many people talk about “headless server”. I suspect that GLFW doesn’t work on headless server… but I need to continue the investigation. I will reply here.

Thank you very much, @mmozeiko . I will try replacing glfw by egl in the code.
You are right, it uses GLFW just to create a context and there is no input at all.

then I installed the package libosmesa6 and finally it gave me the error “OSMesa: Foward-compatible contexts not supported”

It is probably because of too old mesa package version
Latest mesa version (21.3.0) supports up to version 4.5 of both - GL core and GL compatibility profiles in OSMesa.

I suspect that GLFW doesn’t work on headless server…

That is not true. As I said above - you need properly configured X11 display server running if you want to use current glfw version. Once X11 with GLX is working (you can check with glxinfo tool) then glfw will work fine. “headless” in a sense if monitor is connected or not is relevant here. All it matters is working X11 display server with proper GPU drivers.

After trying to install EGL and get stuck on linking problem, and tried to use a Windows machine and get stuck in another linking problem with GLFW, I am back in trying to enable X11 server on the virtual machine.

@mmozeiko for your information, the glxinfo returns:
Error: couldn’t find RGB GLX visual or fbconfig

And also the machine has no GPU ( I tried with a machine of google cloud with a Tesla K80 GPU also and it gave the same message too).

I will continue trying.