Monday, July 10, 2017

Install cuda 8.0 and cudnn in ubuntu 16.04

Original article link https://askubuntu.com/questions/799184/how-can-i-install-cuda-on-ubuntu-16-04

Install CUDA for Ubuntu

There is an Linux installation guide. However, it is basically only those steps:
  1. Download CUDA: I used the 16.04 version and "runfile (local)". That is 1.5 GB + 93MB.
  2. Check the md5 sum: md5sum cuda_7.5.18_linux.run. Only continue if it is correct.
  3. Remove any other installation (sudo apt-get purge nvidia-cuda* - if you want to install the drivers too, then sudo apt-get purge nvidia-*.)
    1. If you want to install the display drivers(*), logout from your GUI. Go to a terminal session (ctrl+alt+F2)
    2. Stop lightdm: $ sudo service lightdm stop or $ sudo init 3
    3. Create a file at /etc/modprobe.d/blacklist-nouveau.conf with the following contents: blacklist nouveau options nouveau modeset=0
    4. Then do: sudo update-initramfs -u
  4. sudo sh cuda_7.5.18_linux.run --override. Make sure that you say y for the symbolic link.
    1. Start lightdm again: sudo service lightdm start
  5. Follow the command-line prompts
  6. Need to reboot computer.
Notes: Yes, there is the possibility to install it via apt-get install cuda. I strongly suggest not to use it, as it changes the paths and makes the installation of other tools more difficult.
You might also be interested in How can I install CuDNN on Ubuntu 16.04?.
*: Don't install the display drivers with this script. They are old. Download the latest ones from http://www.nvidia.com/Download/index.aspx

Verify CUDA installation

The following command shows the current CUDA version (last line):
# Should add cuda path first, don't use $ sudo apt-get install nvidia-tool, the default version is 7.5, will be conflict later.
add two lines in ~/.bashrc
export PATH="/usr/local/cuda-8.0/bin:$PATH"
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
$ source ~/.bashrc
May logout to make sure the path is added. can check by using $ echo $PATH

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sun_Sep__4_22:14:01_CDT_2016
Cuda compilation tools, release 8.0, V8.0.44
The following command shows your driver version and how much GPU memory you have:
$ nvidia-smi
Fri Jan 20 12:19:04 2017       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 367.57                 Driver Version: 367.57                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce 940MX       Off  | 0000:02:00.0     Off |                  N/A |
| N/A   75C    P0    N/A /  N/A |   1981MiB /  2002MiB |     98%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0      1156    G   /usr/lib/xorg/Xorg                             246MiB |
|    0      3198    G   ...m,SecurityWarningIconUpdate<SecurityWarni   222MiB |
|    0      6645    C   python                                        1510MiB |
+-----------------------------------------------------------------------------+

Help! The new driver does not work!

Don't panic. Even if you can't see anything on your computer, the following steps should get you back to the state before:
  1. Press shift while startup
  2. Go into a root shell
  3. Make it writable by mount -o remount,rw / (- is ? and / is - in the american layout)
  4. sh cuda_7.5.18_linux.run --uninstall
  5. sudo apt-get install nvidia-361 nvidia-common nvidia-prime nvidia-settings

Graphic drivers

Installing the graphic drivers is a bit tricky. This has to be done without graphics support.
  1. Logout from your current X session.
  2. Ctrl+Alt+F4 (you can switch back with Ctrl+Alt+F7)
  3. You should remove all other drivers before.
    1. Search them via dpkg -l | grep -i nvidia
    2. Remove them via sudo apt-get remove --purge nvidia-WHATEVER
  4. Stop lightdm via sudo service lightdm stop
  5. You might need to reboot your pc / blacklist the nouveau driver (German tutorial)

The installation of CuDNN is just copying some files. Hence to check if CuDNN is installed (and which version you have), you only need to check those files.

Install CuDNN

Step 1: Register an nvidia developer account and download cudnn here (about 80 MB). You might need nvcc --version to get your cuda version.
Step 2: Check where your cuda installation is. For most people, it will be /usr/local/cuda/. You can check it with which nvcc.
Step 3: Copy the files:
$ cd folder/extracted/contents
$ sudo cp include/cudnn.h /usr/local/cuda/include
$ sudo cp lib64/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

Check version

You might have to adjust the path. See step 2 of the installation.

$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2


No comments:

Post a Comment