Nvidia Optimus on Ubuntu 12.04

Nvidia Optimus is a neat solution to the problem of power consumption vs. 3D performance in notebook computers – the computer comes with two graphics chips, an “integrated graphics package” – the Intel GMA embedded graphics and a “discrete graphics package” – the Nvidia chip. The setup works by running your normal windowing UI on the integrated graphics, only powering on the discrete graphics when you want to play a 3D game or something like that.

Recently I had the fortune to work on a Lenovo T420 laptop1 that has this setup, and it works very well on MS-Windows with the Nvidia Optimus driver – you get battry life around 10 hours with the 9 cell battery.

Unfortunately I spend most of my day in Linux and because Nvidia does not support Optimus on Linux, you have the poor choice of either running everything on the Nvidia chip – decreasing battery life to around 4 hours if you’re careful, or disabling the Nvidia chip completely.

The solution comes from the Bumblebee project – a software suite to handle the switching between the Nvidia discrete graphics and the embedded Intel chip.

The setup is pretty simple to understand (though I suspect under the hood there are many problems to be solved): A service runs and waits for users to ask for 3D accelerated graphics. When a user starts a program using the special command optirun, the service loads the Nvidia driver, starts an X server using the discrete graphics (with the display disconnected from the actual screen) and runs the specified program on that “background” X server. Then it copies the visuals from the program that is rendered using the discrete graphics to a window on the main X server. When the program terminates, the service closes the secondary X server, removes the driver and powers down the graphics card – putting us back into the ~10 hours battery life.

The Bumblebee software had some problems in the past, but the current version – 3.0 – looks very good. There are a few seconds of delay when you launch the application (setting up the driver and X takes some time), but performance is about what you’d expect when running directly on the hardware. All this without any configuration – that is if you are running on the stable Ubuntu version.

As I can’t leave well enough alone, and whenever someone says “alpha”, I say – “I wants”, I’m running the current Ubuntu 12.04 alpha (which is not so bad – due to be released in a couple of months, it works very well). And of course Bumblebee doesn’t work properly here.

So this is what I had to do to get it running:

These instructions assume you are doing everything in a command line. If you are not comfortable with the command line, this guide is not for you – go away.

  1. We will start by installing Bumblbee itself and its dependencies. We will need to add their PPA2 to the Ubuntu software sources list:
    sudo add-apt-repository ppa:bumblebee/stable
    Unfortunately the project has a repository for Ubuntu 12.04, but at this point in time it does not include all the needed packages. I had to edit the source list file and add the 11.10 repositories as well before installation could complete – open the file /etc/apt/sources.list.d/bumblebee-stable-precise.list for editing as root, for example:
    sudo gedit /etc/apt/sources.list.d/bumblebee-stable-precise.list
    and duplicate the two lines found there, changing the word precise to oneiric in the duplicates. Save and exit the editor, then run these:
    sudo apt-get update
    sudo apt-get install bbswitch-dkms bumblebee bumblebee-nvidia virtualgl-libs:i386 libgl1-mesa-glx:i386 libc6:i386
    sudo usermod -a -G bumblebee $USER

    The last line adds the current user (the one you are logged in as) to the “bumblbee” user group – only users who are part of this group may use the Bumblebee service. If you have more users that you want to be able to play 3D games, then you need to add them as well.
  2. The Bumblebee Project recommends Ubuntu users to install the “X-Swat” x-updates PPA, but this isn’t available for 12.04 users, and we are already running the latest X anyway, right? After bumblebee didn’t work for me, I went to the next best thing – the xorg-edgers repository. A word of warning – this is a set of highly experimental software – if it works for you, count yourself lucky and if it doesn’t – don’t complain. To install it, type:
    sudo apt-add-repository ppa:xorg-edgers/ppa
    sudo apt-get update
    sudo apt-get dist-upgrade -y
    At this point most likely your kernel and other important software packages have been upgraded, so its a good idea to reboot.
  3. After you come back from the reboot, the standard Nvidia driver will not work, because of an incompatible ABI3 version. Not to worry, its time to recompile a new Nvidia driver. Start by removing your old one with:
    sudo apt-get purge nvidia-current
    Now to get the most up to date Nvidia driver package to rebuild a driver that works with your latest Xorg and kernel, create a new directory to hold the Nvidia package sources and change into it, and type:
    sudo apt-get install build-essential checkinstall -y
    sudo apt-get build-dep nvidia-current-updates -y
    apt-get source nvidia-current-updates
    Now with the source downloaded and all dependencies installed, we can build a new driver and install it:
    cd nvidia-graphics-drivers*
    dpkg-buildpackage -us -uc -nc
    cd ..
    sudo dpkg -i nvidia-current-updates_*.deb
    If all went well, you have your new Nvidia driver instaled. Lets see if it loads Ok:
    sudo modprobe nvidia_current_updates
    If you get no error, then we can go to the next step.
  4. The Bumblebee configuration must be modified to work with all this cutting edge stuff, so lets start by turning off the Bumblbee service:
    sudo stop bumblebeed

    Now you need to edit the Bumblebee configuration file. Use your favorite text editor to open /etc/bumblebee/bumblebee.conf as root, for example:
    sudo gedit /etc/bumblebee/bumblebee.conf
    . This is a standard INI filewith a lot of comments to help you figure out what is going on in here, and we will need to do some changes to (a) disable auto-detection and force Nvidia mode and (b) help Bumblebee locate the drivers for the Nvidia graphics. Make these changes:

    • In the [bumblebeed] section, set Driver=nvidia
    • In the [driver-nvidia] section, set KernelDriver=nvidia_current_updates and in both LibraryPath and XorgModulePath change nvidia-current to nvidia-current-updates.

    Save the file after all the changes have been complete, and start the service again:
    sudo start bumblbeed

  5. At this point, Bumblebee should be ready to do some damage. The recommended way to test it is to run
    optirun glxspheres
    Give it a few seconds to launch the window (the screen may black out for a bit, don’t worry about it). When the window appears, it should show a graphical demo and a frame per seconds counter at the bottom left corner. On my system it shows a respectable 86 frames per seconds (vs 2 frames per seconds on the embedded Intel chip). If the glxspehere window doesn’t come up, and you get errors from the optirun command, look in the system log file /var/log/syslog for lines from bumblebeed. Also if you haven’t rebooted your computer since you built the Nvidia drivers, it might be a good idea to try that as well.

Running games with optirun is pretty straight forward – either invoke them manually or edit their .desktop files to add the optirun command. I’ve also used this in MS-Windows games running under wine (I recommend using PlayOnLinux – its a beautiful piece of work), and they work perfectly well.

Best of luck, and let me know if it works or if it doesn’t.

Enhanced by Zemanta
  1. with an amazing 1600×900 screen, what they call “HD+”. Truly a work of art []
  2. Private Package Archive []
  3. Application Binary Interface []

