# How to use osmesa backend?

**URL:** https://discourse.glfw.org/t/how-to-use-osmesa-backend/2697
**Category:** support
**Created:** [July 25, 2024, 9:42am UTC](https://discourse.glfw.org/t/how-to-use-osmesa-backend/2697 "2024-07-25T09:42:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![songtianhui](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/songtianhui/32/953_2.png) [@songtianhui](https://discourse.glfw.org/u/songtianhui)
#### Post date: [July 25, 2024, 9:42am UTC](https://discourse.glfw.org/t/how-to-use-osmesa-backend/2697/1 "2024-07-25T09:42:02Z")

</div>

I want to run an application glfw. The case is that the machine does not have display, so I try to use osmesa backend and I am struggling with it.

Here is my simple original code:

```c
int main(int argc, char *argv[])
{
    GLFWwindow* window;
    int width, height;

    printf("%s\n", glfwGetVersionString());
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        exit( EXIT_FAILURE );
    }

    glfwWindowHint(GLFW_DEPTH_BITS, 16);
    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);

    window = glfwCreateWindow( 300, 300, "Gears", NULL, NULL );
    if (!window)
    {
        fprintf( stderr, "Failed to open GLFW window\n" );
        glfwTerminate();
        exit( EXIT_FAILURE );
    }

    // do something
}

```

It works well, with X11.

Now I am trying to replace it with osmesa, so I add these lines:

```c
    glfwWindowHint(GLFW_DEPTH_BITS, 16);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  
    glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_OSMESA_CONTEXT_API);

```

When running the program, it crashes with “Failed to initialize GLFW”. And with error\_callback there is error: [65543]: OSMesa: Failed to create context.  
What is the correct approach to switch the normal mode to osmesa?  
Thanks!

---

<div class="post-metadata">

### Author: ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)
#### Post date: [July 25, 2024, 10:57am UTC](https://discourse.glfw.org/t/how-to-use-osmesa-backend/2697/2 "2024-07-25T10:57:17Z")

</div>

I would first check you have OSMesa installed, and also remove all window hints except for `glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_OSMESA_CONTEXT_API);` as it’s possible the selection of hints you have requested are not compatible with OSMesa.

---

<div class="post-metadata">

### Author: ![songtianhui](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/songtianhui/32/953_2.png) [@songtianhui](https://discourse.glfw.org/u/songtianhui)
#### Post date: [July 26, 2024, 9:01am UTC](https://discourse.glfw.org/t/how-to-use-osmesa-backend/2697/3 "2024-07-26T09:01:19Z")

</div>

I have run `sudo yum install mesa-libOSMesa-devel` and there is `/usr/lib/libOSMesa.so`. It looks OK after removing all window hints except for `glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_OSMESA_CONTEXT_API)`! Thanks!  
Further step. It currently works with X11 forwarding, etc. with environment variable DISPLAY=xxx.  
Finally, I want the program to run on the server without a screen device and X serer, I think there will be no such DISPLAY variable. So I run `unset DISPLAY`, and run the program. It crushes with message `[65550]: Failed to detect any supported platform` and `Failed to initialize GLFW`.  
According to my understanding, OSMesa is for the purpose of running off-screen. How do I fix this? In compilation or runtime?  
Thanks a lot for helping with my stupid question 🙂

---

<div class="post-metadata">

### Author: ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)
#### Post date: [July 26, 2024, 9:21am UTC](https://discourse.glfw.org/t/how-to-use-osmesa-backend/2697/4 "2024-07-26T09:21:02Z")

</div>

If you are using GLFW on a headless system you will need to use the `GLFW_PLATFORM_NULL`, see the [Runtime platform selection documentation](https://www.glfw.org/docs/latest/intro_guide.html#platform).

I see you’ve also opened an issue on GLFW, so I’ll link it here for reference:

> <https://github.com/glfw/glfw/issues/2594>
>
> I want to run an application glfw. The case is that the machine does not have di…splay, so I try to use osmesa backend and I am struggling with it.
> 
> Here is my simple original code:
> \`\`\`c
> int main(int argc, char \*argv\[\])
> {
> GLFWwindow\* window;
> int width, height;
> 
> printf("%s\\n", glfwGetVersionString());
> if( !glfwInit() )
> {
> fprintf( stderr, "Failed to initialize GLFW\\n" );
> exit( EXIT\_FAILURE );
> }
> 
> glfwWindowHint(GLFW\_DEPTH\_BITS, 16);
> glfwWindowHint(GLFW\_TRANSPARENT\_FRAMEBUFFER, GLFW\_TRUE);
> 
> window = glfwCreateWindow( 300, 300, "Gears", NULL, NULL );
> if (!window)
> {
> fprintf( stderr, "Failed to open GLFW window\\n" );
> glfwTerminate();
> exit( EXIT\_FAILURE );
> }
> 
> // do something
> }
> \`\`\`
> It works well, with X11.
> 
> Now I am trying to replace it with osmesa, so I add these lines:
> \`\`\`
> glfwWindowHint(GLFW\_DEPTH\_BITS, 16);
> glfwWindowHint(GLFW\_CONTEXT\_VERSION\_MAJOR, 3);
> glfwWindowHint(GLFW\_CONTEXT\_VERSION\_MINOR, 3);
> glfwWindowHint(GLFW\_TRANSPARENT\_FRAMEBUFFER, GLFW\_TRUE);
> glfwWindowHint(GLFW\_OPENGL\_PROFILE, GLFW\_OPENGL\_CORE\_PROFILE);  
> glfwWindowHint(GLFW\_CONTEXT\_CREATION\_API, GLFW\_OSMESA\_CONTEXT\_API);
> \`\`\`
> When running the program, it crashes with "Failed to initialize GLFW". And with error\_callback there is error: \[65543\]: OSMesa: Failed to create context.
> What is the correct approach to switch the normal mode to osmesa?
> Thanks!

---

<div class="post-metadata">

### Author: ![songtianhui](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/songtianhui/32/953_2.png) [@songtianhui](https://discourse.glfw.org/u/songtianhui)
#### Post date: [July 26, 2024, 10:09am UTC](https://discourse.glfw.org/t/how-to-use-osmesa-backend/2697/5 "2024-07-26T10:09:38Z")

</div>

It works! Now my whole program runs successfully and correctly.  
I have struggled with it for many days, to find a way to run glfw headless. I have tried things like xvfb, osmesa, egl. I encountered various errors and always wondered if I was using OSMesa correctly. It seems that OSMesa headless is a new feature in recent versions, some usage details like ‘remove hints’ and ‘set GLFW\_PLATFORM\_NULL’ are missing. Adding some example code or test cases would be user-friendly.🙂  
Thank you very much!
