~hristoast/hristoast

ref: 6c6b610011fd9b58d5c2baf47345899ba5ef360d hristoast/site/using-my-laptops-nvidia-gpu-on-linux.html -rw-r--r-- 8.7 KiB
6c6b6100Hristos N. Triantafillou Proofreading edits 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<h1 id="title">Using My Laptop's Nvidia GPU On Linux</h1>

<div id="dates">
  <span>Posted: <time id="post-date">2020-04-05</time></span>
</div>

<p id="post-excerpt">
  As far as I know, official support for primus functionality on Linux is somewhat limited.  I recall reading about some sort of official support from Nvidia themselves, but now that I look for evidence of this I'm unable to find it.  As an alternative, I looked to using nvidia-xrun in order to more directly utilize the nvidia GPU inside my laptop.  I've used primusrun, bumblebee, and friends before and they just aren't as easy to work with as this in my opinion - but most importantly, performance was lacking. <span class="footnote"><a href="https://github.com/Witko/nvidia-xrun">https://github.com/Witko/nvidia-xrun</a></span> <span class="footnote"><a href="https://www.bumblebee-project.org/">https://www.bumblebee-project.org/</a></span>  Read on as I discuss how I did this, with Void Linux package recipes.
</p>

<div id="toc"></div>

<h3>Seamless Or Not</h3>

