Ok first of all, a big DISCLAIMER here, Nvidia sucks as once said Linus Torvalds, and before trying to install Cuda and company, I never knew why, but after some weeks redoing the process trying a lot of versions and meeting a lot of mistakes I found a way to install these three nightmares in a way that worked for me, now this may not work for you for some reason but at least it is one more tutorial following a different path that may solve yours nightmares. Also this installation was aimed to use they on YoloV4 and worked on it, so the Opencv flags are related to that.
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 9sudo 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
To be sure that you don’t have the drivers installed, because Cuda 11.4 installs the 460 drivers in the process, and removing your drivers may help you dodge some errors (One of the times I had the 470 drivers but after installing even checking out the drivers installation, I got a error saying that I should have a driver with version higher than 460).
After this follow these steps from the Cuda guide:
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
After these steps, The Nvidia docs would say that you have successfully installed Cuda and it is ready to work, but guess what? Nvidia can’t even do a proper installation guide because they forgot to add to path two variables, and without doing this extra step nvcc would not work.
So now to finish your installation go to your ~/.bashrc file and add these two lines on the end of it:
vim ~/.bashrc
#A reminder that the 2 lines below are no commands, they have to be added to the bashrcexport PATH="/usr/local/cuda-11.4/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH"
At this point your Cuda installation should be complete, reboot your pc and type on the terminal these two commands to see if it is working:
nvidia-smi
nvcc --version
If these two commands doesn’t spill any errors in your face you should be ready to go(NOTE: nvidia-smi shows the most recent version of Cuda available, not yours 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.
https://developer.nvidia.com/rdp/cudnn-download
In this site create your account and then agree to the terms, it should appear under it the cuDNN versions, search for the 8.2.2 and then click on the cuDNN Library for Linux (x86_64), after that a tar will be downloaded on your pc, then follow these steps 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 itsudo 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*
Now you think that cuDNN is installed, because Nvidia just show these steps in the installation guide, but guess who forgot again some steps on their own tutorial?
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*
With that done, your cuDNN should be fully installed, reboot your pc and use:
whereis cudnn
If a path appears your cuDNN is working!
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.
First let’s install some dependencies and libraries:
sudo apt-get update
sudo apt-get upgradesudo 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
Now be sure that you have python 3.X installed in your machine for the next steps, if you want you may do this installation inside a virtual env, but unless you need multiple versions of it I would not do this.
So to install it follow these steps:
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
After unzipping these folders you will build it in your machine, be sure to know if the reason for you to install it needs some specific flag, if you don’t know believe in yourself and pray that these flags help you, they worked for me and my aim was to user YoloV4.
cd opencv-4.5.2mkdir buildcd build
Note: the flag CUDA_ARCH_BIN=X.X
needs to be changed to the cc version of your gpu, to know which version to use consult https://developer.nvidia.com/cuda-gpus and search for your gpu. If you can’t find it try googling your gpu computer capability.
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 ..
After that take your time and let cmake do the magic, then use:
nproc # to know how much cores do you havemake -jx #where x is the number of cores, if not sure use -j4sudo make install
Now go out, do some walking, grab an ice cream, wait one extra hour an then return to you pc to see that the installation is still at 90%, jokes aside this make will take some time, so take your time and go do something else.
When it is over use these commands:
sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'sudo ldconfig
And with all that your OpenCV should be working, to see if its ok, start python on terminal and do:
import cv2
print(cv2.getBuildInformation())
If the import works OpenCV is installed, now search in the print output for lines saying Cuda and cuDNNare marked as yes.
If you are not jinxed this tutorial should have worked and now you are a happily owner of a Ubuntu that has Cuda, cuDNN and OpenCV all working together in harmony.
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
https://gist.github.com/raulqf/f42c718a658cddc16f9df07ecc627be7
所有评论(0)