Installing NVidia CUDA in Ubuntu (Linux)

      CUDA is a parallel computing platform and programming model created by NVIDIA. It is available for almost all the popular computer operating systems. But installing and configuring it in Linux is a bit challenging, specially in computers with NVidia Optimus technology. Therefore I'm going to give a step-by-step guide on how to do that.

       First I am going to give a brief introduction on NVidia Optimus technology and Bumblebee project. If you are not interested you can directly skip to the section on "Installing CUDA".

NVidia Optimus and Bumblebee
    NVidia Optimus is a technology introduced by NVidia cooperation to reduce the power consumption of GPUs in computers, mainly for notebooks. In these computers there are two graphic adapters, a lower performance one from Intel and a higher performance one from NVidia. The objective of this technology is to use the lower performance one for normal applications and to use the higher performance one only for the apps that need higher processing capacity.

   This switching between GPUs should be done by the driver and NVidia Windows drivers support this technology. But the drivers NVidia provides for Linux platform are only designed for single GPU machines. But in computers with Optimus technology, the rendering can only be done by the intel adapter. NVidia adapter also has to go through Intel adapter for rendering works. Therefore if you install a driver which is not designed for Optimus in a computer with dual graphics, you are in a big trouble. I know this by first hand experience because I installed the Linux driver (which they provide on NVidia web site) in Ubuntu 12.04 and ended up with a very low screen resolution which couldn't be changed until I removed the driver.

     This is where the Bumblebee comes to save linux users. Bumblebee is a opensource project to introduce graphic adapter switching to the linux machines. Currently it cannot automatically switch between graphic cards, but users can do it using a special command.

If you want to know more about NVidia Optimus and Bumblebee, you can read from here

Installing CUDA

  • First run these commands to remove any NVidia drivers you already have.
    nvidia-uninstallsudo
    apt-get remove --purge nvidia*

  • Install Bumblebee
    sudo add-apt-repository ppa:bumblebee/stable
    sudo add-apt-repository ppa:ubuntu-x-swat/x-updates
    sudo apt-get update
    sudo apt-get install bumblebee bumblebee-nvidia
    sudo reboot

  • We are going to install CUDA 4.2 in this tutorial. So please download CUDA Toolkit and CUDA SDK from here. When downloading the toolkit, choose either 32-bit version or 64-bit version depending on your OS.

  • Run the toolkit installer.
    chmod +x cudatoolkit_4.2.9_linux_64_ubuntu11.04.run
    sudo ./cudatoolkit_4.2.9_linux_64_ubuntu11.04.run

  • In the middle of the installation, you'll be asked for the installation path. Press ENTER to choose the default path, which is /usr/local/cuda

  • If the installation is successful, you will get a message asking to set path variables. We'll add after the installation of the SDK.

  • To install the SDK, run the installer. (do not use sudo here)
    ./gpucomputingsdk_4.2.9_linux.run

  • The default location for the SDK will be ~/NVIDIA_GPU_Computing_SDK.

  • Now we can add the path variables. For that go into the /etc directory and open the file named environment.
    sudo cd /etc
    gksudo gedit environment

    It will contain a line looks like
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    :/usr/games"

  • Append the the CUDA bin directory path (/usr/local/cuda/bin by default) to the end of that line,save and close the file. After the apending, the line should look like
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    :/usr/games:/usr/local/cuda/bin"

  • Then reload the path.
    source environment

  • To add the library path, run the following commands. It will create a new file called cuda.conf and open it.
    gksudo gedit ld.so.conf.d/cuda.conf

  • Add the follwing lines to that file, save and close it.
    /usr/local/cuda/lib64
    /usr/local/cuda/lib

  • Locate the path of the file libcuda using the following command.
    locate -i libcuda
    It should be something like /usr/lib/nvidia-current

  • Then go to the directory NVIDIA_GPU_Computing_SDK/C/common and open the file common.mk. Find the line with -lcuda (probably 275th line) and add -L and the path for the file libcuda in front of it.
    -L/usr/lib/-lcuda

  • Then install OpenGL libraries.
    sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

  • Open the common.mk file again and find the lines with both ${OPENGLLIB} and ${RENDERCHECKGLLIB}. (there should be 3 lines such as 271st, 275th and 281st) Then swap the positions of  ${OPENGLLIB} and ${RENDERCHECKGLLIB} in these lines.

  • As the last step go to the directory NVIDIA_GPU_Computing_SDK and run the command
    make

  • If the installation is successful, several executables will be created within the folder ~/NVIDIA_GPU_Computing_SDK/C/bin/linux/release

  • Go to that folder and run any executable with the command
    optirun ./executable_name
    (optirun is a command which forces the executable to run on high performance GPU

  • If it executed successfully CONGRATULATIONS! YOU HAVE INSTALLED AND CONFIGURED CUDA SUCCESSFULLY! 



     The latest version of CUDA is CUDA 5.0. The main difference is the toolkit, SDK and the drivers are bundled in a single package. So in the installation process you'll be asked whether to install the driver or not. Choose NOT TO and the rest of the process will be the same.
(Please note that the default directory for cuda 5 is /usr/local/cuda5.0)

Comments

Popular posts from this blog

Automatically connect to a dial-up dongle in Windows 7

Set the battery level at which Ubuntu gives the "Battery Low" Warning

Cleaning older documents from Elasticsearch