Switch OpenGL loader from Mesa to Nvidia, cannot open OpenGL context

Hey guys

I just created a fresh install of Ubuntu 1804 with Cuda 10.1 from the local runfile installation. I also let it bring in the desired driver that comes with the CUDA installation. I did not get any errors from the installation.

I then started to try out the Nvidia sample code that installed right off of my home directory. I typed the “make” command right off of the root directory. All of the examples compiled except for “2_Graphics/marchingCubes”.

It would not compile with this message:

make[1]: Entering directory ‘/home/devmachine/NVIDIA_CUDA-10.1_Samples/2_Graphics/marchingCubes’
/usr/local/cuda/bin/nvcc -ccbin g++ -m64 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_75,code=compute_75 -o marchingCubes marchingCubes.o marchingCubes_kernel.o -lGL -lGLU -lglut
/usr/bin/ld: cannot find -lGL
collect2: error: ld returned 1 exit status
Makefile:314: recipe for target ‘marchingCubes’ failed
make[1]: *** [marchingCubes] Error 1
make[1]: Leaving directory ‘/home/devmachine/NVIDIA_CUDA-10.1_Samples/2_Graphics/marchingCubes’
Makefile:51: recipe for target ‘2_Graphics/marchingCubes/Makefile.ph_build’ failed
make: *** [2_Graphics/marchingCubes/Makefile.ph_build] Error 2

I notice the line “/usr/bin/ld: cannot find -lGL”. I thought this was strange considering OpenGL should come installed when I install CUDA and the correct graphics driver.

I then found that I can check to see what version of OpenGL I am using from the command “glxinfo | grep “OpenGL version””.

On my machine that is not working, I get this output:

OpenGL version string: 3.0 Mesa 19.0.8

I thought that is strange since I am supposed to be using Nvidia drivers.
I tried this on a machine with different problems (but OpenGL did not appear to be one of them) and it gave me this output:

OpenGL version string: 4.6.0 NVIDIA 410.48

This started me wondering: what is different? Well, the driver 410 corresponds with CUDA 10.0, but I do not think a slightly different version of CUDA and driver should prevent OpenGL from loading.
I then had the idea to try installing the CUDA 10.1 revision 1 installer (10.1.168 I believe) since I don’t know what else to install.
I installed 10.1 revision 1 without a hitch. I am now able to compile all of the CUDA examples. I go tro try the same Cuba example and now I get this error:

[./marchingCubes] - Starting…
GPU Device 0: “GeForce GTX 1050 Ti” with compute capability 6.1

grid: 32 x 32 x 32 = 32768 voxels
max verts = 102400
Read ‘./data/Bucky.raw’, 32768 bytes
CUDA error at marchingCubes.cpp:485 code=999(cudaErrorUnknown) “cudaGraphicsGLRegisterBuffer(&cuda_posvbo_resource, posVbo, cudaGraphicsMapFlagsWriteDiscard)”

I am not sure exactly what this means since the error literally says “cudaErrorUnknown”.
In fact, whenever I try to run any of the Cuda examples, I get just about the exact same error message, save for the executable name and the extra info sent to “std::cout” before the graphics loading happens.
I have been trying to run code from this git repository that focuses on OpenGL


and all of the code compiles, but when I try to run the code, I get the same error:
“Unable to create OpenGL context.”
I have been looking around on the internet, and it looks like I need to just change from the Mesa OpenGL loader to the Nvidia loader OpenGL loader. The question is:

How do I switch to the Nvidia OpenGL loader?

I am happy to provide more information on the issue if necessary.

More notes:

I ran the command

startx – -logverbose 6

because the Nvidia post mentions this. This command crashes with this output:

startx – -logverbose 6
xauth: file /home/devmachine/.Xauthority does not exist

/usr/lib/xorg/Xorg.wrap: Only console users are allowed to run the X server
xinit: giving up
xinit: unable to connect to X server: Connection refused
xinit: server error
Couldn’t get a file descriptor referring to the console

This is starting to look like an Xorg issue too, but I am not sure what to focus on.

I have also created a log by running

sudo nvidia-bug-report.sh

So hopefully that helps.

Oh, and one more thing: when I try to go to Nvidia x settings from the GUI, the option shows up, but when I click it, nothing happens.

All of the errors I experienced above were when I was logged into a regular GUI.

I also have provided the nvidia-installer.log file to help out.

Actually, it looks like I have to attach files after the fact.
nvidia-installer.log (28.1 KB)
nvidia-bug-report.log.gz (1.07 MB)
cuda-installer.log (4.96 MB)

what is the output of:

lspci |grep -i vga


This is my output for lspci |grep -i vga:

00:02.0 VGA compatible controller: Intel Corporation Device 3e9b
01:00.0 VGA compatible controller: NVIDIA Corporation GP107M [GeForce GTX 1050 Ti Mobile] (rev a1)

So you have two display controllers in your machine, and this will complicate things.

Which display adapter is your monitor connected to?

Uhm, this is an Acer Nitro 5 an515-53 Laptop, 2018.

I do not know how to answer your question.

OK that is the problem.

Without actually having your laptop (which I don’t) I wouldn’t be able to give a precise set of instructions to explain how to run OpenGL on linux on the dGPU (the NVIDIA GPU). Laptop hardware designs vary, and in my experience there is not one single formula that works on them all.

The biggest challenge here is getting CUDA/OpenGL interop to work, which is what the sample code is demonstrating.

If you simply want OpenGL functionality, its possible that the OpenGL graphics support on the Intel iGPU is enough.

You can try contacting your laptop vendor, to see if they have a recommended linux graphics install procedure.

You may also have luck if you don’t use the runfile install method from NVIDIA, but install using the NVIDIA Ubuntu package manager method, and/or the Ubuntu ppa repository method.


I just want to clarify then the future steps.

I should download the local Deb file installer and install that on TOP of my current installation.

That should give me the correct functionality?

Should I wipe my machine of linux first?

Why would the run file installation not work as well as the Deb file?

I have found that the ppa installs do not work very well.

Is there a way to disable integrated graphics?

But I do want to emphasize: I am NOT able to run OpenGL code that I need to be able to run. I am now able to link with it, but any actual OpenGL code besides import testers do not work.


I just used the local deb version

and it now works like a charm. I think the Nvidia x server was straight up not installed with the runfile. The deb file brought it in.

I will consider this issue closed.

First of all I suggest you should read the CUDA linux install guide. I suggest trying to read it thoroughly and understand it thoroughly.


If you read that carefully, you’ll discover that you should not just do a package manager install “on top” of a pre-existing runfile install. The two options are either to reinstall linux or else follow the suggestions given to handle conflicting install methods.

I won’t be able to give a detailed response to why the runfile install is different than the package manager install. The runfile install basically follows a script or formula. The package manager install uses dependencies to install things, the net effect is that there can be differences.

Regarding is there a way to disable integrated graphics, quite simply I don’t know. it depends on your laptop design. You may wish to study your system BIOS options to see if there is such an option. Some laptops do have that option, but most typically don’t in my experience, because there is tight integration between the dGPU and iGPU, and its not a trivial matter to just disable the iGPU; it supports activities of the dGPU.

I see what you are talking about in the 2.7. Handle Conflicting Installation Methods section.

I installed CUDA 10.1 revision 2 deb on top of CUDA 10.1 revision 1 runfile, so it appears that there should not be an issue, looking at the matrix.

I did not know there were ways to disable iGPU in the BIOS, but that makes sense now why that might be an option.

I should have known to think that the deb version would look at the dependencies and then see that a bunch are missing: the runfile simply does the same thing no matter what. That makes a lot more sense now. I appreciate your support.