Run a Program on your dedicated Nvidia graphics card on Linux
I've got a new laptop, and in it I have an Nvidia graphics card. The initial operating system installation went ok (Ubuntu 20.10 Groovy Gorilla is my Linux distribution of choice here), but after I'd done a bunch of configuration and initial setup tasks it inevitably came around to the point in time that I wanted to run an application on my dedicated Nvidia graphics card.
Doing so is actually really quite easy - it's figuring out the how that was the hard bit! To that end, this is a quick post to document how to do this so I don't forget next time.
Before you continue, I'll assume here that you're using the Nvidia propriety drivers. If not, consult your distribution's documentation on how to install and enable this.
With Nvidia cards, it depends greatly on what you want to run as to how you go about doing this. For CUDA applications, you need to install the
sudo apt install nvidia-cuda-toolkit
...and then there will be an application-specific menu you'll need to navigate to tell it which device to use.
For traditional graphical programs, the process is different.
In the NVIDIA X Server Settings, go to PRIME Profiles, and then ensure
NVIDIA On-Demand mode is selected. If not, select it and then reboot.
Then, to launch a program on your Nvidia dedicated graphics card, do this:
__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia command_name arguments
In short, the
__NV_PRIME_RENDER_OFFLOAD environment variable must be set to
1 , and the
__GLX_VENDOR_LIBRARY_NAME environment variable must be set to
nvidia. If you forget the latter here or try using
DRI_PRIME=1 instead of both of these (as I advise in my previous post about AMD dedicated graphics cards), you'll get an error like this:
libGL error: failed to create dri screen libGL error: failed to load driver: nouveau
The solution is to set the above environment variables instead of
DRI_PRIME=1. Thanks to this Ask Ubuntu post for the answer!
Given that I know I'll be wanting to use this regularly, I've created a script in my bin folder for it. It looks like this:
#!/usr/bin/env bash export __NV_PRIME_RENDER_OFFLOAD=1; export __GLX_VENDOR_LIBRARY_NAME=nvidia; $@;
Save the above to somewhere in your
PATH (e.g. your
bin folder, if you have one). Don't forget to run
chmod +x path/to/file to mark it executable.
Found this useful? Does this work on other systems and setups too? Comment below!