22

I have recently installed tensorflow-gpu using pip. But when I am importing it it is giving the following error:

ImportError: libcudnn.so.7: cannot open shared object file: No such file or directory

I have gone through all the answers of stackoverflow related to this issue but none of them worked for me.

libcudnn.so.7 is present in both the following directories /usr/local/cuda/lib64 and /usr/local/cuda-9.0/lib64 .

Also, I have added the following path in my .bashrc file:

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64\${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64\${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Please help me in resolving this

shivank01
  • 1,015
  • 3
  • 16
  • 35
  • @cdarke Yeah it is present in both /usr/local/cuda/lib64 and /usr/local/cuda-9.0/lib64 – shivank01 Jun 25 '18 at 08:56
  • 1
    What are the actual values of PATH and LD_LIBRARY_PATH in your environment? – Dr. Snoopy Jun 25 '18 at 11:51
  • @MatiasValdenegro I have mentioned the values of PATH and LD_LIBRARY_PATH in the question. – shivank01 Jun 25 '18 at 11:54
  • 1
    Those are not the values, its how you are setting them, we need the actual values that are set in your environment, just do echo $PATH and echo $LD_LIBRARY_PATH in a terminal. – Dr. Snoopy Jun 25 '18 at 11:56
  • @MatiasValdenegro The value of PATH is "-bash: /usr/local/cuda-9.0/bin:/home/tpradhan/anaconda3/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/pycharmCE2017/bin:/usr/java/latest/bin:/opt/eclipse:/opt/ideaCE/bin:/opt/libreofice6.0/:/opt/ubin:/opt/ulibs/Qt5.6.2:/opt/pycharmCE2017/bin: No such file or directory" and the value of LD_LIBRARY_PATH is "/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:}" – shivank01 Jun 25 '18 at 12:00
  • 1
    I think you can see how that the LD_LIBRARY_PATH is wrong. – Dr. Snoopy Jun 25 '18 at 12:01
  • @MatiasValdenegro Can you please elaborate? – shivank01 Jun 25 '18 at 12:02
  • Cuda and Cudnn installation are TWO different thing, and your question has been discussed at length here before: https://askubuntu.com/questions/767269/how-can-i-install-cudnn-on-ubuntu-16-04 – Peter Teoh Apr 02 '19 at 01:09

7 Answers7

15

You are setting LD_LIBRARY_PATH in the wrong way, I would recommend to do it this way (which is kind of the standard):

export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • It is still showing the same error. Now my LD_LIBRARY_PATH is "/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:}" and path is "/home/tpradhan/venvs/tensorflow/bin:/usr/local/cuda-9.0/bin:/home/tpradhan/anaconda3/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/pycharmCE2017/bin:/usr/java/latest/bin:/opt/eclipse:/opt/ideaCE/bin:/opt/libreofice6.0/:/opt/ubin:/opt/ulibs/Qt5.6.2:/opt/pycharmCE2017/bin export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+::/home/tpradhan/.local/bin:/home/tpradhan/bin" . – shivank01 Jun 25 '18 at 12:09
  • 1
    @shivank01 Your LD_LIBRARY_PATH is still wrong, seems you have't changed anything given the output. – Dr. Snoopy Jun 25 '18 at 12:11
  • 1
    I have changed according to your answer. – shivank01 Jun 25 '18 at 12:14
  • @shivank01 On which file exactly? And did you restart the terminal after making changes? – Dr. Snoopy Jun 25 '18 at 12:16
6

You might need to download and install NVIDIA cuDNN.

Download it from https://developer.nvidia.com/rdp/cudnn-download (You have to register an account to download if you don't have). The runtime version is usually more stable than the developer version.

biendltb
  • 1,149
  • 1
  • 13
  • 20
5

Reinstalling CudNN-7.0.5, (make sure you pick the right version from the link below) fixed this for me. You'll need to log in to your Nvidia developer account to access the link. (If you don't have an Nvidia account, creating one is straight forward);

https://developer.nvidia.com/rdp/cudnn-archive

Installation instructions for CudNN; https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html

But I also encountered the following error;

Loaded runtime CuDNN library: 7.0.5 but source was compiled with: 7.4.2. CuDNN library major and minor version needs to match or have higher minor version in case of CuDNN 7.0 or later version. If using a binary install, upgrade your CuDNN library. If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.

Therefore, I had to once again download and install the right CuDNN version, i used the information from the above error message and installed CuDNN 7.4.2 and this fixed all the errors and everything worked fine.

Good Luck!

Sri
  • 51
  • 1
  • 3
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/24909170) – Ṃųỻịgǻňạcểơửṩ Dec 20 '19 at 15:02
  • 1
    Hi there, Thanks for the review on my answer. I have amended the answer after reviewing your comments. The above links now aren't necessarily the answer to this problem considered. But if anyone with the same issue can address the issue by following following what I have suggested, which is to re-install the right CuDNN version that contains the appropriate files and is compatible with the cuda version installed. – Sri Jan 02 '20 at 14:48
