Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CUDA 10.1 Installation on Ubuntu 18.04
#!/bin/bash
## This gist contains instructions about cuda v10.1 and cudnn 7.6 installation in Ubuntu 18.04 for Tensorflow 2.1.0
### steps ####
# verify the system has a cuda-capable gpu
# download and install the nvidia cuda toolkit and cudnn
# setup environmental variables
# verify the installation
###
### If you have previous installation remove it first.
sudo apt-get purge nvidia*
sudo apt remove nvidia-*
sudo rm /etc/apt/sources.list.d/cuda*
sudo apt-get autoremove && sudo apt-get autoclean
sudo rm -rf /usr/local/cuda*
### to verify your gpu is cuda enable check
lspci | grep -i nvidia
### gcc compiler is required for development using the cuda toolkit. to verify the version of gcc install enter
gcc --version
# system update
sudo apt-get update
sudo apt-get upgrade
# install other import packages
sudo apt-get install g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev
# first get the PPA repository driver
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda.list
sudo apt-get update
# installing CUDA-10.1
sudo apt-get -o Dpkg::Options::="--force-overwrite" install cuda-10-1 cuda-drivers
# setup your paths
echo 'export PATH=/usr/local/cuda-10.1/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
sudo ldconfig
# install cuDNN v7.6
# in order to download cuDNN you have to be regeistered here https://developer.nvidia.com/developer-program/signup
# then download cuDNN v7.6 form https://developer.nvidia.com/cudnn
CUDNN_TAR_FILE="cudnn-10.1-linux-x64-v7.6.5.32.tgz"
wget https://developer.nvidia.com/compute/machine-learning/cudnn/secure/7.6.5.32/Production/10.1_20191031/cudnn-10.1-linux-x64-v7.6.5.32.tgz
tar -xzvf ${CUDNN_TAR_FILE}
# copy the following files into the cuda toolkit directory.
sudo cp -P cuda/include/cudnn.h /usr/local/cuda-10.1/include
sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-10.1/lib64/
sudo chmod a+r /usr/local/cuda-10.1/lib64/libcudnn*
# Finally, to verify the installation, check
nvidia-smi
nvcc -V
# install Tensorflow (an open source machine learning framework)
# I choose version 2.1.0 because it is stable and compatible with CUDA 10.1 Toolkit and cuDNN 7.6
sudo pip3 install --user tensorflow-gpu==2.1.0
@haomingw

This comment has been minimized.

Copy link

haomingw commented Jul 2, 2018

line 32 should be ~/.bashrc?

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Jul 9, 2018

Yes. My mistake. Thanks for your correction.

@zosorock

This comment has been minimized.

Copy link

zosorock commented Aug 11, 2018

Shouldn't it include the updates as well? Or does the base always contains all patches? There are 4 patches in nVidia's website. By the way the latest cuDNN link is
https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v7.2.1/prod/9.0_20180806/cudnn-9.0-linux-x64-v7.2.1.38

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Aug 14, 2018

cuDNN link is now updated !

@kennethyu2017

This comment has been minimized.

Copy link

kennethyu2017 commented Sep 3, 2018

I tried on my ubuntu 18.04 and it works well, thanks for your kindly sharing ~

@indukantdeo

This comment has been minimized.

Copy link

indukantdeo commented Sep 4, 2018

Thanks a lot for sharing, it worked on my laptop.

@bubba-coin

This comment has been minimized.

Copy link

bubba-coin commented Sep 5, 2018

Thank you for the tut

@ast

This comment has been minimized.

Copy link

ast commented Sep 10, 2018

Thanks. This worked great with tensorflow-gpu on Ubuntu 18.04.

@chasedenecke

This comment has been minimized.

Copy link

chasedenecke commented Sep 13, 2018

Thanks! This guide is great!

@Elma-Landro

This comment has been minimized.

Copy link

Elma-Landro commented Sep 14, 2018

Hello guys and primarily, thank you to sharing this kinds of information.
So, i'm a noob and for instance i followed your guide from the begenning to the line #52 and then i'm blocked...
When you say "in order to download cuDNN you have to registered here https://developer.nvidia.com/developer-program/signup

then download cuDNN v7.2 form https://developer.nvidia.com/cudnn": i'm not sure what do you mean...

For my first trial, i I've logged to nvidia, and i download cuda v7.2.1.deb (https://developer.nvidia.com/rdp/cudnn-download) and open it directly with ubuntu appstore, but then the commande line #54 and following give nothing...
For a second trial, after erasing my first installation with appstore, i'm only log to nvidia site and then copy/past the comande line #54 and #55 but here the terminal give me
"elma@elma-UX32VD:$ CUDNN_TAR_FILE="cudnn-9.0-linux-x64-v7.2.1.38"
elma@elma-UX32VD:
$ wget https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v7.2.1/prod/9.0_20180806/${CUDNN_TAR_FILE}
--2018-09-14 17:14:46-- https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v7.2.1/prod/9.0_20180806/cudnn-9.0-linux-x64-v7.2.1.38
Résolution de developer.nvidia.com (developer.nvidia.com)… 192.229.182.215
Connexion à developer.nvidia.com (developer.nvidia.com)|192.229.182.215|:443… connecté.
requête HTTP transmise, en attente de la réponse… 403 Forbidden
2018-09-14 17:14:46 erreur 403 : Forbidden."

