Setting Up Bumblebee on Gentoo Linux

bumblebee
By Firas Khalil Khana on 04/06/2017

Optimus support on a GNU/Linux distro has always been a hassle thanks to NVIDIA… Therefore projects were created to compensate for this powersaving feature. Enter bumblebee a project aimed to replicate NVIDIA’s optimus function on GNU/Linux!.

In this article I’ll be talking about setting up a working bumblebee setup on Gentoo Linux. Worry not because setting up bumblebee on Arch Linux/Void Linux is easy. Not that it’s hard on Gentoo, but several steps are required to get a fully working bumblebee setup.

When we talk bumblebee, several things should come to one’s mind:

  1. A laptop with 2 GPUs an iGPU (integrated intel gpu) and a dGPU (discrete NVIDIA gpu).
  2. A kernel that supports loading and unloading modules.
  3. A proper /etc/portage/make.conf that works with this setup.
  4. The packages bumblebee, primus and bbswitch must be installed.
  5. Your user must be added to the following groups video and bumblebee.
  6. You should enable the service bumblebee.
On Gentoo, extra steps are required to get bumblebee working, but guess what, you won’t be going back to Arch Linux ever again because bumblebee on Gentoo is hard to setup.

Before we get started, I want to point out that bumblebee is a fairly old project (last release was on 26 April 2013), but it still works perfectly. Until NVIDIA decides to make optimus working properly on GNU/Linux, you should be using bumblebee.

1- Checking if Your Laptop Uses NVIDIA Optimus


This is pretty much straightforward. If you have an intel processor (2nd Gen +) and a NVIDIA gpu, then you probably have optimus on your laptop. For those who want to check fire up your favorite terminal and run:

lspci -k
...
00:02.0 VGA compatible controller: Intel Corporation 4th Gen Core Processor Integrated Graphics Controller (rev 06)
	Subsystem: Toshiba America Info Systems 4th Gen Core Processor Integrated Graphics Controller
	Kernel driver in use: i915
01:00.0 3D controller: NVIDIA Corporation GK208M [GeForce GT 740M] (rev ff)
...


In my case I’m using an Intel HD4600 (cpu is 4th Gen Core i7 4700MQ) as my iGPU and my dGPU is a NVIDIA GT740M.
Don’t worry if your drivers in use (for now) are different, in my case I have built-in modules and my NVIDIA card is OFF (NVIDIA driver isn’t loaded at boot which means bumblebee is working perfectly).

As you can see, optimus laptops have VGA compatible controller which is the integrated intel card that manages the display and uses the i915 kernel driver, and a 3D controller which is the stronger NVIDIA card which is going to be used when needed via bumblebee.

If your GNU/Linux experience was awful so far because your laptop was on fire 247, then you probably have an optimus laptop that needs correct configuration.


2- Checking if Your Kernel Supports Loading/Unloading Modules


In order for bumblebee to work, then bbswitch must be loaded at boot time and nvidia mustn’t.

Make sure that CONFIG_MODULES=y and CONFIG_MODULE_UNLOAD=y are enabled.

3- Optimizing your ‘/etc/portage/make.conf’


Now make sure that you have the following in your /etc/portage/make.conf:

vim /etc/portage/make.conf
...
VIDEO_CARDS="intel i965 nvidia"
...


In most cases using i965 should be fine, however if you were using an older card then please check the following Intel Feature Support on Gentoo Linux.

Now sync and update your system:

emerge --sync && emerge -avuDN @world


Now simply reboot your system, and continue this article.

shutdown -r now

4- Installation


For the installation part, we need 3 packages, bumblebee,primus and bbswitch. It’s recommended to download the live (git or -9999) versions of the previous packages to ensure that they work well with recent kernel and nvidia-drivers versions.

Fire up your favorite text editor, and make sure your /etc/portage/package.accept_keywords -(which in my case is a single file and not a directory)- contains the following:

=sys-power/bbswitch-9999 **
=x11-misc/bumblebee-9999 **
=x11-misc/primus-0.2 ~amd64


