How to install CUDA Toolkit 7/8/9 on Debian 8 (Jessie) or 9 (Stretch)?

The following instructions are valid for CUDA 7.0, 7.5, and several previous (and probably later) versions. As far as Debian distributions, they're valid for Jessie and Stretch and probably other versions. They assume an amd64 (x86_64) architecture, but you can easily adapt them for x86 (x86_32).

Installation prerequisites

  • g++ - You should use the newest GCC version supported by your version of CUDA. For CUDA 7.x this would be version 4.9.3, last of the 4.x line; for CUDA 8.0, GCC 5.x versions are supported. If your distribution uses GCC 5.x by default, use that, otherwise GCC 5.4.0 should do. Earlier versions are usable but I wouldn't recommend them, if only for the better modern-C++ feature support for host-side code.
  • gcc - comes with g++. I even think CMake might default to having nvcc invoke gcc rather than g++ in some cases with a -x switch (but not sure about this).
  • libGLU - Mesa OpenGL libraries (+ development files?)
  • libXi - X Window System Xinput extension libraries (+ development files?)
  • libXmu - X Window System "miscellaneous utilities" library (+ development files?)
  • Linux kernel - headers for the kernel version you're running.

If you want a list of specific packages - well, that depends on exactly which distribution you're using. But you can try the following (for CUDA 7.x):

sudo apt-get install gcc g++ gcc-4.9 g++-4.9 libxi libxi6 libxi-dev libglu1-mesa libglu1-mesa-dev libxmu6 libxmu6-dev linux-headers-amd64 linux-source

And you might add some -dbg versions of those packages for debugging symbols.

I'm pretty sure this covers it all - but I might have missed something I just had installed already. Also, CUDA can work with clang, at least experimentally, but I haven't tried that.

Installing the CUDA kernel driver

  1. Go to NVIDIA's CUDA Downloads page.
  2. Choose Linux > x86_64 > Ubuntu , and then whatever latest version they have (at the time of writing: Ubuntu 15.04).
  3. Choose the .run file option.
  4. Download the .run file (currently this one). Make sure not to put it in /tmp.
  5. Make the .run file executable: chmod a+x cuda_7.5.18_linux.run.
  6. Become root.
  7. Execute the .run file: Pretend to accept their silly shrink-wrap license; say "yes" to installing just the NVIDIA kernel driver, and say "no" to everything else.

The installation should tell you it expects to have installed the NVIDIA kernel driver, but that you should reboot before continuing/retrying the toolkit installation. So...

  1. Having apparently succeeded, reboot.

Installing CUDA itself

  1. Be root.
  2. Locate and execute cuda_7.5.18_linux.run
  3. This time around, say No to installing the driver, but Yes to installing everything else, and accept the default paths (or change them, whatever works for you).

The installer is likely to now fail. That is a good thing assuming it's the kind of failure we expect: It should tell you your compiler version is not supported - CUDA 7.0 or 7.5 supports up to gcc 4.9 and you have some 5.x version by default. Now, if you get a message about missing libraries, that means my instructions above regarding prerequisites somehow failed, and you should comment here so I can fix them. Assuming you got the "good failure", proceed to:

  1. Re-invoke the .run file, this time with the --override option.
  2. Make the same choices as in step 11.

CUDA should now be installed, by default under /usr/local/cuda (that's a symlink). But we're not done!

Directing NVIDIA's nvcc compiler to use the right g++ version

NVIDIA's CUDA compiler actually calls g++ as part of the linking process and/or to compile actual C++ rather than .cu files. I think. Anyway, it defaults to running whatever's in your path as g++; but if you place another g++ under /usr/local/cuda/bin, it will use that first! So...

  1. Execute symlink /usr/bin/g++-4.9 /usr/local/cuda/bin/g++ (and for good measure, maybe also symlink /usr/bin/gcc-4.9 /usr/local/cuda/bin/gcc.

That's it.

Trying out the installation

  1. cd /root/NVIDIA_CUDA-7.5_Samples/0_Simple/vectorAdd
  2. make

The build should conclude successfully, and when you do

  1. ./vectorAdd

you should get the following output:

root@mymachine:~/NVIDIA_CUDA-7.5_Samples/0_Simple/vectorAdd# ./vectorAdd
[Vector addition of 50000 elements]
Copy input data from the host memory to the CUDA device
CUDA kernel launch with 196 blocks of 256 threads
Copy output data from the CUDA device to the host memory
Test PASSED
Done

Notes

  • You don't need to install the NVIDIA GDK (GPU Development Kit), but it doesn't hurt and it might be useful for some. Install it to the root directory of your system; it's pretty safe and there's an uninstaller afterwards: /usr/bin/uninstall_gdk.pl. In CUDA 8 it's already integrated into CUDA itself IIANM.
  • Do not install additional packages with names like nvidia-... or cuda... ; they might not hurt but they'll certainly not help.
  • Before doing any of these things, you might want to make sure your GPU is recognized at all, using lspci | grep -i nvidia.

I think you should do it using backports, I'm actually smoothly installing nvidia-cuda-toolkit 7.5 on debian jessie.

Add backports, non free, to your /etc/apt/sources.list. For me (on jessie) I just put this on the bottom of my file:

# jessie-backports
deb http://httpredir.debian.org/debian jessie-backports main contrib non-free

saved, apt-get update, and finally:

apt-get install -t jessie-backports nvidia-cuda-toolkit