So i'm lost, can anyone may give me some advice (in the form of comande line) for helping me to finish this installation?

Thank a lot in advance

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Sep 16, 2018

May be some connectivity issues.

Never mind, In #55 instead of using wget
download "cuDNN v7.2.1 Library for Linux" for CUDA 9.0 manually from https://developer.nvidia.com/rdp/cudnn-download

then use command "scp" to your pure-terminal machine.

@Elma-Landro

This comment has been minimized.

Copy link

Elma-Landro commented Sep 16, 2018

hi, thank for answering!
But for instance, i understand nothing of what i have to do in your previous messge... i'm a blind noob! I can dowload it with the buntu app like in my first trial? and then, what is scp command? pure terminale machine? (sudo?)
Thank

@aaralh

This comment has been minimized.

Copy link

aaralh commented Sep 17, 2018

Thanks for this great guide!

@bikashg

This comment has been minimized.

Copy link

bikashg commented Sep 20, 2018

At the end, I had to exit the terminal running all these commands and start a new terminal so that the "nvcc -V" command was recognized. Overall, a great guide. Saved a plenty of frustrating hours.

@Elma-Landro

This comment has been minimized.

Copy link

Elma-Landro commented Sep 21, 2018

So guys, i understand that my low skill level is the main problem here... but i will try to improve myself. I understand that command #54 #55 correspond to downloading the CuDNN, i don't know why the command line respond me forbbiden but i can try to download it using GUI RIGHT? I have made this, (question what is the cuDNN i have to download exactly: cuDNN v7.3.0 Runtime Library for Ubuntu16.04 (Deb) or cuDNN v7.3.0 Developer Library for Ubuntu16.04 (Deb) or cuDNN v7.3.0 Library for Linux; i'm on Ubuntu 18.04) so normally the cuDNN is on my computer (i have try the Runtime... and the Dev Lib....)

after that, #56 #57: I need to copy some file from a direction to an other (if i try now my computer say that the file didn't exist: sudo cp -P cuda/include/cudnn.h /usr/local/cuda-9.0/include cp: impossible d'évaluer 'cuda/include/cudnn.h': Aucun fichier ou dossier de ce type; so it's inform me that the my Gui downloading doesn't give the good library no? when i check on usr/local/cuda or on usr/local/cuda-9 i don't found the cuda/include/cudnn.h nor cuda/lib64/libcudnn

Please someone, if you have any idea, respond me thank in advance

@idantene

This comment has been minimized.

Copy link

idantene commented Sep 27, 2018

Don't forget the cublas patches (1-4).
Great snippet though, thanks!

@Gambitier

This comment has been minimized.

Copy link

Gambitier commented Oct 6, 2018

I followed the script, but after restarting I encountered black screen for tty login instead of gui login screen.

@Gambitier

This comment has been minimized.

Copy link

Gambitier commented Oct 6, 2018

@Gambitier First of all I wasn't able to resolve that problem. I tried apt purge nvidia-*, but that didn't worked for me the problem persisted. So I freshly installed Ubuntu and followed the guide https://www.linuxbabe.com/desktop-linux/switch-intel-nvidia-graphics-card-ubuntu

The key point was to install recommended Nvidia driver, in my case it was 390. Then I followed rest of your script. Now it's working fine.

@aowais2

This comment has been minimized.

Copy link

aowais2 commented Oct 10, 2018

Lines 34 and 35 have a typo. Should be
chmod +x cuda_9.0.176_384.81_linux.run
instead of
chmod +x cuda_9.0.176_384.81_linux-run
Same with 35

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Oct 10, 2018

Thanks for your correction

@kmamine

This comment has been minimized.

Copy link

kmamine commented Oct 12, 2018

nvidea-smi not working
the following error is being displayed :
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running

every thing else is working !
but when i run a Keras Model it still uses CPU instead of GPU !! i have installed the keras -gpu package and the tensorflow-gpu package!

@mbrad26

This comment has been minimized.

Copy link

mbrad26 commented Oct 17, 2018

A bit of an issue:
chmod +x cuda_9.0.176_384.81_linux.run

returns:
chmod: cannot access 'cuda_9.0.176_384.81_linux.run': No such file or directory

Any ideas?

@developer0hye

This comment has been minimized.

Copy link

developer0hye commented Oct 20, 2018

Someone who has error to build openCV after installing cuda,

look at below links
opencv build gcc g++ compiler error
opencv build nvcc error

@danomois

This comment has been minimized.

Copy link

danomois commented Oct 21, 2018