<p>
  The main advantage to using a setup like bumblebee (or, presumably the officially supported method that I've not used) is that you can run an arbitrary program and have it use the Nvidua GPU - on top of any desktop session that might be running off of the integrated GPU.  It's a bit more convenient, but can have performance issues due to various bottlenecks.  It is impressive nonetheless.
</p>

<p>
  I actually ran this setup for quite some time and it worked well enough but the performance issues (stuttering that was likely caused by the integrated GPU) and maintenance cost (after some time of not using it, I wasn't really able to get bumblebee and friends working again and didn't care to put too much time into it) made it not worthwhile to keep up with.
</p>

<h3>Into Xrun</h3>

<p>
  I don't really recall why I started looking at nvidia-xrun, or where I might have first seen it, but eventually I dusted off my altered nvidia package for void and gave it a spin.
</p>

<h4>An Altered Driver Package</h4>

<p>
  I couldn't simply use the nvidia driver package provided by my distribution's package manager, because it would overwrite things like the system <code>libGL.so</code> and others, and that would break the integrated GPU (which I still wanted to use as the main driver).
</p>

<p>
  I decided to fork the provided package template and make some small changes that would allow installing it alongside the Mesa-provided libraries.  The result is viewable on my fork of the void-packages repository , but by and large it is the same thing as the actual nvidia package with the exception that it doesn't make itself the system provider of various libraries as described above. <span class="footnote"><a href="https://github.com/hristoast/void-packages/blob/0cd9f5cea4b1221a7d69a286cef44567b60e8c86/srcpkgs/nvidia-primus/template">srcpkgs/nvidia-primus/template</a></span> <span class="footnote"><a href="https://github.com/hristoast/void-packages/blob/0cd9f5cea4b1221a7d69a286cef44567b60e8c86/srcpkgs/nvidia/template">srcpkgs/nvidia/template</a></span>
</p>

<h4>The Xrun Package</h4>

<p>
  The nvidia-xrun program itself is a bit of an oddball in that it's simply a shell script, but it calls sudo.  I don't think it's common for a package-provided program to implicitly call sudo like that, at least I'd find it a bit jarring, so this isn't something I'd expect the Void maintainers to merge without some modifications on that front.  Anyways, that template is also viewable on my fork of the void-packages repository. <span class="footnote"><a href="https://github.com/hristoast/void-packages/blob/0cd9f5cea4b1221a7d69a286cef44567b60e8c86/srcpkgs/nvidia-xrun/template">srcpkgs/nvidia-xrun/template</a></span>
</p>

<h3>Building And Installing Packages</h3>

<p>
  There's nothing special about building these, but if you don't use Void or never built a package it might not be clear just how easy it is:
</p>

<pre><code># Clone my fork of void-packages, the nvidia-primus branch has the goods:
git clone -b nvidia-primus https://github.com/hristoast/void-packages
cd void-packages

# Bootstrap or update as needed
./xbps-src binary-bootstrap  # OR: ./xbps-src bootstrap-update

# 64-bit driver package:
./xbps-src pkg nvidia-primus

# 32-bit driver package:
./xbps-src -a i686 pkg nvidia-primus

# Now, nvidia-xrun:
./xbps-src pkg nvidia-xrun

# Finally, install them:
sudo xbps-install --repository=./hostdir/binpkgs/nvidia-primus/nonfree nvidia-primus

sudo xbps-install --repository=./hostdir/binpkgs/nvidia-primus/multilib/nonfree nvidia-primus-libs-32-bit

sudo xbps-install --repository=./hostdir/binpkgs/nvidia-primus nvidia-xrun</pre></code>

<p>
  If you don't use Void Linux, you can still of course install these things on your own.
</p>

<h3>Setup</h3>

<p>
  So now you can run all kinds of things on your GPU right?  Well no, not yet.  There is of course some configuration that needs to be done first.  The required files are listed in the nvidia-xrun README, and the repo ships with some example configurations. <span class="footnote"><a href="https://github.com/Witko/nvidia-xrun/blob/af3b7349b322169f5e57ce30da760e50d94644cc/README.md#structure">README.md#structure</a></span>  Below I describe specifically which files are needed.
</p>

<h4>nvidia-xorg.conf</h4>

<p>
  Copy this to <code>/etc/X11/nvidia-xorg.conf</code> and you're all set.  Before moving on, it's worth checking that you have the right bus ID configured in <code>/etc/X11/nvidia-xorg.conf</code>.  Please see the nvidia-xrun README</a> as a reference on how to do that. <span class="footnote"><a href="https://github.com/Witko/nvidia-xrun/blob/af3b7349b322169f5e57ce30da760e50d94644cc/README.md#setting-the-right-bus-id">README.md#setting-the-right-bus-id</a></span>
</p>

<h4>nvidia-xinitrc</h4>

<p>
  No changes are made to this file, just create the directory <code>/etc/X11/xinit</code> if it doesn't exist and copy the file from the repo to <code>/etc/X11/xinit/nvidia-xinitrc</code>.
</p>

<h4>config/nvidia-xrun</h4>

<p>
  Last but not least, this needs to be copied to <code>/etc/default/nvidia-xrun</code>.  I didn't change anything here, but feel free to make any changes you think are appropriate for your setup.
</p>

<h4>Other Considerations</h4>

<p>
  While researching this post, I came across other resources for setting this kind of thing up.  A thread from reddit's /r/linux_gaming covers a lot of details nicely, but it claims bumblebee is needed when in fact it is not. <span class="footnote"><a href="https://old.reddit.com/r/linux_gaming/comments/6ftq10/the_ultimate_guide_to_setting_up_nvidia_optimus/">The Ultimate Guide to Setting Up Nvidia Optimus on Linux</a>; I suspect that Bumblebee was listed as needed because it may have provided a special driver package, similar to what I build above. Any Arch user: feel free to inform me!</span>  This post also confirms my observations: that nvidia-xrun yields noticeably better performance than alternative methods.
</p>

<h3>X-running Stuff</h3>

<p>
  Now it's time to finally run something on the Nvidia GPU.  First thing's first, switch to an unused TTY with a keypress such as Ctrl-Alt F5.  Log in, and invoke the <code>nvidia-xrun</code> command like this:
</p>

<pre><code># I use Openbox, feel free to run any DE you like.
nvidia-xrun openbox-session</pre></code>

<p>
  You will be prompted for a sudo password as various system-level changes are made to enable running the Nvidia GPU. <span class="footnote"><a href="https://github.com/Witko/nvidia-xrun/blob/af3b7349b322169f5e57ce30da760e50d94644cc/nvidia-xrun#L24-L64">nvidia-xrun#L24-L64</a></span>  From here I'm given a desktop session running on the Nvidia GPU; anything I launch from here will also be on the Nvidia GPU.  You can run <code>sudo nvidia-smi</code> to verify that you're using the Nvidia GPU.
</p>

<p>
  A beautiful part of this is, my old X session that's using the integrated GPU is still chugging away.  I can press Ctrl-Alt F7 to go back to it whenever I want, and of course the Nvidia session stays available until I exit with <code>openbox --exit</code> or some other method.
</p>

<p>
  Of course, you don't have to run a desktop environment with <code>nvidia-xrun</code>; it's perfectly fine to directly launch a game, or steam, or anything else that requires an X display.  Feel free to try anything that suits your fancy.
</p>

<h3>Conclusion</h3>

<p>
  It's not a seamless experience, but it is still a decent one at the end of the day.  I can run things on the Nvidia GPU and it doen't interfere with my normal desktop too much.  And the Void packages I've fashioned make staying up to date relatively simple.  If you've got a setup like mine, with a secondary Nividia GPU, this is definitely worth trying.
</p>

<h3>Footnotes And References</h3>

<div id="footnotes"></div>