96 Responses to “Nvidia Optimus on Ubuntu 12.04”

  1. d:

    I followed these instructions, and it did not work. Please follow the instructions over at bumblebee. (TOD IS RIGHT!) Now I have 120fps with nvidia and 60fps with the intel one.

  2. Jorge:

    This guide worked very well for me. I’m running LTS 12.04.1 and only had to track down a couple of dependency issues during the upgrade step (step 2 there). Other than that I got my laptop working well with the bumblebee service. Thank you, very detailed and easy to follow!

  3. Kasper:

    Just, in case someone stumbles along here, I made a script to prefix the exec in .desktop files with optirun. Less of a hassle imo.


  4. Pandu Aji Wirawan:

    iam using Lenovo G480 with NVIDIA G610M, when i try to user grep vga, my ubuntu only detect intel vga, nvidia not detected. i’ve try to search in askubuntu, ubuntuforum, etc but i am not found the answer, can you help me? or any sugestion link?

  5. Ashrith:

    I have a Thinkpad T520 and I am running ubuntu on it. For some reason I get good battery life if I am using integrated card. If I use the Optimus or Discrete then the battery life does not exist beyond two hrs. Any ideas?

    • Oded:

      Yes, that sounds exactly as it should work, I have slightly better performance – something like 2.5 hours on Nvidia. That’s the whole reason for the Optimus technology – it is supposed to allow you to choose whether you want great battery life of 3D performance, by having the Intel integrated graphics handle all the standard work, which is very energy efficient, and when you want 3D performance you start the Nvidia discrete graphics and should connect the laptop to power immediately.

      The purpose of the Bumblbee project that I discuss in this post is to enable this technology in Linux. When you have something running under ‘optirun’, it will enable the Nvidia graphics and will eat all the juice in the battery, and when optirun is closed, it will disable the Nvidia graphics card and stop the drain on the battery.

      Its important to note (which I failed to mention in the post) that you should disable “auto detection” of Optimus compatibility in the BIOS, otherwise the BIOS will always enable the Nvidia graphics card when running under Linux and you will not get any battery saving when using just the integrated graphics.

  6. Ashrith:

    I actually failed to mention on additional info. The 2hrs of battery life that I get under optimus or discrete mode is actually even if I am not using the Nvidia card but if I just enable Optimus. It is pretty surprising but I guess if many of them are getting around the same time then I guess it is about right. Thanks for the reply, Oded!

  7. Alfirus Ahmad:

    i’m using Lenovo G580 and facing battery drain fast and yes i do have Nvidia Geforce graphic. I’m using Ubuntu 12.10.

    I follow the first step without any problem. However

    “open the file /etc/apt/sources.list.d/bumblebee-stable-precise.list for editing as root, for example:
    sudo gedit /etc/apt/sources.list.d/bumblebee-stable-precise.list”

    There is no file named bumblebee-stable-precise.list, except bumblebee-stable-quantal.list

    • Alfirus Ahmad:

      Now i installed
      “Interface for toggling the power on nVidia Optimus video card”
      “NVidia Optimus support”
      From which step i need to start?

    • Alfirus Ahmad:

      I just reinstall Ubuntu, then i tried to install “Interface for toggling the power on nVidia Optimus video card” & “NVidia Optimus support” in Ubuntu Software Center and can’t found it.

      Then i tried sudo add-apt-repository ppa:bumblebee/stable. It went fine however when sudo gedit /etc/apt/sources.list.d/bumblebee-stable-precise.list, the list blank. Nothing.

      • Ashrith:

        If I am not mistaken for 12.10 you would have it quantal and not in precise. These names are generated based on your ubuntu version. So you should find it in that file and not in precise. This blog is written with precise in mind. So all you would be doing is following similar steps in 12.10 but all will be in Quantal. keep that in mind.

      • Alfirus Ahmad:

        Ok. now i’m stuck at

        sudo dpkg -i nvidia-current-updates_*.deb

        it shows

        dpkg: error processing nvidia-current-updates_*.deb (–install):
        cannot access archive: No such file or directory
        Errors were encountered while processing:

      • Alfirus Ahmad:

        After checking, the problem started at this command

        “dpkg-buildpackage -us -uc -nc”

        and below is the result

        cp: cannot stat `./NVIDIA-Linux-x86_64-304.51-no-compat32/nvidia-current-updates-settings.png’: No such file or directory
        dh_install: cp -a ./NVIDIA-Linux-x86_64-304.51-no-compat32/nvidia-current-updates-settings.png debian/nvidia-current-updates//usr/share/pixmaps/ returned exit code 1
        make: *** [binary-install/nvidia-current-updates] Error 2
        dpkg-buildpackage: error: fakeroot debian/rules binary gave error exit status 2

  8. Havary:

    Was a little hard to me, but it is OK. it is working with nvidia 319 beta

Leave a Reply



Spam prevention powered by Akismet