Now simply run:

emerge -av bbswitch primus bumblebee

5- Adding Your User to the Groups video and bumblebee


After installing the previous packages, add your USER to the groups video and bumblebee:

gpasswd -a USER video && gpasswd -a USER bumblebee


Don’t forget to replace USER with your real username.

Now logout and relogin and your user should be in these groups.


6- Modifying /etc/init.d/bumblebee


Here’s where it gets tricky, this file is the one responsible for the hassle. Notice at the beginning of the file it depends on xdm and vgl and we don’t need both of them for our setup to work.

You see it depends on xdm to make sure that bumblebee isn’t started if there’s no Xorg server installed. As for vgl, then we’re using primus which is much better performance wise.

Open a terminal emulator and edit /etc/init.d/bumblebee with your favorite editor (vim is my favorite editor now), and delete the first 5 lines (or their equivalent; the depend() part) as shown below:

vim /etc/init.d/bumblebee
depend() {
    need xdm
    need vgl
	after sshd
}

PIDFILE="${PIDFILE:-/var/run/bumblebee.pid}"

start() {
	ebegin "Starting BumbleBee Daemon"
		start-stop-daemon -S -p "${PIDFILE}" -x /usr/sbin/bumblebeed -- -D ${BUMBLEBEE_EXTRA_OPTS} --pidfile "${PIDFILE}"
	eend $?
}

stop() {
	ebegin "Stopping BumbleBee Daemon"
		start-stop-daemon -K -p "${PIDFILE}" -R SIGTERM/10
	eend $?
}


So your file should look like this:

PIDFILE="${PIDFILE:-/var/run/bumblebee.pid}"

start() {
	ebegin "Starting BumbleBee Daemon"
		start-stop-daemon -S -p "${PIDFILE}" -x /usr/sbin/bumblebeed -- -D ${BUMBLEBEE_EXTRA_OPTS} --pidfile "${PIDFILE}"
	eend $?
}

stop() {
	ebegin "Stopping BumbleBee Daemon"
		start-stop-daemon -K -p "${PIDFILE}" -R SIGTERM/10
	eend $?
}

7- Modifying /etc/bumblebee/bumblebee.conf


We need to change some of the default settings that bumblebee uses, start that terminal emulator and with your favorite editor (vim), edit the file /etc/bumblebee/bumblebee.conf:

# Configuration file for Bumblebee. Values should **not** be put between quotes

## Server options. Any change made in this section will need a server restart
# to take effect.
[bumblebeed]
# The secondary Xorg server DISPLAY number
VirtualDisplay=:8
# Should the unused Xorg server be kept running? Set this to true if waiting
# for X to be ready is too long and don't need power management at all.
KeepUnusedXServer=false
# The name of the Bumbleblee server group name (GID name)
ServerGroup=bumblebee
# Card power state at exit. Set to false if the card shoud be ON when Bumblebee
# server exits.
TurnCardOffAtExit=false
# The default behavior of '-f' option on optirun. If set to "true", '-f' will
# be ignored.
NoEcoModeOverride=false
# The Driver used by Bumblebee server. If this value is not set (or empty),
# auto-detection is performed. The available drivers are nvidia and nouveau
# (See also the driver-specific sections below)
Driver=nvidia
# Directory with a dummy config file to pass as a -configdir to secondary X
XorgConfDir=/etc/bumblebee/xorg.conf.d

## Client options. Will take effect on the next optirun executed.
[optirun]
# Acceleration/ rendering bridge, possible values are auto, virtualgl and
# primus.
Bridge=primus
# The method used for VirtualGL to transport frames between X servers.
# Possible values are proxy, jpeg, rgb, xv and yuv.
VGLTransport=rgb
# List of paths which are searched for the primus libGL.so.1 when using
# the primus bridge
PrimusLibraryPath=/usr/lib/primus:/usr/lib32/primus
# Should the program run under optirun even if Bumblebee server or nvidia card
# is not available?
AllowFallbackToIGC=false


# Driver-specific settings are grouped under [driver-NAME]. The sections are
# parsed if the Driver setting in [bumblebeed] is set to NAME (or if auto-
# detection resolves to NAME).
# PMMethod: method to use for saving power by disabling the nvidia card, valid
# values are: auto - automatically detect which PM method to use
#         bbswitch - new in BB 3, recommended if available
#       switcheroo - vga_switcheroo method, use at your own risk
#             none - disable PM completely
# https://github.com/Bumblebee-Project/Bumblebee/wiki/Comparison-of-PM-methods

## Section with nvidia driver specific options, only parsed if Driver=nvidia
[driver-nvidia]
# Module name to load, defaults to Driver if empty or unset
KernelDriver=nvidia
PMMethod=bbswitch
# colon-separated path to the nvidia libraries
LibraryPath=/usr/lib64/opengl/nvidia/lib:/usr/lib/opengl/nvidia/lib
# comma-separated path of the directory containing nvidia_drv.so and the
# default Xorg modules path
XorgModulePath=/usr/lib64/opengl/nvidia/lib,/usr/lib64/opengl/nvidia/extensions,/usr/lib64/xorg/modules/drivers,/usr/lib64/xorg/modules
XorgConfFile=/etc/bumblebee/xorg.conf.nvidia


I’ve highlighted the lines that you should check, just make sure that:

KeepUnusedXServer=false
Driver=nvidia
Bridge=primus
VGLTransport=rgb (for vgl users)
KernelDriver=nvidia
PMMethod=bbswitch


8- Enabling and Starting bumblebee Service


Simply add the service bumblebee to the runlevel default:

rc-update add bumblebee default


Now simply reboot and you should be good to go!

shutdown -r now

(Optional) Checking if bumblebee is Working