Same issue as mbrad26:
chmod +x cuda_9.0.176_384.81_linux.run

returns:
chmod: cannot access 'cuda_9.0.176_384.81_linux.run': No such file or directory

FWIW the last line in the terminal before trying shows:
‘cuda_9.0.176_384.81_linux-run.3’ saved [252/252]

So, being a newbie I tried the command with -run, -run.3, .run, .run.3, unless I missed one it does not seem to be working.
Any help would be greatly appreciated - I'd be lost without this instruction

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Oct 22, 2018

@mbrad26 and @danomois, I think the problem may be due to broken file
One way to ensure the file is not broken is to manually check whether or not
the downloaded file cuda_9.0.176_384.81_linux-run file is 1.6 GB.

If you still have problem, try download it manually instead of using "wget" in terminal.

@danomois

This comment has been minimized.

Copy link

danomois commented Oct 22, 2018

Redownload is working so far!
Though with linux-run not linux.run
Also, there seemed to be some intermediate steps regarding location of install?
Anyway, will post again if more fun issues crop up
Thanks Again!

@danomois

This comment has been minimized.

Copy link

danomois commented Oct 22, 2018

Sigh
Any idea what to do with
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 9.0 functionality to work.
To install the driver using this installer, run the following command, replacing with the name of this run file:
sudo .run -silent -driver

For my part, I'm not sure what to replace with to try?

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Oct 23, 2018

It's not a big deal ! Just ignore this warning.

May be it's due to the negation of the driver installation from the CUDA 9.0 package.

@hxyhxym

This comment has been minimized.

Copy link

hxyhxym commented Oct 25, 2018

nvidea-smi not working
the following error is being displayed :
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running

every thing else is working !
but when i run a Keras Model it still uses CPU instead of GPU !! i have installed the keras -gpu package and the tensorflow-gpu package!

hi,
I also have this problem, have you already solved this ?
thanks!

@venkatrebba

This comment has been minimized.

Copy link

venkatrebba commented Oct 31, 2018

It worked, thanks for sharing!

@vthuongt

This comment has been minimized.

Copy link

vthuongt commented Nov 8, 2018

A bit of an issue:
chmod +x cuda_9.0.176_384.81_linux.run

returns:
chmod: cannot access 'cuda_9.0.176_384.81_linux.run': No such file or directory

Any ideas?

This is probably due to the fact that the downloaded file is 'cuda_9.0.176_384.81_linux-run' but the commands in line 34 and 35 refer to 'cuda_9.0.176_384.81_linux.run'

Solution is to rename files I guess:

mv cuda_9.0.176_384.81_linux-run cuda_9.0.176_384.81_linux.run

@srcolinas

This comment has been minimized.

Copy link

srcolinas commented Nov 11, 2018

I followed this steps before installing TensorFlow. However, I got the following error when importing Tensorflow:
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

@flxh

This comment has been minimized.

Copy link

flxh commented Nov 17, 2018

I followed this steps before installing TensorFlow. However, I got the following error when importing Tensorflow:
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

Hey I had the same Issue. I got it fixed by running ldconfig, as suggested in this comment tensorflow/tensorflow#19375 (comment)

@ohenrik

This comment has been minimized.

Copy link

ohenrik commented Nov 22, 2018

After following your guide i got an error when trying to use CUDA with Tensorflow:
tensorflow/tensorflow#19266

The solution offered by @1Mr-Styler, worked for me:
sudo apt install nvidia-modprobe

tensorflow/tensorflow#19266 (comment)

@NightFantom

This comment has been minimized.

Copy link

NightFantom commented Nov 24, 2018

nvidea-smi not working
the following error is being displayed :
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running
every thing else is working !
but when i run a Keras Model it still uses CPU instead of GPU !! i have installed the keras -gpu package and the tensorflow-gpu package!

hi,
I also have this problem, have you already solved this ?
thanks!

I had it. But after reboot of OS it worked.

@wonderingabout

This comment has been minimized.

Copy link

wonderingabout commented Nov 28, 2018

@Mahedi-61

the no such file or directory issue is due to a typo in the name :
(you notice it with ls command)

first, for ubuntu 18.04 forget all what is written here

this is full of compatibility issues

follow these instructions to install cuda 9.0 and cudnn 7.0 for cuda 9.0 in ubuntu 18.04 LTS

first :
i dont recommend using ppa, biggest chance to mess up everything :
just install nvidia-384 (without dev), then reboot, is fine

then i recommend following nvidia official instructions

for ubuntu 18.04 you have to use run file for both cuda and cudnn (no .deb for ubuntu 18.04 cuda 9.0 : max deb file 16.04 for cuda 9.0) :

https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#runfile
https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installcuda

