Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install NVIDIA CUDA 9.0 on Ubuntu 16.04.4 LTS

Updated 4/11/2018

Here's my experience of installing the NVIDIA CUDA kit 9.0 on a fresh install of Ubuntu Desktop 16.04.4 LTS.

Table of Contents

Table of contents generated with markdown-toc

1. Install NVIDIA Graphics Driver via apt-get

Do not use the CUDA run file to install your driver. Use apt-get instead. This way you do not need to worry about the Nouveau stuff you read about on StackOverflow.

As of 04/11/2018, the latest version of NVIDIA driver for Ubuntu 16.04.4 LTS is 384. To install the driver, excute

sudo apt-get nvidia-384 nvidia-modprobe

, and then you will be prompted to disable Secure Boot. Select Disable.

Reboot the machine but enter BIOS to disable Secure Boot. Typically you can enter BIOS by hitting F12 rapidly as soon as the system restarts.

Afterwards, you can check the Installation with the nvidia-smi command, which will report all your CUDA-capable devices in the system.

Common Errors and Solutions

  1. ERROR: Unable to load the 'nvidia-drm' kernel module.
  • One probable reason is that the system is boot from UEFI but Secure Boot option is turned on in the BIOS setting. Turn it off and the problem will be solved.

Additional Notes

nvidia-smi -pm 1 can enable the persistent mode, which will save some time from loading the driver. It will have significant effect on machines with more than 4 GPUs.

nvidia-smi -e 0 can disable ECC on TESLA products, which will provide about 1/15 more video memory. Reboot is reqired for taking effect. nvidia-smi -e 1 can be used to enable ECC again.

nvidia-smi -pl <some power value> can be used for increasing or decrasing the TDP limit of the GPU. Increasing will encourage higher GPU Boost frequency, but is somehow DANGEROUS and HARMFUL to the GPU. Decreasing will help to same some power, which is useful for machines that does not have enough power supply and will shutdown unintendedly when pull all GPU to their maximum load.

-i <GPUID> can be added after above commands to specify individual GPU.

These commands can be added to /etc/rc.local for excuting at system boot.

2. Install CUDA 9.0

Installing CUDA from runfile is much simpler and smoother than installing the NVIDIA driver. It just involves copying files to system directories and has nothing to do with the system kernel or online compilation. Removing CUDA is simply removing the installation directory. So I personally does not recommend adding NVIDIA's repositories and install CUDA via apt-get or other package managers as it will not reduce the complexity of installation or uninstallation but increase the risk of messing up the configurations for repositories.

The CUDA runfile installer can be downloaded from NVIDIA's websie, or using wget in case you can't find it easily on NVIDIA:

cd
wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda_9.0.176_384.81_linux-run

What you download is a package the following three components:

  1. an NVIDIA driver installer, but usually of stale version;
  2. the actual CUDA installer;
  3. the CUDA samples installer;

I suggest extracting the above three components and executing 2 and 3 separately (remember we installed the driver ourselves already). To extract them, execute the runfile installer with --extract option:

cd
chmod +x cuda_9.0.176_384.81_linux-run
./cuda_9.0.176_384.81_linux-run --extract=$HOME

You should have unpacked three components: NVIDIA-Linux-x86_64-384.81.run (1. NVIDIA driver that we ignore), cuda-linux.9.0.176-22781540.run (2. CUDA 9.0 installer), and cuda-samples.9.0.176-22781540-linux.run (3. CUDA 9.0 Samples).

Execute the second one to install the CUDA Toolkit 9.0:

sudo ./cuda-linux.9.0.176-22781540.run

You now have to accept the license by scrolling down to the bottom (hit the "d" key on your keyboard) and enter "accept". Next accept the defaults.

To verify our CUDA installation, install the sample tests by

sudo ./cuda-samples.9.0.176-22781540-linux.run

After the installation finishes, configure the runtime library.

sudo bash -c "echo /usr/local/cuda/lib64/ > /etc/ld.so.conf.d/cuda.conf"
sudo ldconfig

It is also recommended for Ubuntu users to append string /usr/local/cuda/bin to system file /etc/environments so that nvcc will be included in $PATH. This will take effect after reboot. To do that, you just have to

sudo vim /etc/environments

and then add :/usr/local/cuda/bin (including the ":") at the end of the PATH="/blah:/blah/blah" string (inside the quotes).

After a reboot, let's test our installation by making and invoking our tests:

cd /usr/local/cuda-9.0/samples
sudo make

It's a long process with many irrelevant warnings about deprecated architectures (sm_20 and such ancient GPUs). After it completes, run deviceQuery and p2pBandwidthLatencyTest:

cd /usr/local/cuda/samples/bin/x86_64/linux/release
./deviceQuery

The result of running deviceQuery should look something like this:

./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GTX 1060"
  CUDA Driver Version / Runtime Version          9.0 / 9.0
  CUDA Capability Major/Minor version number:    6.1
  Total amount of global memory:                 6073 MBytes (6367739904 bytes)
  (10) Multiprocessors, (128) CUDA Cores/MP:     1280 CUDA Cores
  GPU Max Clock rate:                            1671 MHz (1.67 GHz)
  Memory Clock rate:                             4004 Mhz
  Memory Bus Width:                              192-bit
  L2 Cache Size:                                 1572864 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 9.0, CUDA Runtime Version = 9.0, NumDevs = 1
Result = PASS

Cleanup: if ./deviceQuery works, remember to rm the 4 files (1 downloaded and 3 extracted).

Install cuDNN 7.0

The recommended way for installing cuDNN is to

  1. Download the "cuDNN v7.0.5 Library for Linux" tgz file (need to register for an Nvidia account).

  2. sudo mv the downloaded archive to /usr/local. This might seem silly at first, but when you unzip it next you will see that the contents end up going to various folders under /usr/local/cuda and would be messy to move otherwise.

  3. Then cd /usr/local and extract the tgz by

sudo tar -xvzf cudnn-9.0-linux-x64-v7.tgz
  1. Finally, execute sudo ldconfig to update the shared library cache.

  2. Clean up now or later by sudo rm cudnn-9.0-linux-x64-v7.tgz

@netw0rkf10w

This comment has been minimized.

Copy link

netw0rkf10w commented May 9, 2018

I would like just to thank! This is the only guide that helped me to install CUDA 9.0 on Ubuntu 16.04 without any problem!

@alexampa

This comment has been minimized.

Copy link

alexampa commented May 11, 2018

Thank you for your guide! You can also use
sudo ./cuda-linux.9.0.176-22781540.run -noprompt
sudo ./cuda-samples.9.0.176-22781540-linux.run -cudaprefix=/usr/local/cuda-9.0 -noprompt
so as to accept the license and default paths automatically! (the cuda path given as argument must be the default path set by the installation)

@munir01

This comment has been minimized.

Copy link

munir01 commented May 11, 2018

This was very helpful! Thanks so much! A few minor issues:

The first statement should be:
sudo apt-get install nvidia-384 nvidia-modprobe
Also, by default, cuda is installed in cuda-9.0 folder. So, be cautious about setting paths and installing cudNN. I added these lines in my .bashrc file:
export CUDA_HOME=/usr/local/cuda-9.0
export LD_LIBRARY_PATH=${CUDA_HOME}/lib:$LD_LIBRARY_PATH
export PATH=${CUDA_HOME}/bin:${PATH}

@shizukanaskytree

This comment has been minimized.

Copy link

shizukanaskytree commented May 12, 2018

Thanks. Great post!
A minor error: sudo vim /etc/environments --> sudo vim /etc/environment

@agarwalanant

This comment has been minimized.

Copy link

agarwalanant commented May 30, 2018

Thanks a lot man!, It help me a lot in installing CUDA in my machine.

@rabinnh

This comment has been minimized.

Copy link

rabinnh commented May 31, 2018

To speed up compilation (tremendously) when building the samples use multiple threads:
make -j 6
-j 99 will use all available threads on the machine.

@lkshrsch

This comment has been minimized.

Copy link

lkshrsch commented Jun 6, 2018

Worked perfectly

@mohak1

This comment has been minimized.

Copy link

mohak1 commented Jun 10, 2018

Thank you so much!
I was finally able to install CUDA on my machine.

@gjxdxh

This comment has been minimized.

Copy link

gjxdxh commented Jun 12, 2018

When I do sudo make in the cuda samples, it shows the following errors:

/usr/bin/ld: cannot find -lglut
collect2: error: ld returned 1 exit status
Makefile:293: recipe for target 'simpleTexture3D' failed
make[1]: *** [simpleTexture3D] Error 1
make[1]: Leaving directory '/usr/local/cuda-9.0/samples/2_Graphics/simpleTexture3D'
Makefile:52: recipe for target '2_Graphics/simpleTexture3D/Makefile.ph_build' failed
make: *** [2_Graphics/simpleTexture3D/Makefile.ph_build] Error 2

Do you know how can I solve it? Thank you in advance

@frozflame

This comment has been minimized.

Copy link

frozflame commented Jun 19, 2018

Minor error: sudo apt-get install nvidia-384 nvidia-modprobe.
You omitted the word install.

@kunalchelani

This comment has been minimized.

Copy link

kunalchelani commented Jun 22, 2018

@gjxdxh. I guess you do not have glut installed. You can install it using sudo apt-get install freeglut3-dev and retry making the samples.

@jcolares

This comment has been minimized.

