Installing CUDA 5 on Ubuntu 12.04

This is a follow up to my previous post which was aimed at CUDA 4.2 on Ubuntu 12.04. Although 12.10 is out, it’s not as stable as I would like it to be – I’d recommend sticking with Ubuntu 12.04 for development unless you have specific reasons to upgrade. With CUDA 5, Nvidia has greatly simplified the installation process for Linux, packaging the CUDA toolkit, the SDK and the development drivers all in a neat little package.

A few points before we begin though –

  1. As always, a working CUDA installation requires a CUDA capable card.
  2. Installing CUDA 5 will give you the option of opting for the development drivers or sticking with the regular ones. Personally, I chose the regular ones and they’ve worked out just fine (version 310.19 as of this writing).

STEP I – Driver installation (if you choose the regular drivers)

This section remains almost unchanged. Download the Nvidia drivers for Linux from their website, making sure to select 32 or 64-bit Linux based on your system.

Make sure the requisite tools are installed using the following command –

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

Next, blacklist the required modules (so that they don’t interfere with the driver installation) –

sudo nano /etc/modprobe.d/blacklist.conf

Add the following lines to the end of the file, one per line, and save it when done –

blacklist amd76x_edac
blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist nvidiafb
blacklist rivatv

In order to get rid of any nVidia residuals, run the following command in a terminal –

sudo apt-get remove --purge nvidia*

This may take a while, so be patient. Once it’s done, reboot your machine. At the login screen, don’t login just yet. Press Ctrl+Alt+F1 to switch to a text-based login, and switch to the directory which contains the downloaded driver. Run the following commands –

sudo service lightdm stop
chmod +x NVIDIA*.run

where NVIDIA*.run is the full name of your driver. Next, start the installation with –

sudo ./NVIDIA*.run

Follow the onscreen instructions. If the installer throws up an error about nouveau still running, allow it to create a blacklist for nouveau, quit the installation and reboot. In that case, run the following commands again –

sudo service lightdm stop
sudo ./NVIDIA*.run

The installation should now proceed smoothly. When it asks you if you want the 32-bit libraries and if you want it to edit xorg.conf to use these drivers by default, allow both.

Reboot once the installation completes.

STEP II – CUDA toolkit installation

Download the CUDA toolkit (I used the Ubuntu 11.10 64-bit version). Navigate to the directory that contains the downloaded CUDA toolkit package, and run the following command –

chmod +x cuda*.run
sudo ./cuda*.run

where cuda*.run is the full name of the downloaded CUDA toolkit. Accept the license that appears. Next, choose to install the driver if you’ve not done so already (this is the development version). Installation of the driver will require switching to a text-based shell (Ctrl+Alt+F1), stopping lightdm as detailed in the previous step and running the toolkit installer as superuser. The CUDA 5.0 toolkit installation is compulsory, while that of the included samples isn’t. Allow the installer to proceed with default locations unless you have specific reasons for not doing so. This makes for easier troubleshooting in the future.

This process might take a while depending upon your system config. Once it’s done, double check the summary screen to make sure everything is in place. If the installation of the samples fails (which it does more often than not), it’s probably due to one or more missing libraries. In that case, install the required libraries and rerun the installer, choosing to install the samples alone.

To make sure the necessary environment variables (PATH and LD_LIBRARY_PATH) are modified every time you access a terminal, add the requisite lines (from the summary screen) to the end of ~/.bashrc as follows –

32 bit systems –
export PATH=$PATH:/usr/local/cuda-5.0/bin
export LD_LIBRARY_PATH=/usr/local/cuda-5.0/lib

64 bit systems –
export PATH=$PATH:/usr/local/cuda-5.0/bin
export LD_LIBRARY_PATH=/usr/local/cuda-5.0/lib64:/lib

The paths used above will vary based on the install directory you choose for the toolkit. This wraps up the arguably simpler setup (compared to CUDA 4.x for Linux users), although the samples’ installation still suffers from dependency hell.

STEP III – CUDA samples installation and troubleshooting

While the installation of the samples should be straightforward (simply run the all in one toolkit installer), it’s often not all that easy. If you’re getting an error similar to “Samples installation failed due to missing libraries – not found”, it’s typically because the installer looks for and not variants (like To fix this, we’ll have to create softlinks so the installer can track down these files.

Determine if variants of are present as follows –

sudo find /usr -name libglut\*

On my 64-bit installation of Ubuntu 12.04, this output the following text –


Now that a variant of (that is, has been found, we can create a softlink to it –

sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/

Next, rerun the all in one installer choosing to install only the samples this time around. That should fix the “Missing libraries” error that cropped up previously.

Further reading resources can be found here if you’re looking for books on CUDA and GPGPU programming in general.

Credit for STEP III goes to these release notes (refer section III b – Known issues on CUDA Samples for Linux).

84 thoughts on “Installing CUDA 5 on Ubuntu 12.04

  1. Pingback: Installing CUDA on Ubuntu 12.04 | sn0v

  2. First, why should i install the driver from its own file when the cuda download includes a driver in it?
    Second, when I try installing the toolkit and samples it fails, says “Unsupported compiler: 4.7.2 Missing required library Toolkit: Installation Failed. Using unsupported compiler.. Samples: Installation Failed. Missing required libraries”

    I followed your suggestions of requirements at the beginning, what then could be wrong?

    • I chose to install the regular driver because I’ve had issues (display freezing, random crashes) on my machine with development drivers in the past. Also, the driver I downloaded separately (309.xx) was a later version than the one packaged with the installer (306.xx). You should be fine as long as you use version 304.54 or later. I’ve updated the post with the answer to your second question (refer STEP III – CUDA samples installation and troubleshooting). As for the toolkit installation failing, I used gcc v4.6.3 – which one are you using?

      • my gcc is v4.7.2
        I was trying to get this to work on ubuntu 12.10 so that’s probably why i was having trouble.
        I went back to 12.04 though. I’ll try this again and see if i have any problems. I’m trying to get this to work on a laptop and also want the bumblebee package to work

  3. This guide is great, completely spot on. I will like to translate it (to spanish) for my research group, can I have your permission? Of course I will cite the original source. Thank you.

  4. For hose of us with later versions of gcc, run -override compiler
    This drop the compiler version check. Worked for me on Fedora 17, YMMV 🙂

  5. Do you mind if I use this in a video tutorial? I will cite this before going into it. Though it’s graphics driver first and then cuda as I’m doing tutorials for Steam on linux since it goes open beta this week.

    P.S. John, the ‘-overide compiler’ worked for me thanks!

  6. I found that letting the cuda driver install the video driver did not register it to DKMS , so when i updated the kernel the video driver was broken and i had to reinstall it and make it register to DMKS that will automatically manage it

    chmod +x cuda*.run
    mkdir /tmp/nvidia
    sudo ./cuda*.run -extract=/tmp/nvidia

    now in /tmp/nvidia there are the cuda toolkit , cuda samples and the driver i need
    so i just entered

    /tmp/nvidia/devdriver*.run –dkms

    and followed the procedure , entering yes when you’re prompter to register the driver to DKMS and reboot, hopefully it should work !
    to check if it’s registered run
    $ sudo dkms status
    and it should print something like
    nvidia, 304.54, 3.2.0-35-generic, x86_64: installed
    this way it should last for every update!
    PS: to install the video driver you need to stop the desktop manager and do it while in the textual terminal like the original guide

  7. I have a system with 4 K10’s and am running 12.04. I don’t need the video as this is a compute system only with a simple video card. After going through this tutorial I am still left with “-> no CUDA-capable device is detected”
    I have no /dev/nvidia* which would be created if I have an nVidia card for video.
    Thank you for any direction
    Lawrence Downes

  8. I am having a problem with this installation:
    My System Configuration:
    Ubuntu 12.04 64 bit
    NVIDIA Geforce GT 540M

    The resolution of the screen is giving only 640*480. I has some thing to do with the installation of NVIDIA Drivers. (I have correctly choosen my NVIDIA drivers too.)

    Execution is also giving problems as below:
    $ ./matrixMul
    [Matrix Multiply Using CUDA] – Starting…
    cudaGetDevice returned error code 35, line(382)
    cudaGetDeviceProperties returned error code 35, line(395)
    MatrixA(320,320), MatrixB(640,320)
    cudaMalloc d_A returned error code 35, line(164)

    Also it is giving error code 38 a few times.

    Please help.

  9. from bobo

    maingear:/usr/local/cuda/samples/0_Simple/matrixMul # lsmod | grep -i video
    video 19436 0
    thermal_sys 24622 4 video,fan,thermal,processor
    maingear:/usr/local/cuda/samples/0_Simple/matrixMul # ./matrixMul
    [Matrix Multiply Using CUDA] – Starting…
    cudaGetDevice returned error code 35, line(382)
    cudaGetDeviceProperties returned error code 35, line(395)
    MatrixA(320,320), MatrixB(640,320)
    cudaMalloc d_A returned error code 35, line(164)
    maingear:/usr/local/cuda/samples/0_Simple/matrixMul #

    • Error code 35 is most likely a driver issue resulting from either an old driver (not recent enough to run CUDA 5) or your program being linked to an older version of In your case, “lsmod | grep -i video” returns no mention of the Nvidia proprietary driver. The easiest solution is to remove any existing NVidia driver(s) and residuals, and then installing the development drivers that are a part of the CUDA 5 installer. Check the post for instructions.

    • The installation of the included samples often fails due to certain missing (or improperly linked) libraries. In my experience, the most common error is the “ not found” one which I’ve addressed in step III. In rare cases, your system may altogether be missing libraries that are mentioned in the installer’s error messages. Softlinking isn’t an option here, and the required libraries must be manually installed first.

  10. Excellent tutorial. Worked fine for me.

    However, I would just like to mention, it is required to stop the gdm before you proceed to installation. In Ubuntu 12.04 —
    $ sudo /etc/init.d/lightgdm stop
    Note “lightgdm” instead of gdm

  11. Hi,
    Just went through your documentation….Its gr8…..But I couldn’t find the solution to this:
    When I make the deviceQuery sample, it gives this error

    apoorv@apoorv-PC:~/NVIDIA_GPU_Computing_SDK/C/src/deviceQuery$ make
    /usr/bin/ld: cannot find -lcuda
    collect2: ld returned 1 exit status
    make: *** [../../bin/linux/release/deviceQuery] Error 1

    Cud u Plz help me with this 🙂

    • Hey I faced the same issue. This has nothing to do with cuda toolkit installation but the nvidia driver installation. does not come with the toolkit it comes with the driver even if you don’t install cuda it will exist /usr/lib/nvidia-current (Default directory for ubuntu don’t know about rest)

    • If you use the nvidia driver installed by Ubuntu from the restricted repository (which makes things a lot easier generally) the cuda library is not where the script expects it. I had to issue the following command (note the directory where I made it) :

      /usr/local/cuda/lib64 $>sudo ln -s /usr/lib/nvidia-current-updates/ .

      Note the period at the end. Before issuing this command, you need to switch to the /usr/local/cuda/lib64 directory by using the command

      chdir /usr/local/cuda/lib64

      BTW a great set of instructions! Thank you!

  12. Thanks the tips, worked great!

    FYI, some of the sample code(s) won’t compile without MPI. If you opt to install it, try and use the flag –disable-vt when configuring as VampirTrace is not currently compatible with CUDA 5.

  13. Pingback: Install CUDA 5.0 on Ubuntu 12.10 | Daniel's Utopia

  14. I got stuck at “… and switch to the directory which contains the downloaded driver.” I have no idea where apt get installed NVIDIA drivers. Nor do i know what to search for. Very new to Ubuntu.

    • Hey, if you’ve already got the proprietary NVidia drivers installed, you only need to run the CUDA installer. This is most probably in your Downloads folder – navigate to it using “cd ~/Downloads”. Let me know if you need further help.

      • wow, thanks for the quick reply. I am following the directions on this page and have not downloaded anything nor do I know if I already have the proprietary NVidia drivers. If the step on this page prior to the restart step include the installation of proprietary drivers then I have them. Everything seemed to go smoothly up to this point – I got through the section that states:

        “In order to get rid of any nVidia residuals, run the following command in a terminal –
        sudo apt-get remove –purge nvidia*

        I then restarted and now i am not sure what to do. It sounds like I was supposed to download the CUDA package at some point. I did not follow any steps from the “previous post” because the first sentence says that that page is replaced by new instructions (this page). Should I go back to the “Previous post” and follow to some point and then come back to this page?

        My inexperience makes me extremely cautious,

      • also, I am so cautious that I am afraid to log into the GUI desktop for fear that not following the instructions will somehow nullify what I have already done. Thus, I have not downloaded any CUDA packages – I assumed the apt-get commands took care of the CUDA bits.

  15. Thanks! I spent a few hours trying to make it work on my own but I had some problems with openGL windows which were transparents. Works perfectly now, thanks to you!

  16. Pingback: install cuda on ubuntu 12.04 64 bit ( plus bumblebee 3.0) | Lava's Blog

  17. Pingback: Why is CUDA flummoxed by variable declaration in bashrc? | BlogoSfera

  18. Really worked like a charm (3 weeks ago).Thank you so much!
    I am new to linux and i noticed that some system updates may introduce problems. last update included nouveau and xorg something..i wondered about the reason that i had to update them but finally i clicked yes…and BOOM!
    how can i just rip off everything and reinstall nvidia proprietary drivers+Cuda?
    thank you!!! resolution in terminal is totally wrong…i use it almost blind.( i can see pixels…100% sure that i can login as root)
    *sorry for my english*

  19. My laptop has two GPU, namely Intel hd4000 and Nvidia GT630M, with Optimus technology. Since I will only be using Intel’s GPU for display, and the Nvidia card is for CUDA, is it possible just to intall the CUDA toolkit without installing the Nvidia Graphics driver? Also, do I need to install bumblebee to support the Optimus card?

  20. thank you once again!!! I was afraid that i had to tweak some files, as remnants of the previous installations, but i just had to run again the driver installer, after purging nvidia*.
    (I had a problem with Ctrl-F1, only the upper half of the screen seemed to display something and that something was (were) 4 mini screens side by side. Since it was difficult to guess only by pixel patterns, i just booted in failsafe mode and clicked to reset the default screen settings. Luckily after that i got a proper display and managed to install the driver through tty.)

  21. Pingback: Installing Cuda on Ubuntu 13.04 Raring Ringtail | Musings of a geek

  22. It might be a good idea to mention the process for getting the driver as a specific stage. I found it a bit disconcerting, having rebooted, to be told “go to the directory where you downloaded the driver”.I was supposed to download a driver also???

    There was some general discussion about drivers but nothing specific about downloading them. And also what is the process if you do not choose the regular drivers? Does the toolkit install the drivers? I guess the answers to these questions may be obvious but not to me the first time.

    • I’ve updated Step I to include the download link. Regarding the development drivers, I’ve already mentioned that the toolkit, SDK and dev drivers are all packaged together. Thanks for the suggestions 🙂

  23. Pingback: Installing CUDA and Theano on Ubuntu 12.04 Precise | Ben McCann

  24. Thanks for the great guide.

    One point:
    Setting LD_LIBRARY_PATH as you recommend breaks some programs (Namely gazebosim(.org)).
    Setting it as
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-5.0/lib64:/lib
    Maintains the old path and prevents breakage.

  25. This is great, works perfectly fine in Lubuntu 13.04, after a little trial and error. I used the packages nvidia-cuda-toolkit and nvidia-cuda-dev that seem to install all that is needed. (But downloading from Nvidia, of course, can me more up-to-date). The only snag is the compiler, I had to edit host_config.h to disable the compiler check.

  26. I have tried to follow the script but got stuck at the beginning:

    sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
    packages have unmet dependencies:
    libgl1-mesa-glx: Depends: libglapi-mesa (= 8.0.4-ubuntu0.5)
    Recommends: libgl1-mesa-dri (>= 7.2)
    E: unable to correct problems, you have held broken packages

    Any suggestions? I am using 12.04.

    • Use sudo dpkg -r where is the name of the package which throws up errors.
      Then, try sudo apt-get update && sudo apt-get upgrade. Next, retry the instructions in the blog. Lemme know if this helps.

      • Utkarsh, thanks for the feedback but it didn’t solve the problem

        following your suggestions the following were tested:
        sudo dpkg -r libgl1-mesa-dri
        resulted in — dpkg: warning: there’s no installed package matching libgl1-mesa-glx
        after this the rest is the same as was shown before.

        In the process I have found that there were others complaining about what appears to be a similar problem:

        Any comments?

      • The bug mentioned in your link would apply if you were using GNOME 3.8, which is highly unlikely since the version available in the Ubuntu 12.04 repos is GNOME 3.4. Use “gnome-shell –version” to verify it though. If it’s not 3.8, proceed with the following – “sudo apt-get install libglapi-mesa libgl1-mesa-dri”. Please let me know how it goes, we can continue this via chat if need be 🙂

  27. There is an alternative way to install the CUDA toolkit that fixes the libglut issue as well as the compiler version check.

    sudo apt-get install gcc-4.6 g++-4.6
    sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-4.6 40 –slave /usr/bin/g++ g++ /usr/bin/g++-4.6
    sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-4.7 60 –slave /usr/bin/g++ g++ /usr/bin/g++-4.7
    sudo update-alternatives –config gcc

    Then select 4.6 and do the install.

  28. Pingback: Install Kinect Fusion on Ubuntu | lvzhaoyang

  29. I ve installed cuda on my acer v3-771q on ubuntu 12.04 lts using the package manager. (see nvidia linux install help).
    I had to install the following:

    sudo apt-get purge nvidia-319-updates nvidia-settings-319-updates nvidia-prime
    sudo apt-get update
    sudo apt-get dist-upgrade
    sudo apt-get install xserver-xorg-lts-raring
    sudo apt-get install nvidia-319-updates nvidia-prime
    sudo reboot

    The resulting xserver is freezing every 1 Minute !
    Reason is NOT the NVIDIA driver but the touchpad.
    solution: solution for touchpad was copied from here:
    create a file cat /etc/modprobe.d/psmouse.conf .
    copy this to it: options psmouse proto=imps
    then save it.

    create a file .envcuda
    and copy in it the following:

    export PATH=/usr/local/cuda-5.5/bin:$PATH
    export LD_LIBRARY_PATH=/usr/local/cuda-5.5/lib64:$LD_LIBRARY_PATH

    add this to .bashrc: . ~/.envcuda

    like this you get a stable running environment for cuda.
    Here a screenshot from running cuda samples:

  30. Hello guys,
    I think its time to give up,
    I installed ubuntu 12.04 two days ago. and wasted 36 hours on finding solution here at askubunto and other forum, following them more than 4 times reinstalled ubntu but its such a waste , not because ubuntu is not good, because there are so many different information here, so confusing for newbies like me. so thought its time to get back to window,
    still i want to try one more time, this time i again reinstalled a fresh ubuntu
    and here is what i want.

    I want to run blender 2.69.
    **what i have.**

    i7 processor with 3gb
    nvidia Geforce 640 with 4gb ram

    **what i did so far.**

    tried many follow up here on askubuntu. got some response to my help call too. but they just keep quite after a few answer and leave me in the middle of no where then i have install fresh and follow again different suggestion. the one i found very easy to follow is this.
    but the installation of nvidia worked and the installation of cuda failed, again i installed ubuntu fresh one, and started second time this time the first step installation of nvidia failed.
    now its fresh installation of ubuntu and waiting for one more time is some one comes up with the help would be appreciated. thanks.

  31. Pingback: Can not properly install blender | Ubuntu InfoUbuntu Info

  32. Pingback: Cuda4Ubuntu | bittnt's Research Blog

  33. Fabulous. I’d had all kinds of problems trying to install on 13.10, ending up with my screen inverted and upside down, and terminals becoming whitespace I couldn’t type into.

    After following this tutorial and installing 12.04, the installation worked smoothly.

    Thanks for taking the time to write it up.

  34. The tutorial you wrote works. But after updating kernel, gpu is not getting detected. If I reinstall driver and cuda, then I am not able to login to my interface. How do I handle updating the system without disturbing my Cuda environment?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s