and/or (better) these (it's for ubuntu 16.04 but you can adapt it) :
https://medium.com/@zhanwenchen/install-cuda-and-cudnn-for-tensorflow-gpu-on-ubuntu-79306e4ac04e

during cuda install, it's easier to use nano than vim to edit files though
in nano (^ = ctrl, so it's ctrl+x to save modfications, then press enter), you can verify that file has been modified by relaunching it, then reboot to run sample tests

after reboot, to run sample tests you need to use update-alternatives to install gcc-6 and g++-6 instead of the default gcc7
you will need gcc6 and g++6 max for cuda 9 and then test samples for cuda :
https://askubuntu.com/a/1039919
https://forum.libreelec.tv/thread/12368-fixed-compilation-under-ubuntu-18-04-lts/

gcc --version && g++ --version && sudo apt-get -y install gcc-6 && sudo apt-get -y install g++-6 && sudo update-alternatives --install "/usr/bin/gcc" "gcc" "/usr/bin/gcc-6" 60 --slave "/usr/bin/g++" "g++" "/usr/bin/g++-6" && sudo update-alternatives --config gcc && gcc --version && g++ --version

if you do it like that cuda install works and test sample works too :

./deviceQuery Starting...

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

Detected 1 CUDA Capable device(s)

Device 0: "Tesla K80"
  CUDA Driver Version / Runtime Version          9.1 / 9.0
  CUDA Capability Major/Minor version number:    3.7
  Total amount of global memory:                 11441 MBytes (11996954624 bytes)
  (13) Multiprocessors, (192) CUDA Cores/MP:     2496 CUDA Cores
  GPU Max Clock rate:                            824 MHz (0.82 GHz)
  Memory Clock rate:                             2505 Mhz
  Memory Bus Width:                              384-bit
  L2 Cache Size:                                 1572864 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 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:                     No
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Enabled
  Device supports Unified Addressing (UVA):      Yes
  Supports Cooperative Kernel Launch:            No
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 0 / 4
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

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

then for cudnn run file, you can use this trick to wget cudnn run file on ubuntu 18.04 on command line
https://stackoverflow.com/questions/31279494/how-to-install-cudnn-from-command-line

download cudnn 7.0.x for CUDA 9.0 (tar.gz archive for universal linux, includes ubuntu 18.04)

it would be easier to use .deb for ubuntu, but max is ubuntu 16.04 for cudnn 7.0
still, run files (library tar gz file) are universal so let's learn how to do it ! :)
(cant use deb because max is ubuntu 16.04 as explained here :
https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installcuda

download from (need to create an account, free) :
https://developer.nvidia.com/rdp/cudnn-archive

first use this trick to download cudnn using your firefox link :
https://stackoverflow.com/a/49752272

then "copy original download URL"
and do, for example :
wget PASTE-YOUR-COPIED-LINK-HERE

the link is something like this (unique token that will expire) , for example :
for example :
wget https://developer.download.nvidia.com/compute/machine-learning/cudnn/secure/v7.0.5/prod/9.0_20171129/cudnn-9.0-linux-x64-v7.tgz?oj-SZ9vkkWyIVSb6YfUo6ci2jqR9-E117-7xmyoTFs5h5K3Tnuej1bTtJmGKXIa-V8WFUcevldjedUL2JlYCchL0SNA_wZlzMlc_rJ4Vktc4fbC2bpNZmk8Q-nHcRnIcdGSmVT0YIkBkAglzwcEGP5s9yfx4YlgQhL50yfQNdV6ddTC77StJUKjyLrcH246nbJvvGVn5h94

then rename it to remove temporary download token
The downloaded filename is libcudnn***.deb?. You will need to rename it by stripping the ? and everything after it:

ls to list files then copy name from ls result, then rename it removing :

ls
NVIDIA-Linux-x86_64-384.81.run
cuda-linux.9.0.176-22781540.run
cuda-samples.9.0.176-22781540-linux.run
cuda_9.0.176_384.81_linux-run
'cudnn-9.0-linux-x64-v7.tgz?oj-SZ9vkkWyIVSb6YfUo6ci2jqR9-E117-7xmyoTFs5h5K3Tnuej1bTtJmGKXIa-V8WFUcevldjedUL2JlYCchL0SNA_wZlzMlc_rJ4Vktc4fbC2bpNZmk8Q-nHcRnIcdGSmVT0YIkBkAglzwcEGP5s9yfx4YlgQhL50yfQNdV6ddTC77StJUKjyLrcH246nbJvvGVn5h94'

for example :

mv cudnn-9.0-linux-x64-v7.tgz?oj-SZ9vkkWyIVSb6YfUo6ci2jqR9-E117-7xmyoTFs5h5K3Tnuej1bTtJmGKXIa-V8WFUcevldjedUL2JlYCchL0SNA_wZlzMlc_rJ4Vktc4fbC2bpNZmk8Q-nHcRnIcdGSmVT0YIkBkAglzwcEGP5s9yfx4YlgQhL50yfQNdV6ddTC77StJUKjyLrcH246nbJvvGVn5h94 cudnn-9.0-linux-x64-v7.tgz

result :

ls
NVIDIA-Linux-x86_64-384.81.run cuda-samples.9.0.176-22781540-linux.run cudnn-9.0-linux-x64-v7.tgz
cuda-linux.9.0.176-22781540.run cuda_9.0.176_384.81_linux-run

"install" (more like extract) cudnn 7.0.x for CUDA 9.0 (linux) :

these instructions look great :
https://medium.com/@taylordenouden/installing-tensorflow-gpu-on-ubuntu-18-04-89a142325138

Unpack the archive
tar -zxvf cudnn-9.0-linux-x64-v7.tgz

result :
cuda/include/cudnn.h
cuda/NVIDIA_SLA_cuDNN_Support.txt
cuda/lib64/libcudnn.so
cuda/lib64/libcudnn.so.7
cuda/lib64/libcudnn.so.7.0.5
cuda/lib64/libcudnn_static.a

Move the unpacked contents to your CUDA directory, then # Give read access to all users, Install libcupti, Do the CUDA post-install actions So Tensorflow can find your CUDA installation and use it properly, you need to add these lines to the end of you ~/.bashrc or ~/.zshrc.

sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64/ && sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include/ && sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda/lib64/libcudnn* && sudo apt-get -y install libcupti-dev && export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}} && export LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} && sudo reboot

reboot is needed
if all went it will reboot

revert to gcc7 and g++7 for the future :

last thing, you probably dont need gcc6 and g++6 so revert to gcc7 and g++7

https://codeyarns.com/2015/02/26/how-to-switch-gcc-version-using-update-alternatives/

select the version you want :

gcc --version && g++ --version && sudo apt-get -y install gcc-7 && sudo apt-get -y install g++-7 && sudo update-alternatives --install "/usr/bin/gcc" "gcc" "/usr/bin/gcc-7" 60 --slave "/usr/bin/g++" "g++" "/usr/bin/g++-7" && sudo update-alternatives --config gcc && gcc --version && g++ --version && sudo reboot

reboot is needed

thats it !

hope this helps

@Rahul-Venugopal

This comment has been minimized.

Copy link

Rahul-Venugopal commented Dec 13, 2018

Hi ,
Now I am not able to find cudNN7.2 for cuda 9.0 in nvidia site. Will this steps work if I download cudNN 7.4 for cuda 9.0 ?

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Dec 13, 2018

Sure.
Just change the script according to updated cuDNN link.

@lz-chen

This comment has been minimized.

Copy link

lz-chen commented Dec 26, 2018

Thanks a lot for sharing! I tried it with with tensorflow-gpu on Ubuntu 18.04 and it works:)

@YangLiu14

This comment has been minimized.

Copy link

YangLiu14 commented Jan 6, 2019

This method works on my Ubuntu 18.04, thank you for writing this tutorial!

@Yossarian0916

This comment has been minimized.

Copy link

Yossarian0916 commented Jan 12, 2019

Thank you a lot! The script is of great help.

@siddhantkaushal

This comment has been minimized.

Copy link

siddhantkaushal commented Jan 16, 2019

Hello Friends,

Stuck at line number 54.
getting forbidden error..

Please assist

@zuoanqh

This comment has been minimized.

Copy link

zuoanqh commented Jan 18, 2019

line 34 and 35 needs to end with "-run" not ".run". look at line 31: the file name ends with "-run".

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Jan 19, 2019

Nope. It's alright.
Check the downloaded file.

@DanielRobertAppel

This comment has been minimized.

Copy link

DanielRobertAppel commented Jan 31, 2019

Line 34 : in-which you are making the cuda download executable (chmod +x) you have a typo.

Current line: chmod +x cuda_9.0.176_384.81_linux.run
Should Be: chmod +x cuda_9.0.176_384.81_linux-run

Line 35: in-which you are executing the cuda download. Same typo.

Current line: sudo ./cuda_9.0.176_384.81_linux.run --override
Should Be: sudo ./cuda_9.0.176_384.81_linux-run --override

@raeidsaqur

This comment has been minimized.

Copy link

raeidsaqur commented Feb 6, 2019

Thanks for compiling this. Might want to add the cuBLAS patches installation after cuda toolkit archive.

@Remeus

This comment has been minimized.

Copy link

Remeus commented Feb 8, 2019

It seems to work. Thanks for the wrap-up!

@i-chaochen

This comment has been minimized.

Copy link

i-chaochen commented Mar 5, 2019

Same problem for nvidia-smi. Anyone knows how to solve it?

I tried to install NVIDIA drive 384 and 390, both of them have the following same error.

$ nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

My nvcc is fine thought.
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.176

I can't run nvidia-settings as well.

$ nvidia-settings

ERROR: NVIDIA driver is not loaded

ERROR: Unable to load info from any available system

@biphasic

This comment has been minimized.

Copy link

biphasic commented Mar 7, 2019

NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
had the same issue after fresh 18.04.2 install. Disabled secure boot and it worked!

@Nova-Scotia

This comment has been minimized.

Copy link

Nova-Scotia commented Mar 15, 2019