0

you add the following path in your .bashrc file:

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATH


export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
Miladfa7
  • 102
  • 1
  • 7
0

FWIW is interested I created a shell script which installs different CUDA versions in Debian which can be easily ported to Ubuntu:

gogasca
  • 9,283
  • 6
  • 80
  • 125
0

I had a libcudnn.so.7 error while trying to install MXNet on Google Colab. What ultimately solved it for me was installing libcudnn7 after some other steps; here's all the major stuff I performed. I hope this helps anyone else who is slogging through this kind of mess like I did.

My specific need was to downgrade Cuda in Google Colab; at the time of writing this it comes with 11.8 but MXNet only supports older versions. I was following this tutorial: https://aconcaguasci.blogspot.com/2019/12/setting-up-cuda-100-for-mxnet-on-google.html

I followed the majority of it including:

#Uninstall the current CUDA version
!apt-get --purge remove cuda nvidia* libnvidia-*
!dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge
!apt-get remove cuda-*
!apt autoremove
!apt-get update
#Download CUDA 10.0
!wget  --no-clobber https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
#install CUDA kit dpkg
# Note: I piped yes to answer the config file prompt with installing new version
!yes | dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
!sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
!apt-get update
!apt-get install cuda-10-0
# Although I did not encounter a `libcurand.so.10` error yet, I still ran this part too:
#Solve libcurand.so.10 error
!wget --no-clobber http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
#-nc, --no-clobber: skip downloads that would download to existing files.
!apt install ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
!apt-get update

HERE was the point where I could pip install mxnet-cu100 and it would fail on import mxnet as mx with "OSError: libcudnn.so.7: cannot open shared object file: No such file or directory".

!find / -iname libcudnn would only return two folders, /var/lib/dpkg/alternatives/libcudnn and /etc/alternatives/libcudnn.

  1. The step that fixed it for me: I dug into the NVIDIA cuDNN Archives right here and found these instructions:

For example, for Cuda 9.0 and cuDNN 7.4.1:

$ sudo apt-get install libcudnn7=7.4.1.5-1+cuda9.0 sudo apt-get install libcudnn7-devel=7.4.1.5-1+cuda9.0

I swapped the Cuda version cuda9.0 for cuda10.0 and ran:

!sudo apt-get install libcudnn7=7.4.1.5-1+cuda10.0

I did not/could not run the libcudnn7-devel because it was "Unable to locate package"

After this, I could pip install mxnet-cu100==1.9.0 (MXNet for Cuda 10.0). And of course nvcc --version would report Cuda 10.0. I was finally able to run import mxnet as mx without getting any "cannot open shared object file: ..." errors. I validated it successfully with:

import mxnet as mx
print(mx.context.num_gpus())
a = mx.nd.ones((2, 3), mx.gpu())
b = a * 2 + 1
print(b.asnumpy())

Outputting:

1
[[3. 3. 3.]
 [3. 3. 3.]]

I realize this was with MXNet and not TensorFlow, but it was a libcudnn.so.7 error and I hope it helps anyone else coming across this, at least with Google Colab. I could not find much support for that recently, hence why I followed that tutorial I mentioned at the top.

  • Your answer may be related to this question, but rather than put your answer here, you could also ask a question ("libcudnn.so.7 error while trying to install MXNet on Google Colab") and answer it yourself. – coradek Jun 21 '23 at 22:24
  • @coradek thank you! Since I already have an answer, what sort of question should I "ask" to answer myself with? Do I try to restate the problem I had before I got my solution? Also, I appreciate your suggested q. name. – rodriguezrrp Jun 23 '23 at 20:41
  • yes, state the problem you had as you understood it before you found the solution, and then post your own answer. This will help others with your problem find a solution relevant to their situation with out having to read through all the answers to this question – coradek Jun 24 '23 at 09:00
-2

The reason is that some libraries are missing. Try installing

sudo apt install libcudnn7
Pipper Tetsing
  • 403
  • 4
  • 5