How to use Nvidia-Xrun to unlock your Nvidia laptop's full potential on linux

Robert Washbourne - 3 months ago - desktop environments, distributions, open source news

Optimus laptops are considered a major pain to use on linux, with many different ways to use them. You can opt to use the nvidia proprietary drivers and disable your internal graphics and forgo all hopes of battery life, or disable your graphics card entirely and use integrated graphics. Or, you can use bumblebee, a popular effort to enable powersaving while still using your dedicated GPU, but the project is a mixed bag of bad compatibility, much worse performance than just the nvidia drivers, and zero support for vulkan and no plans to implement it.

But, fear not, nvidia-xrun is here to save you! It's a open source script that can toggle your dedicated GPU on and off for full powersaving. Nvidia-xrun provides the same compatibility as the proprietary drivers, full performance, full vulkan support all at the cost of a little convenience (as far as I can tell, you need to run the nvidia gpu in a separate desktop environment).

For newbies

Skip to the next section if you understood the introduction.

Basically, the goal of this project is to allow games and other applications that can make use of a graphics card run at full speed, the way that they do automatically on Windows. Here are a few definitions:

phrase definition
dedicated graphics card, dedicated GPU, nvidia GPU this is the high performance card in your laptop
integrated GPU, intel graphics the graphics card built into your laptops CPU
nvidia-xrun
a script that allows the high performance GPU to run applications in linux
nvidia drivers the proprietary, not open source, not free as in free beer drivers

I'll be adding to this section as I get questions so please ask if you have one. The comment section is at the bottom of the page.

How to tell if this guide is for you

On Windows, open Device Manager and open the display adapters section:

Nvidia and Intel graphics

Image from the nvidia website. Make sure your system is similar if you plan to follow this tutorial once you install linux (or on your dual booted system).


On linux, run this command:

lspci | grep 'VGA|3D'  

and make sure the output is similar to the following:

XX:XX.X VGA/3D compatible controller: Intel Corporation Graphics  
XX:XX.X VGA/3D compatible controller: NVIDIA Corporation Device  

If the nvidia card is not showing you might not have a driver loaded for it (such as nouveau, the open source driver for nvidia cards).

Installing what you need

This guide is centered on Arch linux, and I will not be providing support on this page. If something is broken or if it is not working, post on your distribution forums, on the nvidia-xrun github page, or linux communities on reddit that can all provide much better support. That said if there is something wrong with this guide or if you have a general question please comment. Nvidia xrun is the easiest way to use a nvidia card on a laptop in my experience and your installation should go smoothly, fingers crossed.

On Arch

Install proprietary drivers:

sudo pacman -S nvidia  

Install nvidia-xrun-git from the aur (edit with your aur helper):

yay nvidia-xrun-git  

Or (if you dont have an aur helper):

git clone https://aur.archlinux.org/nvidia-xrun-git.git  
cd nvidia-xrun-git  
makepkg -si  

On other distributions

Install the nvidia drivers from your package manager (or from the nvidia website).

It's pretty unlikely that nvidia-xrun is in your distribution package manager but check anyway first.

git clone https://github.com/Witko/nvidia-xrun.git  
cd nvidia-xrun  

This is the structure of the script (edited from the git repo)

  • /usr/bin/nvidia-xrun - the executable script
  • /etc/X11/nvidia-xorg.conf - the main X conf file
  • /etc/X11/xinit/nvidia-xinitrc - xinitrc config file. Contains the setting of provider output source
  • /etc/X11/xinit/nvidia-xinitrc.d - custom xinitrc scripts directory
  • /etc/X11/nvidia-xorg.conf.d - custom X config directory
  • /etc/systemd/system/nvidia-xrun-pm.service systemd service
  • /etc/default/nvidia-xrun - nvidia-xrun config file

Optional (we will cover openbox later, I've found the xsession file unnecessary but if you would like, you can try to use it):

  • /usr/share/xsessions/nvidia-xrun-openbox.desktop - xsession file for openbox
  • /usr/share/xsessions/nvidia-xrun-plasma.desktop - xsession file for plasma
  • ~/.nvidia-xinitrc - user-level custom xinit script file

Use something similar to the following to set up the script (this may vary depending on distro! Especially the service portion or the X11 path). Don't blindly paste this in! Look into your distro first and modify the paths.

# inside the nvidia-xrun folder
sudo cp nvidia-xrun /usr/bin/nvidia-xrun  
sudo cp nvidia-xorg.conf /etc/X11/nvidia-xorg.conf  
sudo cp nvidia-xinitrc /etc/X11/xinit/nvidia-xinitrc  
sudo cp nvidia-xrun-pm.service /etc/systemd/system/nvidia-xrun-pm.service

# enable the service
sudo systemctl enable nvidia-xrun-pm

# config directories
sudo mkdir /etc/X11/xinit/nvidia-xinitrc.d/  
sudo mkdir /etc/X11/nvidia-xorg.conf.d/  

Set bus ID

If you got nvidia-xrun from the AUR it's been done automatically. Skip this.

lspci | grep -i nvidia | awk '{print $1}'  

You should get 01:00.0 or something similar. Now make a file in your xorg config directory.

# /etc/X11/nvidia-xorg.conf.d/30-nvidia.conf

Section "Device"  
    Identifier "nvidia"
    Driver "nvidia"
    BusID "PCI:1:0:0"
EndSection

# uncomment if you get issues

Section "Screen"  
    Identifier "nvidia"
    Device "nvidia"
    #  Option "AllowEmptyInitialConfiguration" "Yes"
    #  Option "UseDisplayDevice" "none"
EndSection  

Now reboot and add a desktop.

Openbox and using nvidia-xrun

This is the desktop you will run your games and nvidia applications in. This is required to make apps like steam work but you can try running nvidia-xrun *executable* instead.

Install openbox on arch:

sudo pacman -S openbox  

Follow this guide to make it pretty

I reccomend something lightweight so that you can run two desktop environements (intel and nvidia) at once but feel free to use any desktop you want.

Add this so that it launches when you run nvidia-xrun

The file .nvidia-xinitrc:

# ~/.nvidia-xinitrc
if [ $# -gt 0 ]; then  
    $*
else  
    openbox-session
fi  

Now you are good to go! If you haven't already, reboot. Then run nvidia-xrun in a new terminal session (not in a running DE! The shortcut for most systems is CTRL + ALT + F# where F# is 0-9. You may need to add FN) and see if openbox opens!


This is a simple command to check which GPU you are using:

glxinfo|egrep "OpenGL vendor|OpenGL renderer*"  

With intel, I get the following:

[robert@rawlinux ~]$ ./gpu.sh 
OpenGL vendor string: Intel Open Source Technology Center  
OpenGL renderer string: Mesa DRI Intel(R) HD Graphics 630 (Kaby Lake GT2)  

And with nvidia

[robert@rawlinux ~]$ ./gpu.sh
OpenGL vendor string: NVIDIA Corporation  
OpenGL renderer string: GeForce 1050/PCIe/SSE2  

Now time to benchmark it! ;)

Credits

This article was adapted from the arch wiki. Here is the article

However it seems to be outdated as it is reccomending bbswitch and following it broke my xorg install on my laptop (as of writing this article). I'm suggesting changes there. I've also tried to make the process easier to follow. Again, if you have any questions comment down below.