I had an error when checking to see if the install was successful. I rebooted and then checked the install; it worked.

@dsysoev

This comment has been minimized.

Copy link

dsysoev commented Apr 28, 2019

Correct filename cuda_9.0.176_384.81_linux-run (instead cuda_9.0.176_384.81_linux.run ) in line 34 and 35.
Please fix it.

@prameshbajra

This comment has been minimized.

Copy link

prameshbajra commented May 12, 2019

NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
had the same issue after fresh 18.04.2 install. Disabled secure boot and it worked!

Having the same issue. Any help?

@jisnava

This comment has been minimized.

Copy link

jisnava commented May 16, 2019

It's working Thanq

@mcanet

This comment has been minimized.

Copy link

mcanet commented May 17, 2019

I am not able to download install cuDNN v7.2 or 7.5 (lastest in nvidia cuDNN website) in terminal and i cannot use manual download throught browser since i am using a cloud machine with only ssh remote control.

@prameshbajra

This comment has been minimized.

Copy link

prameshbajra commented May 17, 2019

@mcanet You can download it into your local machine and the upload the downloaded tar.gz into your cloud machine. Also, can you please tell us what cloud machine are you referring to? May be I can help by providing a step wise answer.

@raniaoueslati

This comment has been minimized.

Copy link

raniaoueslati commented Jun 23, 2019

Same problem for nvidia-smi. Anyone knows how to solve it?

I tried to install NVIDIA drive 384 and 390, both of them have the following same error.

$ nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

My nvcc is fine thought.
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.176

I can't run nvidia-settings as well.

$ nvidia-settings

ERROR: NVIDIA driver is not loaded

ERROR: Unable to load info from any available system

I have this same error plz how to slove it ?

@John-Almardeny

This comment has been minimized.

Copy link

John-Almardeny commented Oct 25, 2019

You need:

sudo apt-get update

before

sudo apt-get -o Dpkg::Options::="--force-overwrite" install cuda-10-0 cuda-drivers 
@aivf-compute

This comment has been minimized.

Copy link

aivf-compute commented Oct 29, 2019

great tutorial!
I couldn't execute:
"sudo apt-get -o Dpkg::Options::="--force-overwrite" install cuda-10-0 cuda-drivers"
i got "Unable to locate package cuda-10-0"

i needed to download manually the cuda deb file and to execute the following lines:
sudo dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.148-1_amd64.deb
sudo apt-key add /var/cuda-repo-<version>/7fa2af80.pub
sudo apt-get update

@valdezf10

This comment has been minimized.

Copy link

valdezf10 commented Nov 10, 2019

Hi,

I have been following the steps to install CUDA and tensorflow but when I get to the following step

-Finally, to verify the installation, check

nvidia-smi
nvcc -V

nvidia-smi output:
Failed to initialize NVML: Driver/library version mismatch

nvcc -V output:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130

I am using an RTX 2070 Super and have previously installed an NVIDIA driver 440.35 (neweste version) in Ubuntu 18.04
Any help would be greatly appreciate it!

@Mahedi-61

This comment has been minimized.

Copy link
Owner Author

Mahedi-61 commented Nov 11, 2019

You have probably mixed a previous runfile install with this current install (apt-get).
May a reboot can help

@Ankitjaiswal1201

This comment has been minimized.

Copy link

Ankitjaiswal1201 commented Nov 14, 2019

rstudio/keras#461 (comment)

Cudnn 7.5 is not supported with Tensorflow. It caused me some issues later after installing cudnn 7.5. So install cudnn 7.4 to avoid it.

https://www.tensorflow.org/install/source#linux

@rpvelloso

This comment has been minimized.

Copy link

rpvelloso commented Dec 15, 2019

Hi,

I have been following the steps to install CUDA and tensorflow but when I get to the following step

-Finally, to verify the installation, check

nvidia-smi
nvcc -V

nvidia-smi output:
Failed to initialize NVML: Driver/library version mismatch

nvcc -V output:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130

I am using an RTX 2070 Super and have previously installed an NVIDIA driver 440.35 (neweste version) in Ubuntu 18.04
Any help would be greatly appreciate it!

same problem here. Did you solved it? How?

@mxochicale

This comment has been minimized.

Copy link

mxochicale commented Jan 22, 2020

Hi,
I have been following the steps to install CUDA and tensorflow but when I get to the following step
-Finally, to verify the installation, check

nvidia-smi
nvcc -V

nvidia-smi output:
Failed to initialize NVML: Driver/library version mismatch
nvcc -V output:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130
I am using an RTX 2070 Super and have previously installed an NVIDIA driver 440.35 (neweste version) in Ubuntu 18.04
Any help would be greatly appreciate it!

same problem here. Did you solved it? How?

For the output of nvidia-smi I reboot my machine and for the nvcc -V I forget to activate my virtual environment which fixed the problem by simply source ~/.bashrc.

Many thanks @Mahedi-61 for your nice script!

@tahakucukkatirci

This comment has been minimized.

Copy link

tahakucukkatirci commented Mar 26, 2020

Great script. Thanks for sharing. You saved me hours.

@MittalShruti

This comment has been minimized.

Copy link

MittalShruti commented Apr 1, 2020

I installed cuda10.1 on google colab.
After source ~/.bashrc, I get
on running
!nvcc -V
/bin/bash: nvcc: command not found

@espoirMur

This comment has been minimized.

Copy link

espoirMur commented May 3, 2020

@Mahedi-61, Line 38 , we should also do :

sudo apt-get update

@espoirMur

This comment has been minimized.

Copy link

espoirMur commented May 3, 2020

@Mahedi-61

the no such file or directory issue is due to a typo in the name :
(you notice it with ls command)

first, for ubuntu 18.04 forget all what is written here

this is full of compatibility issues

follow these instructions to install cuda 9.0 and cudnn 7.0 for cuda 9.0 in ubuntu 18.04 LTS

first :
i dont recommend using ppa, biggest chance to mess up everything :
just install nvidia-384 (without dev), then reboot, is fine

then i recommend following nvidia official instructions

for ubuntu 18.04 you have to use run file for both cuda and cudnn (no .deb for ubuntu 18.04 cuda 9.0 : max deb file 16.04 for cuda 9.0) :

https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#runfile
https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installcuda

and/or (better) these (it's for ubuntu 16.04 but you can adapt it) :
https://medium.com/@zhanwenchen/install-cuda-and-cudnn-for-tensorflow-gpu-on-ubuntu-79306e4ac04e

during cuda install, it's easier to use nano than vim to edit files though
in nano (^ = ctrl, so it's ctrl+x to save modfications, then press enter), you can verify that file has been modified by relaunching it, then reboot to run sample tests

after reboot, to run sample tests you need to use update-alternatives to install gcc-6 and g++-6 instead of the default gcc7
you will need gcc6 and g++6 max for cuda 9 and then test samples for cuda :
https://askubuntu.com/a/1039919
https://forum.libreelec.tv/thread/12368-fixed-compilation-under-ubuntu-18-04-lts/

gcc --version && g++ --version && sudo apt-get -y install gcc-6 && sudo apt-get -y install g++-6 && sudo update-alternatives --install "/usr/bin/gcc" "gcc" "/usr/bin/gcc-6" 60 --slave "/usr/bin/g++" "g++" "/usr/bin/g++-6" && sudo update-alternatives --config gcc && gcc --version && g++ --version

if you do it like that cuda install works and test sample works too :

./deviceQuery Starting...

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

Detected 1 CUDA Capable device(s)

Device 0: "Tesla K80"
  CUDA Driver Version / Runtime Version          9.1 / 9.0
  CUDA Capability Major/Minor version number:    3.7
  Total amount of global memory:                 11441 MBytes (11996954624 bytes)
  (13) Multiprocessors, (192) CUDA Cores/MP:     2496 CUDA Cores
  GPU Max Clock rate:                            824 MHz (0.82 GHz)
  Memory Clock rate:                             2505 Mhz
  Memory Bus Width:                              384-bit
  L2 Cache Size:                                 1572864 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 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:                     No
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Enabled
  Device supports Unified Addressing (UVA):      Yes
  Supports Cooperative Kernel Launch:            No
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 0 / 4
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

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

then for cudnn run file, you can use this trick to wget cudnn run file on ubuntu 18.04 on command line
https://stackoverflow.com/questions/31279494/how-to-install-cudnn-from-command-line

download cudnn 7.0.x for CUDA 9.0 (tar.gz archive for universal linux, includes ubuntu 18.04)

it would be easier to use .deb for ubuntu, but max is ubuntu 16.04 for cudnn 7.0
still, run files (library tar gz file) are universal so let's learn how to do it ! :)
(cant use deb because max is ubuntu 16.04 as explained here :
https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installcuda

download from (need to create an account, free) :
https://developer.nvidia.com/rdp/cudnn-archive

first use this trick to download cudnn using your firefox link :
https://stackoverflow.com/a/49752272

then "copy original download URL"
and do, for example :
wget PASTE-YOUR-COPIED-LINK-HERE

the link is something like this (unique token that will expire) , for example :
for example :
wget https://developer.download.nvidia.com/compute/machine-learning/cudnn/secure/v7.0.5/prod/9.0_20171129/cudnn-9.0-linux-x64-v7.tgz?oj-SZ9vkkWyIVSb6YfUo6ci2jqR9-E117-7xmyoTFs5h5K3Tnuej1bTtJmGKXIa-V8WFUcevldjedUL2JlYCchL0SNA_wZlzMlc_rJ4Vktc4fbC2bpNZmk8Q-nHcRnIcdGSmVT0YIkBkAglzwcEGP5s9yfx4YlgQhL50yfQNdV6ddTC77StJUKjyLrcH246nbJvvGVn5h94

then rename it to remove temporary download token
The downloaded filename is libcudnn***.deb?. You will need to rename it by stripping the ? and everything after it:

ls to list files then copy name from ls result, then rename it removing :

ls
NVIDIA-Linux-x86_64-384.81.run
cuda-linux.9.0.176-22781540.run
cuda-samples.9.0.176-22781540-linux.run
cuda_9.0.176_384.81_linux-run
'cudnn-9.0-linux-x64-v7.tgz?oj-SZ9vkkWyIVSb6YfUo6ci2jqR9-E117-7xmyoTFs5h5K3Tnuej1bTtJmGKXIa-V8WFUcevldjedUL2JlYCchL0SNA_wZlzMlc_rJ4Vktc4fbC2bpNZmk8Q-nHcRnIcdGSmVT0YIkBkAglzwcEGP5s9yfx4YlgQhL50yfQNdV6ddTC77StJUKjyLrcH246nbJvvGVn5h94'

for example :

mv cudnn-9.0-linux-x64-v7.tgz?oj-SZ9vkkWyIVSb6YfUo6ci2jqR9-E117-7xmyoTFs5h5K3Tnuej1bTtJmGKXIa-V8WFUcevldjedUL2JlYCchL0SNA_wZlzMlc_rJ4Vktc4fbC2bpNZmk8Q-nHcRnIcdGSmVT0YIkBkAglzwcEGP5s9yfx4YlgQhL50yfQNdV6ddTC77StJUKjyLrcH246nbJvvGVn5h94 cudnn-9.0-linux-x64-v7.tgz

result :

ls
NVIDIA-Linux-x86_64-384.81.run cuda-samples.9.0.176-22781540-linux.run cudnn-9.0-linux-x64-v7.tgz
cuda-linux.9.0.176-22781540.run cuda_9.0.176_384.81_linux-run

"install" (more like extract) cudnn 7.0.x for CUDA 9.0 (linux) :

these instructions look great :
https://medium.com/@taylordenouden/installing-tensorflow-gpu-on-ubuntu-18-04-89a142325138

Unpack the archive
tar -zxvf cudnn-9.0-linux-x64-v7.tgz

result :
cuda/include/cudnn.h
cuda/NVIDIA_SLA_cuDNN_Support.txt
cuda/lib64/libcudnn.so
cuda/lib64/libcudnn.so.7
cuda/lib64/libcudnn.so.7.0.5
cuda/lib64/libcudnn_static.a

Move the unpacked contents to your CUDA directory, then # Give read access to all users, Install libcupti, Do the CUDA post-install actions So Tensorflow can find your CUDA installation and use it properly, you need to add these lines to the end of you ~/.bashrc or ~/.zshrc.

sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64/ && sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include/ && sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda/lib64/libcudnn* && sudo apt-get -y install libcupti-dev && export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}} && export LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} && sudo reboot

reboot is needed
if all went it will reboot

revert to gcc7 and g++7 for the future :

last thing, you probably dont need gcc6 and g++6 so revert to gcc7 and g++7

https://codeyarns.com/2015/02/26/how-to-switch-gcc-version-using-update-alternatives/

select the version you want :

gcc --version && g++ --version && sudo apt-get -y install gcc-7 && sudo apt-get -y install g++-7 && sudo update-alternatives --install "/usr/bin/gcc" "gcc" "/usr/bin/gcc-7" 60 --slave "/usr/bin/g++" "g++" "/usr/bin/g++-7" && sudo update-alternatives --config gcc && gcc --version && g++ --version && sudo reboot

reboot is needed

thats it !

hope this helps

Bro, you are a lifesaver the firefox download lin link..... @nahedi-61 , we should include it in the guide

@Kreyren

This comment has been minimized.

Copy link

Kreyren commented May 14, 2020

Sorry, but this is shell malware that none should be using namely:

  1. Lack of sanitization so code alike https://gist.github.com/Mahedi-61/2a2f1579d4271717d421065168ce6a73#file-cuda_10-1_installation_on_ubuntu_18-04-L45 is placing these lines on every invokation resulting in overwhelming spam.

You should be using something alike:

if ! grep pattern path/to/file; then printf '%s\n' "expected content" > path/to/file; fi
  1. Other various issues mensioned on https://shellcheck.net/?id=cb40763 -> ALWAYS use shellcheck (linting) if you are writing a shell/bash script

I am willing to make a contribution if you provide an abstract to what needs to be done and why as in-code documentation and assuming this being released under FSF approved license

@naknakLEE

This comment has been minimized.

Copy link

naknakLEE commented Jun 3, 2020

Thanks for this great guide!

@supersexy

This comment has been minimized.

Copy link

supersexy commented Jun 24, 2020

@Kreyren - where can I download your much better version of this? I can not find it. Thanks!

@Kreyren

This comment has been minimized.

Copy link

Kreyren commented Jun 24, 2020

@supersexy I would need an abstract for that version meaning what the script should do, how and when which wasn't provided yet and i am not motivated enough to try to reverse engineer it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.