1.Uninstalling any cuda/cudnn/nvidia drivers from previous installations

First of all I would highly recommend you to do this process on a fresh Ubuntu install, but if you can’t, you can follow these steps to kill and purge any remains of Nvidia software on your Ubuntu.

sudo apt-get --purge remove "*cublas*" "*cufft*" "*curand*" \
"*cusolver*" "*cusparse*" "*npp*" "*nvjpeg*" "cuda*" "nsight*"
sudo apt-get --purge remove "*nvidia*"
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /usr/local/cuda*

2. Checking your gcc

If you are seeing this tutorial from the future and the newer versions of Cuda are so horrible that you think that is a good idea to go back to some older version, you should first check your gcc and g++ version, because each version of Cuda have a different maximum version supported for these two packages, Cuda 11.4 will support gcc and g++ up to version 10, if yours are higher you should downgrade, to do this follow this:

sudo apt install build-essentialsudo apt -y install gcc-8 g++-8 gcc-9 g++-9sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
sudo update-alternatives --config gcc

3.Installing Cuda 11.4 and drivers

After purging Nvidia and downgrading gcc/g++ if you are from the future, you should be ready to go to the next level, but first use:

nvidia-smi
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pinsudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600wget https://developer.download.nvidia.com/compute/cuda/11.4.1/local_installers/cuda-repo-ubuntu2004-11-4-local_11.4.1-470.57.02-1_amd64.debsudo dpkg -i cuda-repo-ubuntu2004-11-4-local_11.4.1-470.57.02-1_amd64.debsudo apt-key add /var/cuda-repo-ubuntu2004-11-4-local/7fa2af80.pubsudo apt-get updatesudo apt-get -y install cuda
vim ~/.bashrc
#A reminder that the 2 lines below are no commands, they have to be added to the bashrc
export PATH="/usr/local/cuda-11.4/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH"
nvidia-smi
nvcc --version

4. Installing cuDNN 8.2.2

Now to install cuDNN you have to download it, from the Nvidia Developers Program site, and then untar it and do some commands to install it.

# Go to the directory where the tar is located
tar -xqvf cudnn-11.4-linux-x64-v8.2.2.26.tgz
# if they updated the file to 8.2.2.x just change it
sudo cp cuda/include/cudnn*.h /usr/local/cuda/includesudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
sudo cp -P cuda/include/cudnn.h /usr/includesudo cp -P cuda/lib64/libcudnn* /usr/lib/x86_64-linux-gnu/sudo chmod a+r /usr/lib/x86_64-linux-gnu/libcudnn*
whereis cudnn

4.Installing OpenCV 4.5

At this point if you just wanted to install Cuda and cuDNN you are ready to go! But if you want to work with some computer vision stuff, it is time to install OpenCV.

sudo apt-get update
sudo apt-get upgrade
sudo apt install cmake pkg-config unzip yasm git checkinstall libjpeg-dev libpng-dev libtiff-dev libavcodec-dev libavformat-dev libswscale-dev libavresample-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev libfaac-dev libmp3lame-dev libvorbis-dev libopencore-amrnb-dev libopencore-amrwb-devsudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utilscd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
cd ~
sudo apt-get install libgtk-3-dev libtbb-dev libatlas-base-dev gfortran
cd ~/Downloadswget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.5.2.zipwget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.2.zipunzip opencv.zipunzip opencv_contrib.zip
cd opencv-4.5.2mkdir buildcd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=X.X \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib-4.5.2/modules \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF ..
nproc # to know how much cores do you havemake -jx #where x is the number of cores, if not sure use -j4sudo make install
sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'sudo ldconfig
import cv2
print(cv2.getBuildInformation())

Source

CUDA Toolkit Documentation

CUDA Toolkit Documentation - v11.4.1 ( older) - Last updated August 2, 2021 - Send Feedback Release Notes The Release…

docs.nvidia.com

Logo

Ubuntu 社区为您提供最前沿的新闻资讯和知识内容

更多推荐