[HOWTO] Getting Nvidia Optimus to work

Share your own howto's etc. Not for support questions!

[HOWTO] Getting Nvidia Optimus to work

Postby zdomazet » 2013-12-31 13:41

A few months ago, I bought a new notebook. Since I do CUDA programming at university, the Nvidia GPU was absolutely necessary. I've spent many a sleepless night to get the GPU working properly on Debian, and finally managed to do it. Meanwhile, I also managed to make a few other glitches go away, but that's for another howto. This tutorial is a summary of my efforts to get the Nvidia graphics in this notebook working, so noone else will have to waste so much time. I have tested it only on my machine, but it should work with any other notebook with similar hardware.

The notebook is a not so old Asus N76VZ model. I am using Debian Jessie, with non-free and contrib repositories enabled in apt. System information is as follows:

Code: Select all
# lspci | grep -i vga
00:02.0 VGA compatible controller: Intel Corporation 3rd Gen Core processor Graphics Controller (rev 09)
01:00.0 VGA compatible controller: NVIDIA Corporation GK107M [GeForce GT 650M] (rev ff)


Code: Select all
# uname -a
Linux artoo-deetoo 3.11-2-amd64 #1 SMP Debian 3.11.10-1 (2013-12-04) x86_64 GNU/Linux


UPDATE: Most of this tutorial is now obsolete. Scroll down and start reading from the green marker.

First of all, we need to install some additional packages, not necessarily related to graphics. Some of the hardware needs proprietary firmware; we will need dkms, debhelper, git and linux-headers to make the GPU work; and intel-microcode to get full CPU capabilities. As root, do:
Code: Select all
apt-get install firmware-linux-nonfree linux-headers-$(uname -r) intel-microcode git dkms debhelper


Now, let's take a look at the graphics subsystem. This notebook utilizes the Nvidia Optimus technology, which means that most of the rendering is done by the integrated Intel GPU, and the Nvidia chip is switched off for most of the time for power saving reasons. As useful that might seem, this brings an additional layer of complexity to the system. In order to use the Nvidia GPU, one must (in this order) activate the chip, load the driver, create a secondary X server on which the rendering will be performed, execute the application, terminate the secondary X server, unload the driver, and switch the GPU off. To accomplish all this, the Bumblebee Project (http://bumblebee-project.org/) was created. Those nice people made the previously mentioned procedure possible on Linux systems. However, if you just install everything from repository, you will probably end up with kernel panic every time you try to activate the Nvidia GPU. Therefore, we will need to compile and build a part of Bumblebee ourselves.

One part of Bumblebee is called bbswitch. It is a kernel module with a sole purpose of switching the Nvidia graphics chip ON or OFF. In Debian repositories you can find the bbswitch-dkms_0.7-2, but you *do not* want to install that version, as it contains a bug that makes it impossible to do anything with the Nvidia GPU. We will need to get bbswitch-0.8 source and build a kernel module ourselves. (This part has been adapted from https://wiki.kubuntu.org/Kernel/Dev/DKMSPackaging)

To get bbswitch to work, follow this procedure:
1. Clone the bbswitch git repository:
Code: Select all
[root@artoo-deetoo:~]# cd ~
[root@artoo-deetoo:~]# git clone https://github.com/Bumblebee-Project/bbswitch.git
[root@artoo-deetoo:~]# cd bbswitch


2. Move some files around, and then move the contents of this directory to /usr/src/bbswitch-0.8
Code: Select all
[root@artoo-deetoo:~/bbswitch]# cp dkms/dkms.conf .
[root@artoo-deetoo:~/bbswitch]# mkdir /usr/src/bbswitch-0.8
[root@artoo-deetoo:~/bbswitch]# cp -r * /usr/src/bbswitch


3. Next, we need to register this module with dkms.
Code: Select all
[root@artoo-deetoo:~/bbswitch]# dkms add bbswitch/0.8

Creating symlink /var/lib/dkms/bbswitch/0.8/source ->
                 /usr/src/bbswitch-0.8

DKMS: add completed.


4. Next, we need to build the module using dkms.
Code: Select all
[root@artoo-deetoo:~/bbswitch]# dkms build bbswitch/0.8

Kernel preparation unnecessary for this kernel.  Skipping...

Building module:
cleaning build area....
make KERNELRELEASE=3.11-2-amd64 KVERSION=3.11-2-amd64....
cleaning build area....

DKMS: build completed.


5. Finally, we install the module.
Code: Select all
[root@artoo-deetoo:~/bbswitch]# dkms install bbswitch/0.8

bbswitch:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/3.11-2-amd64/updates/dkms/

depmod....

DKMS: install completed.


That's it! Now you can check the kernel module status.
Code: Select all
[root@artoo-deetoo:~/bbswitch]# dkms status
bbswitch, 0.8, 3.11-2-amd64, x86_64: installed


UPDATE: The repositories now contain bbswitch-dkms 0.8. No need to build it from source any more, just do:
Code: Select all
apt-get install bbswitch-dkms


Now, as root, open the /etc/modules file, and append the following to the end of the file:
Code: Select all
i915
bbswitch


Also, as root, open the /etc/modprobe.d/nouveau.conf file, and put the following inside:
Code: Select all
blacklist nouveau


You should reboot your computer now.
After the reboot, you can check whether bbswitch can manage your GPU. /proc/acpi/bbswitch file holds that information, and also the way of manually switching the GPU on or off.
Code: Select all
[root@artoo-deetoo:~/bbswitch]# cat /proc/acpi/bbswitch
0000:01:00.0 OFF
[root@artoo-deetoo:~/bbswitch]# echo ON > /proc/acpi/bbswitch
[root@artoo-deetoo:~/bbswitch]# cat /proc/acpi/bbswitch
0000:01:00.0 ON
[root@artoo-deetoo:~/bbswitch]# echo OFF > /proc/acpi/bbswitch
[root@artoo-deetoo:~/bbswitch]# cat /proc/acpi/bbswitch
0000:01:00.0 OFF


If everything works, we can move to the next step. We need to install the proprietary driver, the Bumblebee daemon, and the primus bridge. In order to get the latest packages, we'll add the experimental repository to our sources.list. Also, if you intend to run 32 bit applications on your Nvidia chip, you will need the i386 libs.
As root, open the file /etc/apt/sources.list. My file looks like this:
Code: Select all
deb http://ftp.tu-graz.ac.at/mirror/debian/ jessie main non-free contrib
deb-src http://ftp.tu-graz.ac.at/mirror/debian/ jessie main non-free contrib

deb http://security.debian.org/ jessie/updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free

deb http://ftp.tu-graz.ac.at/mirror/debian/ jessie-updates main contrib non-free
deb-src http://ftp.tu-graz.ac.at/mirror/debian/ jessie-updates main contrib non-free


You should add something like this inside:
Code: Select all
deb http://ftp.tu-graz.ac.at/mirror/debian/ experimental main non-free contrib
deb-src http://ftp.tu-graz.ac.at/mirror/debian/ experimental main non-free contrib


Save and exit. Then, as root, run the following:
Code: Select all
[root@artoo-deetoo:~/bbswitch]# dpkg --add-architecture i386
[root@artoo-deetoo:~/bbswitch]# apt-get update
[root@artoo-deetoo:~/bbswitch]# apt-get -t experimental install bumblebee bumblebee-nvidia primus primus-libs primus-libs:i386


After that is complete, you will need to edit some configuration files. As root, open the file /etc/default/grub, find the line which says GRUB_CMDLINE_LINUX_DEFAULT (should be near the top of the file), and make it look like this:
Code: Select all
GRUB_CMDLINE_LINUX_DEFAULT="quiet rcutree.rcu_idle_gp_delay=1"

This kernel parameter is a workaround for the bug in the proprietary Nvidia drivers. That bug causes the GPU to fall off the bus on every attempt of initialization. (source: https://github.com/Bumblebee-Project/Bu ... issues/455)

Next, as root, open the file /etc/bumblebee/bumblebee.conf. Find and modify the following two lines so they appear like this:
Code: Select all
Driver = nvidia
KernelDriver = nvidia-current


All you need to do now is add yourself to the bumblebee group, so you can use the GPU. As root, do:
Code: Select all
[root@artoo-deetoo:~/bbswitch]# addgroup <your_username_here> bumblebee


Reboot. After the system is rebooted, you can try running something on your Nvidia GPU. Try:
Code: Select all
[user@artoo-deetoo:~]$ optirun glxgears


You should experience a small delay while the new X server is being initialized, and the gears should appear on the screen. Power management should also now be working properly, so you will experience an increase in battery life, too.

In order to get CUDA working, you just need to do
Code: Select all
[root@artoo-deetoo:~/bbswitch]# apt-get install nvidia-cuda-toolkit


CUDA applications should be run with optirun, too.

UPDATE: Since the repositories now contain updated versions of bumblebee and related packages, installation is quite simplified. Read on from here.

In order to get full Optimus functionality, follow these steps:

1. Install all related packages:
Code: Select all
# dpkg --add-architecture i386
# apt-get update
# apt-get install bbswitch-dkms intel-microcode firmware-linux-nonfree bumblebee bumblebee-nvidia primus primus-libs primus-libs:i386 linux-headers-$(uname -r)


Nvidia driver installer will complain about nouveau being loaded and X server not being configured, just ignore it and say "OK".

2. A bit of configuration. As root, open /etc/bumblebee/bumblebee.conf, find
Code: Select all
KernelDriver=nvidia


and change it to
Code: Select all
KernelDriver=nvidia-current


3. Add yourself to the bumblebee group:
Code: Select all
# addgroup <your_username_here> bumblebee


4. More configuration. As root, open /etc/default/grub, find GRUB_CMDLINE_LINUX_DEFAULT, and append "rcutree.rcu_idle_gp_delay=1" to it. Regenerate the boot image:
Code: Select all
# update-grub


(optional) In order to get CUDA working, you just need to do
Code: Select all
# apt-get install nvidia-cuda-toolkit


5. Reboot! Once you boot, you can use your Nvidia card by running applications with optirun:
Code: Select all
$ optirun glxgears


NOTE: I have not yet managed to do anything with the HDMI output. As soon as I get any success, I'll post it here.

I hope these instructions work for everyone! Any comments and improvements are welcome. :)

(Now, has anyone with a similar notebook managed to make the external subwoofer work properly? And/or WiFi toggle (Fn+F2)? )
Last edited by zdomazet on 2014-05-12 09:28, edited 2 times in total.
zdomazet
 
Posts: 12
Joined: 2013-10-22 20:08

Re: [HOWTO] Getting Nvidia Optimus to work

Postby bettylou » 2014-01-01 23:27

Thank you for taking the time to write this. Sooner or later I'm gonna get stuck with an Optimus laptop too.
bettylou
 
Posts: 53
Joined: 2013-06-14 22:42

Re: [HOWTO] Getting Nvidia Optimus to work

Postby blažek » 2014-01-09 22:46

Thanks for this great tutorial, everything is ok but there is one thing,
I just switched to systemd and it turns nvidia card ON by default, to disable this just add this line

Code: Select all
options bbswitch load_state=0

to /etc/modprobe.d/nouveau.conf and nvidia will be turned off by default
User avatar
blažek
 
Posts: 5
Joined: 2012-10-02 13:21

Re: [HOWTO] Getting Nvidia Optimus to work

Postby guilherme7tw » 2014-08-08 15:58

Not work for me.

I have a LG 550C (Nvidia 640M) and on Ubuntu bumblebee works fine, in Debian not. I follow the guide, but if i try
Code: Select all
optirun glxgears
i get
Code: Select all
[   90.345048] [ERROR]Cannot access secondary GPU - error: Could not load GPU driver

[   90.345167] [ERROR]Aborting because fallback start is disabled.


Any ideas ?

(Sorry for my english, i am brazilian :roll: )

Edit: I forgot to say I'm using debian jessie
guilherme7tw
 
Posts: 3
Joined: 2014-08-08 15:51

Re: [HOWTO] Getting Nvidia Optimus to work

Postby zdomazet » 2014-08-08 20:04

Could you please do (as root):
Code: Select all
modprobe nvidia-current

and post the results of that command? Also, after you do this, do
Code: Select all
lsmod |grep -i nv

and post the results.
zdomazet
 
Posts: 12
Joined: 2013-10-22 20:08

Re: [HOWTO] Getting Nvidia Optimus to work

Postby guilherme7tw » 2014-08-09 11:31

Code: Select all
root@debian:/home/tw# modprobe nvidia-current
modprobe: ERROR: could not insert 'nvidia_current': No such device


Code: Select all
lsmod |grep -i nv
No results... So, i have a problem with the driver instalation ?
guilherme7tw
 
Posts: 3
Joined: 2014-08-08 15:51

Re: [HOWTO] Getting Nvidia Optimus to work

Postby guilherme7tw » 2014-08-20 12:20

Hi guys, for register, this post http://linuxgnublog.org/nvidia-optimus-en-debian-con-bumblebee help me for put bumblebee working in my system. :D

Everything working fine now.
guilherme7tw
 
Posts: 3
Joined: 2014-08-08 15:51

Re: [HOWTO] Getting Nvidia Optimus to work

Postby Aramir » 2014-09-17 20:48

Thanks a lot ! I struggled for days to get it work under wheezy. To keep it short, I tried everything and couldn't get my drivers to work because the kernel was too old so I drivers didn't wroked and other stuff. So I finally decided to update to jessie/sid, followed your tutorial, and VOILA. It worked! never heard of the grub delay trick anywhere else. Thanks for that.

Also it might be good to add as an intro, that if you had installed nvidia drivers before (using apt, or .RUN file) you'll have to uninstall them before (by doing apt-get purge nvidia-* and removing your /etc/X11/xorg.conf and /etc/modprobe.d/nvidia-installer-disable-nouveau.conf files ) !!! Otherwise it will just crash your X server ( (EE) no screens found ).
Aramir
 
Posts: 13
Joined: 2014-08-05 19:12

Re: [HOWTO] Getting Nvidia Optimus to work

Postby wat » 2014-11-08 14:26

Hi.
There is a way to run the HDMI output. It works on nvidia drivers downloaded from nvidia.com! I don't know what's the difference, but it works. The solution works on wheezy too.
So to run HDMI output you have to uninstall debian's nvidia-driver, download driver from nvidia.com an install it. I did installation with default options - without dkms and wih i386 support (almost always enter, enter ... :D). In consequence, in /etc/bumblebee/bumblebee.conf you have to change KernelDriver=nvidia-current to KernelDriver=nvidia. Additionaly you have to change PMMethod=none (no powersaving :( ) in section [driver-nvidia]
And the last thing to do is /etc/X11/xorg.conf file. There is my xorg.conf:

Code: Select all
Section "Device"
        Identifier  "intelgpu"
        Driver      "intel"
        BusID       "PCI:0:2:0"
EndSection

Section "Device"
        Identifier  "nvidia"
        Driver      "nvidia"
        BusID       "PCI:1:0:0"
        Option      "IgnoreEDID"
        Option "ConnectedMonitor" "DFP"
EndSection

Section "Screen"
    Identifier     "laptopscreen"   
    Device         "intelgpu"
    Monitor        "Monitor0"
    DefaultDepth    24
    SubSection     "Display"
        Depth       24
    EndSubSection
EndSection

Section "Screen"
    Identifier     "extscreen"   
    Device         "nvidia"
    Monitor        "Monitor1"
    DefaultDepth    24
    SubSection     "Display"
        Depth       24
        Modes      "1920x1080"
    EndSubSection
EndSection


Section "ServerLayout"
    Identifier  "Default Layout"
    Screen "extscreen"
    Screen "laptopscreen" Leftof "extscreen"

    Option "Xinerama" "on"

    Option "AutoAddDevices" "true"
EndSection



And some comments to xorg.conf:
1. To use glx HDMI screen (nvidia card) must be the main screen as in my xorg.conf
2. If you have xinerama on, some programs do not work (memory segfault), but
3. If you comment xinerama, you have two desktops, and glx runs perfectly on HDMI output (nvidia card).

Disandvantages:
If you do it on a laptop, you'll probably want to remove xorg.conf every time, you disconnect HDMI monitor :( and if you'd like to save power (work on battery) you need to change PMMethod. The best way is to write simple script to automatically change it ;D
wat
 
Posts: 1
Joined: 2014-11-08 13:30

Re: [HOWTO] Getting Nvidia Optimus to work

Postby achuthpv » 2015-01-27 18:01

Hi,

Thanks for the nice and pretty well explained tutorial.
I have a query.
Will this method work if we need to use Nvidia non free driver from Nvidia.com ?
I tried once using the latest driver from nvidia.com and not installing the bumblebee-nvidia package (setting KernerDriver as nvidia (driver from nvidia.com) instead of nvidia-current in bumblebee.conf). I was able to use bbswitch. But I was not able to run 'optirun glxgears' using this method, resulting in an error similar to

[ 90.345048] [ERROR]Cannot access secondary GPU - error: Could not load GPU driver

[ 90.345167] [ERROR]Aborting because fallback start is disabled.

:|


Later I installed the bumblebee-nvidia package after uninstalling non-free package from nvidia.com, and changed the KernelDriver to nvidia-current. Then as per the steps given by you, 'optirun glxgears' worked. :D

Later I saw that, in /etc/modprobe.d/bumblebee.conf, it is black listing the 'nvidia' driver from nvidia.com. I am not sure whether this might have caused the failure of the 'optirun glxgears'.

Well the point that I am trying to make is, if the method that I proposed works, wouldn't it be better using the driver from nvidia.com rather than using the nvidia driver from debian along with bumblebee? :?

Thanks
achuthpv
 
Posts: 1
Joined: 2015-01-27 17:39

Re: [HOWTO] Getting Nvidia Optimus to work

Postby lucasbretana » 2016-10-14 17:48

After I running your tutorial I keep getting an error:
Using
Code: Select all
 primusrun glxgears

Code: Select all
primus: fatal: Bumblebee daemon reported: error: [XORG] (EE) /dev/dri/card0: failed to set DRM interface version 1.4: Permission denied


And with
Code: Select all
 optirun glxgears

Code: Select all
[  398.258291] [ERROR]Cannot access secondary GPU - error: [XORG] (EE) /dev/dri/card0: failed to set DRM interface version 1.4: Permission denied

[  398.258316] [ERROR]Aborting because fallback start is disabled.


Any suggestions?
lucasbretana
 
Posts: 1
Joined: 2016-10-14 17:43

Re: [HOWTO] Getting Nvidia Optimus to work

Postby GarryRicketson » 2016-10-14 19:27

Docs, Howtos, Tips & Tricks
Share your own howto's etc. Not for support questions!



by lucasbretana » Any suggestions?

Try starting a topic in one of the support forums, as part
of your details, you can refer to this tutorial ,..
example , " I used this tutorial at
http://forums.debian.net/viewtopic.php?f=16&t=110314 "
Then follow with the rest of your details.
If you have followed this tutorial, did you notice ?:
by zdomazet » 2013-12-31 07:41 UPDATE: Most of this tutorial is now obsolete. Scroll down and start reading from the green marker.


And did you notice, the last time anybody commented on the tutorial
was a year ago ?
Postby achuthpv » 2015-01-27 12:01


When you re-post, it might also help to include what version of Debian you are
using, as well.
User avatar
GarryRicketson
 
Posts: 4117
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: [HOWTO] Getting Nvidia Optimus to work

Postby Rhuks » 2016-11-29 23:59

Excellent post! Finally I have my gt540 working. Yeah, It's an old card (2011?) but I had lost all hope after many fail attempts (even following this thread :shock: )
Just a comment: I should install virtualgl to get all functional... so I recommend to include that package in the apt- install command.
Rhuks
 
Posts: 1
Joined: 2016-11-29 23:42


Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable