Installation of CUDA Toolkit on Linux
A short guideline for installation of CUDA Toolkit 9.1 on Ubuntu 17.10
This article aims to be a guideline for installation of CUDA Toolkit on Linux. Target environment of this guideline is CUDA 9.1 and Ubuntu 17.10, however it can be applicable to other systems. Actually, it’s not a rocket science, but there are certain points which require extra attention and those points are completely distributive-independent.
0. Pay attention to system requirements
Official installation guide for Linux defines system requirements which you must conform to make things work. This might be obvious that everyone should get familiar with system requirements before installing anything on a target system. However it is easy to lull your vigilance if you are working basically with software in the modern world of constant changes and updates.
So, pay special attention to supported versions of kernel and gcc. If your existing installation differs from the recommended one, you must be able to decide whether you are OK to upgrade/downgrade your kernel and whether it’s possible for you to install required version of gcc.
1. Manage kernel version
CUDA is a device architecture which is implemented inside GPU. It allows to create a heterogeneous computing architecture, where CPU acts as a supervisor and GPU takes role of a workhorse. Thus, CPU and GPU need to talk to each other and such communication is done via driver. As Linux implements microkernel architecture where drivers are treated as plugins, they must be compatible with the kernel which they are going to be plugged into.
Unfortunatelly, Nvidia’s drivers are source-closed and you cannot compile them for your kernel version. Hence, you are left to use what you are given. If driver is not compatible with your kernel, you have to change the kernel.
As official documentation states, supported version of kernel for Ubuntu 17 is 4.9. Well, Ubuntu 17.10 is shipped with kernel 4.13 and downgrading it to version 4.9 may look too aggressive.
So, will it work with kernel 4.13? Actually, yes and no, it depends. Installation has failed for my version 4.13.0–32 and it has failed for version 4.13.26. Why? Well, there are bugs inside the kernel. For example, some functions may be used without includes of their headers and this results into compilation errors. While such issues are easy to fix, there are other errors which can drive you crazy.
Luckily, kernel 4.13.16 is suitable for CUDA 9.1 and no errors were observed.
I recommend to use UKUU to manage kernels on Ubuntu. You can stay without it, however personally I have found it to be very useful, as it allows you to do all job with few command-line instructions. Feel free to follow this article to get familiar with UKUU. In short installation looks like:
$ sudo ukuu --install v4.13.16
Don’t forget to reboot your system after switching to another kernel:
$ sudo reboot
After that make sure the kernel headers and development packages for the currently running kernel are installed:
$ sudo apt install linux-headers-$(uname -r)
$ sudo apt install build-essential
2. Check out GCC
CUDA needs gcc 6 for Ubuntu 17. The latter one comes with gcc 7. Required version can coexist with default one:
$ sudo apt install gcc-6
$ sudo apt install g++-6
3. Download the toolkit
Download process is straightforward:
- Go to download page.
- Select
Linux
OS. - Select
x86_64
architecture. - Select
Ubuntu
distribution. - Select version
17
. - Select
runfile (local)
installer type.
It’s important to select
runfile
for installer type, as this installer will contain display driver of version which is guaranteed to be compatible with toolkit.
4. Extract installer’s content
Downloaded installer contains display driver, toolkit and samples. We are going to install them manually, so extract them:
$ cd ~/Downloads
$ mkdir ./nvidia
$ ./cuda_9.1.85_387.26_linux.run -extract=./nvidia
5. Uninstall existing Nvidia drivers
If your system already has display driver installed, delete it:
$ sudo apt --purge remove nvidia-*
6. Remove X server config
If you have configuration of X server, delete or backup it:
$ sudo rm /etc/X11/xorg.conf
7. Disable Nouveau drivers
The Nouveau drivers must be disabled if they are present:
- Create a file
/etc/modprobe.d/blacklist-nouveau.conf
with the following 2 lines:
blacklist nouveau
options nouveau modeset=0
2. Regenerate the kernel initramfs
:
$ sudo update-initramfs -u
8. Reboot
$ sudo reboot
9. Switch to text mode
Press Ctrl
+ Alt
+ F1
to enter text mode and login as usual user.
10. Disable display manager
Disable display manager, so nothing will interact with graphics device:
$ sudo service lightdm stop
After this point you will not be able to read this text from target machine, so make sure you can read it from another device.
11. Install display driver and toolkit
Now it’s time to install all we need:
$ cd sudo ~/Downloads/nvidia
$ sudo ./NVIDIA-Linux-x86_64-387.26.run --no-opengl-files
$ sudo ./cuda-linux.9.1.85-23083092.run
$ sudo ./cuda-samples.9.1.85-23083092-linux.run
12. Update environment variables
Edit rc
file of your shell to update environment variables (e.g., ~/.bashrc
):
export PATH="/usr/local/cuda-9.1/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-9.1/lib64:$LD_LIBRARY_PATH"
and refresh environment for current session:
$ source ~/.bashrc
13. Verify display driver version
Check display driver is installed properly:
$ cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module 387.26 Thu Nov 2 21:20:16 PDT 2017
GCC version: gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3)
14. Verify CUDA compiler driver
Check CUDA compiler driver is installed properly:
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:07:56_CDT_2017
Cuda compilation tools, release 9.1, V9.1.85
15. Give CUDA proper version of GCC
We have installed gcc 6
and g++ 6
at step #2. Now it’s time to make CUDA able to use it:
$ sudo ln -s /usr/bin/gcc-6 /usr/local/cuda/bin/gcc
$ sudo ln -s /usr/bin/g++-6 /usr/local/cuda/bin/g++
16. Test compilation
Now it’s time to check if installation was successful. We can do this by compiling and running one of samples.
Go to the sample directory:
$ cd /usr/local/cuda-9.1/samples/0_Simple/matrixMul
Compile sample (as root
, because samples are located inside system dir):
$ sudo make"/usr/local/cuda-9.1"/bin/nvcc -ccbin g++ -I../../common/inc -m64 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_70,code=compute_70 -o matrixMul.o -c matrixMul.cu
"/usr/local/cuda-9.1"/bin/nvcc -ccbin g++ -m64 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_70,code=compute_70 -o matrixMul matrixMul.o
mkdir -p ../../bin/x86_64/linux/release
cp matrixMul ../../bin/x86_64/linux/release
Check it can run:
$ ./matrixMul [Matrix Multiply Using CUDA] - Starting...
GPU Device 0: "GeForce GTX 950" with compute capability 5.2MatrixA(320,320), MatrixB(640,320)
Computing result using CUDA Kernel...
done
Performance= 189.37 GFlop/s, Time= 0.692 msec, Size= 131072000 Ops, WorkgroupSize= 1024 threads/block
Checking computed result for correctness: Result = PASSNOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.
17. Reboot
Reboot just to be sure everything is good:
$ sudo reboot
18. Get your hands dirty
At this point CUDA Toolkit is installed and it’s time to get hands dirty.
Good luck!
Links and references
- http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
- https://linuxhint.com/how-to-upgrade-the-kernel-on-ubuntu-17-10/
- https://kislayabhi.github.io/Installing_CUDA_with_Ubuntu/
- https://devtalk.nvidia.com/default/topic/1028566/cuda-9-1-install-fail-with-ubuntu-16-04-kernel-version-4-13-0-26/
- https://devtalk.nvidia.com/default/topic/950647/linux/black-screen-with-367-35
- https://devtalk.nvidia.com/default/topic/838534/can-t-boot-ubuntu-14-04-lts-after-cuda-7-install/?offset=4
- https://devtalk.nvidia.com/default/topic/995429/linux/failed-installed-nvidia-with-kernel-4-10/
- https://devtalk.nvidia.com/default/topic/1028016/patch-for-compiling-v384-98-modules-with-linux-v4-14-9-/
- https://devtalk.nvidia.com/default/topic/1024925/driver-install-issue-with-kernel-4-9-35/