If you’ve followed along with this article then your bumblebee setup should be working 100%. I recommend using primusrun instead of optirun since optirun tends to spit out more errors than primusrun (but after our configuration they should both be using the primus bridge and provide similar performance.

To test our bumblebee configuration, install the package mesa-progs:

emerge -av mesa-progs


Now before testing anything the NVIDIA card should be OFF, and the nvidia module shouldn’t be loaded, instead the module bbswitch should be loaded. To double check fire up that terminal emulator and run:

lsmod
Module                  Size  Used by
bbswitch                5461  0


then run:

cat /proc/acpi/bbswitch
0000:01:00.0 OFF


As you can see:
1- bbswitch is loaded
2- nvidia isn’t loaded
3- card is OFF as reported by bbswitch

Now let’s check if the NVIDIA card will be switched ON and the nvidia module will be loaded once optirun/primusrun are executed, and if it’ll switch OFF and the nvidia module will be unloaded once optirun/primusrun finish executing.

Inside your terminal emulator (and while you’re running in a Xorg session… obviously…) run:

optirun glxgears


or (but not both):

primusrun glxgears


A window showing glxgears should open. While it’s running check the following:

lsmod
Module                  Size  Used by
nvidia              10652360  51
bbswitch                5461  0


and:

cat /proc/acpi/bbswitch
0000:01:00.0 ON


Notice how the nvidia module got loaded and the card switched on. Now end the running glxgears and do a simple check, the card should be OFF and the nvidia module should be unloaded and bbswitch will stay loaded.

Some of you may say that using vgl gets me more fps when running glxgears or any benchmark application. That is simply not the case as vgl doesn’t adjust itself to the screen’s refresh rate like primus does. Try running the following and see how primus crushes vgl:

vblank_mode=0 primusrun glxgears


If your card was refusing to turn OFF and the nvidia module is still loaded upon finishing the execution, then run the following:

rmmod nvidia && echo "OFF" >> /proc/acpi/bbswitch

(Optional) USE Flags


For those of you that are wondering what USE flags I’m using for my packages:

For nvidia-drivers(notice how I disabled uvm and kms USE flags, as they can lead to errors when unloading the nvidia module unless you’re using a patched version of bumblebee):

equery u nvidia-drivers
* Found these USE flags for x11-drivers/nvidia-drivers-381.22:
 U I
 + + X           : Install the X.org driver, OpenGL libraries, XvMC libraries, and VDPAU libraries
 + + acpi        : Add support for Advanced Configuration and Power Interface
 + + compat      : Install non-GLVND libGL for backwards compatibility
 + + driver      : Install the kernel driver module
 + + gtk3        : Install nvidia-settings with support for GTK+ 3
 - - kms         : Enable support for kernel mode setting (KMS)
 - - pax_kernel  : PaX patches from the PaX project
 - - static-libs : Build static versions of dynamic libraries as well
 + + tools       : Install additional tools such as nvidia-settings
 - - uvm         : Install the Unified Memory kernel module (nvidia-uvm) for sharing memory between CPU and GPU in CUDA programs
 - - wayland     : Enable dev-libs/wayland backend


For xf86-video-intel:

equery u xf86-video-intel
* Found these USE flags for x11-drivers/xf86-video-intel-2.99.917_p20170313:
 U I
 - - debug : Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see
             https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces
 + + dri   : Enable direct rendering: used for accelerated 3D and some 2D, like DMA
 + + dri3  : (Restricted to <=x11-drivers/xf86-video-intel-2.99.917_p20160621)
             Enable DRI3 support
 + + sna   : Enable SandyBridge's New Acceleration (useful on all chipsets, not just SandyBridge)
 - - tools : Build the intel-virtual-output tool
 + + udev  : Enable virtual/udev integration (device discovery, power and storage device support, etc)
 - - uxa   : Enable UMA Acceleration Architecture
 + + xvmc  : Enables X-Video Motion Compensation support


For bumblebee:

equery u bumblebee
* Found these USE flags for x11-misc/bumblebee-3.2.1:
 U I
 + + bbswitch            : Add dependency on sys-power/bbswitch for PM feature
 - - video_cards_nouveau : VIDEO_CARDS setting to build reverse-engineered driver for nvidia cards
 + + video_cards_nvidia  : VIDEO_CARDS setting to build driver for nvidia video cards

Conclusion


Congratulations! No need to go back to Arch Linux now! You have a working bumblebee configuration on Gentoo Linux. One important thing to note though is that you may get some errors regarding the secondary gpu not starting or some segfaults, simply wait a few seconds and rerun what you’re executing and it’ll run.

Just keep in mind that since the card is being turned ON and OFF, you may want to wait a few seconds before and after the execution of any code using bumblebee to prevent errors from popping up.

Another important thing to keep note of is that whenever you do a major change in your kernel configuration remember to recompile the nvidia-drivers (and the kernel OFC :P), otherwise expect a ton of errors.

Thanks for reading the article! Have a great day!

18 Comments

Drakkar

09/07/2017

Hi. I am Spanish and I am writing from the Google translator, I hope it is understood well. I followed your tutorial and I never turned off the Nvidia card, it always stays on. “cat /proc/acpi/bbswitch 0000:01:00.0 ON “ E followed the tutorial step by step. And the only way I have to shut down my Nvidia is this way “rmmod nvidia rmmod nvidia_modeset tee /proc/acpi/bbswitch <<< OFF “ Even without having switched it on before, bbswitch does not seem to be working. If you could lend me a hand I would be grateful. regards

DOTSLASHLINUX

09/07/2017

@Drakkar, Hey there, thanks for reading the article above. Yes, I completely understood what you said. As I mentioned above, setting up bumblebee on Gentoo GNU/Linux can really be a hassle. Looks like nvidia_modeset is being loaded along side nvidia, this may prevent bbswitch from correctly turning off the card after boot and will definitely prevent bumblebee from turning off the card using bbswitch after using optirun unless you’re using a patched version of bumblebee. I listed my USE flags above and I recommend that you disable the kms USE flag for nvidia-drivers package and re-emerge it again. Disabling kms should remove the nvidia-modeset kernel module. Another thing to do would be to double check if bumblebee -the service- is actually running or not. Might as well confirm that the USE flags on your packages match the ones listed in the article above. Hope this helps! Best of luck!

Drakkar

12/07/2017

Hi. I compile with the USE that you have in the manual, but there is no way, do not turn off nvidia, I skipped no manual step, bbswitch does not seem to work

DOTSLASHLINUX

12/07/2017

@Drakkar, sorry to hear that. Can you share your dmesg output?

Drakkar

13/07/2017

Hello Goodnight. Sorry for the delay in passing the dmesg exit and for being these hours so late, but only now. Thank you very much for your attention and interest in helping me. I leave you the file with the output dmesg here.

DOTSLASHLINUX

13/07/2017

@Drakkar, that dmesg output doesn’t look so good. I suggest you stick around here until the kernel configuration guide comes out. For now here’s what you can do.

1- Disable “CONFIG_VGA_SWITCHEROO”, we’re already relying on bbswitch so there’s no need for vga_switcheroo.

2- Looks like you’re using more than one driver for you nvidia card which is creating conflicts, I suggest that you disable “CONFIG_DRM_NOUVEAU” and blacklist it’s driver. The nvidia driver that you’re supposed to use for this setup to work is “nvidia.ko” provided by the package “nvidia-drivers”.

3- You should also disable “nvidiafb” the only framebuffer kernel config that you should be using is “CONFIG_DRM_FBDEV_EMULATION”.

Keep me updated after you try the suggestions listed above. Best of luck!

Drakkar

16/07/2017

Having done what you recommend, it seems that now it works for me and now bbswitch seems to be working, my nvidia card goes out. I’m going to follow your web closely, it helped me a lot with bumblebee and with the guide intel-ucode, congratulations for your great work and thank you very much. Greetings from the Canary Islands

DOTSLASHLINUX

16/07/2017

@Drakkar, you’re welcome! Glad that it worked for you! Let me know whenever you need any help. Thanks for your feedback!

Michael Mounteney

23/07/2017

I’d be obliged if you could look over my configuration. I think I’ve done everything as you said. Mine is a x86_64 build on a Lenovo W520. When I start lightdm, for example, the system switches to another virtual console but never leaves text-mode (the blinking cursor never disappears). Relevant output: (1) lspci -v https://pastebin.com/g4GDD21J (2) dmesg https://pastebin.com/Fy2WsuFZ (3) build use flags for nvidia-drivers https://pastebin.com/4CLDvPVa (4) build use flags for xf86-video-intel https://pastebin.com/ZnPM9MmB (5) build use flags for bumblebee https://pastebin.com/J8n0zqBB (6) Xorg.0.log https://pastebin.com/WAMACsNR (7) lsmod https://pastebin.com/AcLuyYq9 . I had ‘modprobe nvidia’ which accounts for the appearance of that driver in the lspci and lsmod output

DOTSLASHLINUX

23/07/2017

@Michael Mounteney, thanks for visiting the website. After reading through the log files you provided, it seems that bbswitch doesn’t want to switch the nvidia card OFF leaving the card always ON (in D0 power state). I’m not sure I fully understood the problem you’re going through. Is it related to bumblebee or lightdm? Is the card OFF once you boot your system? Is the nvidia driver being loaded automatically upon booting? Can you turn off your card manually as suggested in the article? Is the problem only with lightdm? Hopefully we can figure something out once I fully understand what the problem is. I also noticed that you enabled both uxa and sna USE flags on xf86-video-intel. It’d be better if you disabled the uxa USE flag and check if you’re using sna as your acceleration method as it may have something to do with your problem. Thanks for passing by! Have a great day!

Michael Mounteney

23/07/2017

Sorry for re-posting. I didn’t appreciate that my OP would only appear when you reviewed it. Thank you for the answer.

DOTSLASHLINUX

23/07/2017

@Michael Mounteney, oh don’t you worry about it. Comments are being monitored to prevent spam, so it may take a while before your comment appears.

Michael Mounteney

24/07/2017

Thank you for the reply.

Here are two more relevant files: (8) /etc/bumblebee/bumblebee.conf https://pastebin.com/SjWZmK wV (9) kernel .config https://pastebin.com/gvs5ES .

I removed use flag uxa and rebuilt.

After rebooting, /proc/acpi/bbswitch contains “0000:01:00.0 OFF”

If I run “primusrun X”, the output is as per Xorg.0.log in https://pastebin.com/WAMACsNR includi ng the “no screens found(EE)” line.

After running “primusrun X” /proc/acpi/bbswitch still contains “0000:01:00.0 OFF” so it seems th at the nvidia driver is not being loaded.

Kernel module “nvidia_drm” is loaded; is that significant?

DOTSLASHLINUX

24/07/2017

@Michael Mounteney, primusrun X? Are you trying to run a Xorg session on your discrete GPU? I’d suggest checking eselect opengl list and switching it to xorg-x11. Another thing I’d recommend is test older or stable versions of both the kernel and nvidia-drivers. I had problems getting the kernel and nvidia-drivers to work with bbswitch on the ~amd64 unstable branch , but it’s working fine on the amd64 stable branch (kernel version 4.9.34, nvidia-drivers version 381.22). You might want to disable runtime power manager as well for the nvidia card such as TLP/powertop and see how things go. Oh and the link to the kernel .config you shared is broken. It’d be better as well if you clarified what the problem is because it’s still vague to me, and perhaps mention the steps required to recreate it. Thanks for stopping by! Have a great day!

peedub

16/08/2017

@DOTSLASHLINUX, Great job documenting the process! I have run into a similar issue as to what @drakkar had. On boot bumblebee is running but the card is on and i have the 3 nvidia modules loaded “nvidia nvidia_modeset nvidia_drm”. I’ve compiled nvidia-drivers with USE=“-kms” but the module is still being loaded and I was under the impression that getting rid of the kmd use flag would get rid of the modeset module but its not seaming to.. nvidia-drivers-384.59-r1. If I rmmod the modules after boot and turn off the card that works. also the card is turned back on when running primusrun glxgears but is not turned back off after exiting due to having the nvidia_modeset module loaded again. I’m pretty sure my solution here is to blacklist the nvidia_modeset module but haven’t given it a shot to verify if in fact this fi xes the issue or causes yet another. any ways, thanks and cheers for the docs so far, great work!! keep em coming!

DOTSLASHLINUX

17/08/2017

@peedub, thanks for your reply! Indeed, the problem is from nvidia_drm and nvidia_modeset being loaded at boot time. Unless bumblebee and bbswitch were patched to prevent these 3 from loading (which is unlikely), the only way to do that is by disabling the kms USE flag. The reason that they’re still being loaded on your end is that the modules were previously built for your kernel so you have to remove them manually or remove the /lib/modules/linux* dir and build the kernel and nvidia-drivers and bbswitch again. That can be troublesome for some users, but give it a shot along with the blacklisting and tell me how things go! Glad you liked the article!

VICTORGUERRAVELOSO

19/08/2017

@DOTSLASHLINUX Thanks for the Tutorial. Finally i can use Gentoo on my notebook. I`ve been stuck on Arch since the last year cause i couldn’t figure out what i was doing wrong. I followed your steps exactly as you said, and it’s working!

DOTSLASHLINUX

20/08/2017

@VICTORGUERRAVELOSO Thanks for your stopping by, glad that it worked for you!

Leave A Comment

Recommended Distros

Upcoming Articles

  • The Linux Kernel Configuration Guide Part 1
  • The Linux Kernel Configuration Guide Part 2
  • The Linux Kernel Configuration Guide Part 3
  • The Linux Kernel Configuration Guide Part 4
  • The Linux Kernel Configuration Guide Part 5
  • The Linux Kernel Configuration Guide Part 6
  • The Linux Kernel Configuration Guide Part 7

Suggestions

Apart from the comments' section, if you wanted to see an article on how to configure a certain application for a GNU/Linux distro, or wanted to share one (perhaps one that you've written), feel free to send me an email on:

firasuke@gmail.com