Copy link

jcolares commented Jun 22, 2018

Best tutorial ever! Thanks!

@npatel37

This comment has been minimized.

Copy link

npatel37 commented Jul 16, 2018

Worked flawlessly!
I don't know why other guides are not as clear.
Thank you very very much!

@Tixierae

This comment has been minimized.

Copy link

Tixierae commented Jul 23, 2018

Thank you so much for sharing, this saved me lots of time!

@anik-jha

This comment has been minimized.

Copy link

anik-jha commented Jul 28, 2018

This is a life saver!!
Now I am not afraid to install cuda ever again :)

Although I found the following working for me to install cudnn (from official website):
$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include
$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h
/usr/local/cuda/lib64/libcudnn*

@shivamp21

This comment has been minimized.

Copy link

shivamp21 commented Aug 3, 2018

Can you please give a suggestion on how to apply cuda patches that are given alongside
cuda runfile installer on NVIDIA CUDA website

@srinivasbakki

This comment has been minimized.

Copy link

srinivasbakki commented Aug 7, 2018

After following those steps i still get the error:
./deviceQuery Starting...

CUDA Device Query (Runtime API) version (CUDART static linking)

cudaGetDeviceCount returned 35
-> CUDA driver version is insufficient for CUDA runtime version
Result = FAIL

can you please suggest ?

@ludennis

This comment has been minimized.

Copy link

ludennis commented Sep 11, 2018

Great guide!!!

Minor error as mentioned by @shizukanaskytree still need to be corrected, though.

Thanks,

Dennis

@Gneoma

This comment has been minimized.

Copy link

Gneoma commented Sep 23, 2018

This is a nice description of the loading process that has apparently helped many people, but not me. On the reboot after the install of the graphics driver using the command you suggest, I have the infinite login-loop problem, even though I have set up my fresh install of Ubuntu 16.04 with secure login disabled and made sure that the secure login was disabled in the BIOS. I have been at this for several weeks now and have tried a number of different approaches proposed in other discussions with the same result. No matter what approach I have tried, once I install a Nividia driver or cuda I have the login-loop problem. All the simple fixes that have been proposed, such as resetting permissions or ownership of .Xauthority and other files do not fix problem. However, once I purge the Nvidia driver, the login-loop problem goes away. I have tried to install Nvidia drivers on several vintages of 16.04 and 14.04 with the same result. I am doing this on a Supermicro workstation that was specifically designed to support GPUs. I am puzzled to say the least.

@flydagger

This comment has been minimized.

Copy link

flydagger commented Sep 28, 2018

Many thanks. This guide works on my computer.

@KittyLiou

This comment has been minimized.

Copy link

KittyLiou commented Oct 6, 2018

@gjxdxh I got a similar error too.
Here's the error message:

/usr/bin/ld: cannot find -lglut
collect2: error: ld returned 1 exit status
Makefile:296: recipe for target 'Mandelbrot' failed
make[1]: *** [Mandelbrot] Error 1
make[1]: Leaving directory '/usr/local/cuda-9.0/samples/2_Graphics/Mandelbrot'
Makefile:52: recipe for target '2_Graphics/Mandelbrot/Makefile.ph_build' failed
make: *** [2_Graphics/Mandelbrot/Makefile.ph_build] Error 2

According to the NVIDIA CUDA installation guide,

Some CUDA samples use third-party libraries which may not be installed by default
on your system. These samples attempt to detect any required libraries when building.
If a library is not detected, it waives itself and warns you which library is missing.

we just need to install those third-party libraries to make the CUDA samples run correctly.

For Ubuntu, we just need to install the below:
sudo apt install g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

Everything then works fine.

@david90103

This comment has been minimized.

Copy link

david90103 commented Oct 20, 2018

Thank you!!!
This is the only working way for me.

@ThejanW

This comment has been minimized.

Copy link

ThejanW commented Oct 26, 2018

This helped..thanks 💯

@bibin-sebastian

This comment has been minimized.

Copy link

bibin-sebastian commented Oct 30, 2018

Thank you.. had to use this to set up a gcp vm

@VhaijaiyanthishreeVenkataramanan

This comment has been minimized.

Copy link

VhaijaiyanthishreeVenkataramanan commented Nov 11, 2018

One of the best guide for CUDA installation!! I've been struggling for the last two days and this for sure came to my rescue!! A million thanks to the author !! ^_^

@HareshMiriyala

This comment has been minimized.

Copy link

HareshMiriyala commented Dec 21, 2018

Thanks ! It worked on my GTX 960M

@curtcorum

This comment has been minimized.

Copy link

curtcorum commented Feb 15, 2019

Yes, thank you!
Trouble free for equivalent installation of cuda 9.1 using ppa for nvidia-387

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment