[HowTo] Installing NVIDIA CUDA and cuDNN for Machine Learning

In this post I want to show how to install CUDA & cuDNN as a first step for additional software, e.g. OpenCV or others. However, the following steps are for those who want to go through it step by step themselves without using an AUR.

For this installation, I have selected the current version of CUDA 11.5 (Rev. 1) and cuDNN 8.3.1, also due to the fact that it is the latest version (when this post was written). You can change the versions according to your needs.

cuDNN relies on the CUDA version and therefore it is easy to choose it.

IMPORTANT: You should have the NVIDIA driver already installed (in my case rev. 495), because I didn’t test the installation of the driver myself and it’s also not recommended to use the NVIDIA installer for this. The installer assumes an Ubuntu and is not necessarily designed for ARCH distros.

Let’s go:

1.0 Download the CUDA installer and execute it

cd ~
wget https://developer.download.nvidia.com/compute/cuda/11.5.1/local_installers/cuda_11.5.1_495.29.05_linux.run
chmod +x cuda_11.5.1_495.29.05_linux.run 
./cuda_11.5.1_495.29.05_linux.run

You will get the following screen after you accepted the EULA. In this menu, you have to deselect the driver installation as we already should have it.

Choose Install and be patient, this will take a while to complete.

2.0 Setting the PATH

Next, add the CUDA path to .bashrc:

 nano ~/.bashrc

at the end of the file:

export PATH="${PATH}:/usr/local/cuda-11.5/bin"
export LD_LIBRARY="${LD_LIBRARY}:/usr/local/cuda-11.5/lib64"

and the same for .zshrc; paste the previous lines here too:

nano ~/.zshrc

In addition, add it to ld.so.conf, therefore switch to su and not using sudo:

sudo su
echo /usr/local/cuda-11.5/lib64 >> /etc/ld.so.conf
ldconfig
exit

3.0 Compiling some CUDA tools and picking out the CUDA COMPUTE CAPABILITY

Now, let’s do some compilations to ensure everything is ok and we get the CUDA COMPUTE CAPABILITY of the installed NVIDIA card. This is important, because you need it later e.g. in case you’d like to compile OpenCV or other software.

You can start compiling the examples by executing the commands below. We will then use the deviceQuery to automatically determine the CUDA compute capability:

export CUDA_EXAMPLES_PATH="NVIDIA_CUDA-11.5_Samples"
cd ~/$CUDA_EXAMPLES_PATH
make -j$(nproc)
cd ~

You will find it in the file ~/ComputeCapability.opencv after the compilation is done:

if [ -f ~/$CUDA_EXAMPLES_PATH/1_Utilities/deviceQuery/deviceQuery ];  then 
   ~/$CUDA_EXAMPLES_PATH/1_Utilities/deviceQuery/deviceQuery
   ~/$CUDA_EXAMPLES_PATH/1_Utilities/deviceQuery/deviceQuery | grep "CUDA Capability Major/Minor version number:" >  ~/ComputeCapability.CUDA
   for i in ` sed s'/=/ /g' ~/ComputeCapability.CUDA | awk '{print $6}' `
       do  
       export CUDA_COMPUTE_CAPABILITY=$i
       echo "CUDA_COMPUTE_CAPABILITY "$CUDA_COMPUTE_CAPABILITY                
       awk -v "a=$CUDA_COMPUTE_CAPABILITY" -v "b=10" 'BEGIN {printf "%.0f\n", a*b}' >     ~/ComputeCapability.opencv
   done  
fi

If you do

cat ~/ComputeCapability.opencv

you should get e.g a 2-digit number. This is the CUDA COMPUTE CAPABILITY of your NVIDIA card.

4.0 Download cuDNN

Next, we need cuDNN. Therefore, you have to register an account for free at CUDA Deep Neural Network (cuDNN) | NVIDIA Developer

Select Download and then you can go ahead.

Once you’re in, select the following package:

This is important, because the cuDNN version is depending on the CUDA version! Download the Local Installer for Linux x86_64 (Tar).

4.1 Installing cuDNN

After downloading, the package must be unpacked in the home directory:

chmod +x ~/Downloads/cudnn-linux-x86_64-8.3.1.22_cuda11.5-archive.tar.xz 
tar -xf ~/Downloads/cudnn-linux-x86_64-8.3.1.22_cuda11.5-archive.tar.xz -C ~/  

Installing it:

cd ~
sudo cp cudnn-linux-x86_64-8.3.1.22_cuda11.5-archive/include/cudnn*.h /usr/local/cuda/include  
sudo cp cudnn-linux-x86_64-8.3.1.22_cuda11.5-archive/lib/libcudnn* /usr/local/cuda/lib64  
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
cd /usr/local/cuda/lib64

if [ -f libcudnn.so ];   then sudo rm libcudnn.so;   fi 
if [ -f libcudnn.so.8 ]; then sudo rm libcudnn.so.8; fi 
    
sudo ln libcudnn.so.8.3.1 libcudnn.so.8
sudo ln libcudnn.so.8 libcudnn.so

cd  /usr/local/cuda-11.5/targets/x86_64-linux/lib
if [ -f libcudnn_adv_infer.so ];   then sudo rm libcudnn_adv_infer.so;   fi 
if [ -f libcudnn_ops_train.so ];   then sudo rm libcudnn_ops_train.so;   fi 
if [ -f libcudnn_cnn_train.so ];   then sudo rm libcudnn_cnn_train.so;   fi 
if [ -f libcudnn_cnn_infer.so ];   then sudo rm libcudnn_cnn_infer.so;   fi 
if [ -f libcudnn_adv_train.so ];   then sudo rm libcudnn_adv_train.so;   fi 
if [ -f libcudnn_ops_infer.so ];   then sudo rm libcudnn_ops_infer.so;   fi 
if [ -f libcudnn_adv_infer.so.8 ]; then sudo rm libcudnn_adv_infer.so.8; fi 
if [ -f libcudnn_ops_train.so.8 ]; then sudo rm libcudnn_ops_train.so.8; fi 
if [ -f libcudnn_cnn_train.so.8 ]; then sudo rm libcudnn_cnn_train.so.8; fi 
if [ -f libcudnn_cnn_infer.so.8 ]; then sudo rm libcudnn_cnn_infer.so.8; fi 
if [ -f libcudnn_adv_train.so.8 ]; then sudo rm libcudnn_adv_train.so.8; fi 
if [ -f libcudnn_ops_infer.so.8 ]; then sudo rm libcudnn_ops_infer.so.8; fi 

sudo ln libcudnn_adv_infer.so.8.3.1 libcudnn_adv_infer.so.8
sudo ln libcudnn_ops_train.so.8.3.1 libcudnn_ops_train.so.8
sudo ln libcudnn_cnn_train.so.8.3.1 libcudnn_cnn_train.so.8
sudo ln libcudnn_cnn_infer.so.8.3.1 libcudnn_cnn_infer.so.8
sudo ln libcudnn_adv_train.so.8.3.1 libcudnn_adv_train.so.8
sudo ln libcudnn_ops_infer.so.8.3.1 libcudnn_ops_infer.so.8
sudo ln libcudnn_adv_infer.so.8 libcudnn_adv_infer.so
sudo ln libcudnn_ops_train.so.8 libcudnn_ops_train.so
sudo ln libcudnn_cnn_train.so.8 libcudnn_cnn_train.so
sudo ln libcudnn_cnn_infer.so.8 libcudnn_cnn_infer.so
sudo ln libcudnn_adv_train.so.8 libcudnn_adv_train.so
sudo ln libcudnn_ops_infer.so.8 libcudnn_ops_infer.so

Finalizing:

sudo ldconfig

5.0 Check

After performing the above steps, CUDA as well as cuDNN should now be available. Trying to compile OpenCV shows the successful installation:

there is a version Cuda in community repos manjaro

pacman -Ss cuda
core/icu 70.1-1
    International Components for Unicode library
extra/opencv-cuda 4.5.5-1
    Open Source Computer Vision Library (with CUDA support)
community/cuda 11.5.1-1
    NVIDIA's GPU programming toolkit
community/cuda-tools 11.5.1-1
    NVIDIA's GPU programming toolkit (extra tools: nvvp, nsight, samples)
community/cudnn 8.3.0.98-1
    NVIDIA CUDA Deep Neural Network library
community/python-pycuda 2021.1-8
    Python wrapper for Nvidia CUDA
community/python-pytorch-cuda 1.10.1-1
    Tensors and Dynamic neural networks in Python with strong GPU acceleration
    (with CUDA and AVX2 CPU optimizations)
community/python-tensorflow-cuda 2.7.0-4
    Library for computation using data flow graphs for scalable machine learning
    (with CUDA)
community/python-tensorflow-opt-cuda 2.7.0-4
    Library for computation using data flow graphs for scalable machine learning
    (with CUDA and AVX2 CPU optimizations)
community/root-cuda 6.24.06-12
    C++ data analysis framework and interpreter from CERN with GPU (CUDA)
    features enabled
community/tensorflow-cuda 2.7.0-4
    Library for computation using data flow graphs for scalable machine learning
    (with CUDA)
community/tensorflow-opt-cuda 2.7.0-4
    Library for computation using data flow graphs for scalable machine learning
    (with CUDA and AVX2 CPU optimizations)
multilib/lib32-icu 70.1-1
    International Components for Unicode library (32 bit)
1 Like

There is also a cudnn package
sudo pacman -S cuda cudnn is all it takes

3 Likes

Thank you, haven’t seen it. I’m new to Manjaro, therefore have done it in the traditional way. :smiley: , so to speak…

2 Likes

Actually, this is one of the reasons on why I switched from ubuntu to manjaro, it was an absolute nightmare to install cuda on this distro a couple years ago, whereas on manjaro or any arch based distro, it’s like installing any package.
The funny part is that it should be the other way since nvidia does not even support arch…

1 Like

My situation: pytorch by default supports cuda 11.7, but, alternatively, also provides support for 11.8. I need 11.8 because 11.7 does not support the native capabilities of my card. Whereas 11.7 is in AUR, 11.8 is not.

This is the Arch git recipe:

CUDA

git clone https://gitlab.archlinux.org/archlinux/packaging/packages/cuda.git
cd cuda
git tag --list

chose the right version from the list

git switch --detach 11.8.0-1

edit PDKBUILD to change gcc version if required
cuda 11.8 is incompatible with ggc 13
I installed gcc 11 from AUR (yay gcc11)
in theory gcc 12 should do as well

makepkg
sudo pacman -U cuda-11.8.0-1-x86_64.pkg.tar.zst cuda-tools-11.8.0-1-x86_64.pkg.tar.zst
export MAX_GCC_VERSION=11
sudo ln -s /opt/cuda /usr/local/cuda
sudo ln -s /usr/bin/gcc-$MAX_GCC_VERSION /usr/local/cuda/bin/gcc 
sudo ln -s /usr/bin/g++-$MAX_GCC_VERSION /usr/local/cuda/bin/g++

CUDNN

git clone https://gitlab.archlinux.org/archlinux/packaging/packages/cudnn.git
cd cudnn
git tag --list

chose the right version from the list

git switch --detach 8.9.2.26-1

edit PDKBUILD to change cuda version to 11 and the b2sum of the tar file
b2sum cudnn-linux-x86_64-8.9.2.26_cuda11-archive.tar.xz

makepkg
sudo pacman -U cudnn-8.9.2.26-1-x86_64.pkg.tar.zst
1 Like

Helps a lot. Thanks. :hand_with_index_finger_and_thumb_crossed:Thanks. :hand_with_index_finger_and_thumb_crossed: