ubuntu开发环境配置(cuda、cudnn、ffmpeg、opencv、darknet-master、TensorRT、python、pytorch、MySql)

版本介绍:
电脑:笔记本
显卡:RTX3050
ubuntu:20.04
cuda:11.6.2
cudnn:8.4.1
ffmpeg:4.4.3
opencv:4.5.5
darknet-master:yolo-v4
TensorRT:8.4.3.1
python:3.8
pytorch:1.12.1
MySql:8.24

开始配置

一、基础环境依赖

1.更新

sudo apt update
sudo apt upgrade

2.基础工具

sudo apt install build-essential \
cmake \
mlocate \
git \
python3-pip

图形界面相关

sudo apt install lightdm

注:在弹出对话框选择"lightdm"

二、显卡驱动与cuda-11.6.2

1.文件下载

网址

https://developer.nvidia.com/cuda-toolkit-archive

依次点击

(1)“CUDA Toolkit 11.6.2”

(2)“Linux”

(3)“x86_64”

(4)“Ubuntu”

(5)“20.04”

(6)“runfile(local)”

在"Installation Instructions:"下方为下载安装指令
下载指令(文件需下载到英文路径)

wget https://developer.download.nvidia.com/compute/cuda/11.6.2/local_installers/cuda_11.6.2_510.47.03_linux.run

安装指令

sudo sh cuda_11.6.2_510.47.03_linux.run

2.安装准备

(1)卸载原有驱动

sudo apt remove --purge nvidia*

(2)禁用nouveau

备份文件

sudo cp /etc/modprobe.d/blacklist.conf /etc/modprobe.d/blacklist.conf.backup

打开文件

sudo gedit /etc/modprobe.d/blacklist.conf

修改文件

在文件末尾添加如下内容

blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off

保存后关闭文件

关闭nouveau

echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf

更新

sudo update-initramfs -u

重启电脑

reboot

重启后查看是否禁用成功

执行以下内容,没有任何输出内容则为成功禁用

lsmod | grep nouveau

3.安装

(1)按下组合键ctrl+alt+f3(f1-f6均可)进入字符界面

注:进入界面后先登陆,先输入本机名回车后输入密码即可(小键盘不可用)

(2)关闭图形界面服务

sudo service lightdm stop

(3)进入存放cuda安装包目录

cd /home/heqingchun/softWare/files/CUDA

(4)赋予可执行权限

chmod 755 cuda_11.6.2_510.47.03_linux.run

(5)运行安装

sudo sh cuda_11.6.2_510.47.03_linux.run

注:期间会弹出对话框,需手动输入"accept"回车,之后再弹出对话框向下选择"install"后等待安装完毕即可,安装完毕后驱动也一起装完了

(6)安装完毕重启电脑

sudo service lightdm start && reboot

注:
1.需要bios禁用安全启动
2.重启的时候如果电脑可切换独显与混合显示模式需要切换到独显直连

4.环境配置

(1)打开文件

sudo gedit /etc/profile

(2)修改文件

在文件最后加上以下内容

export PATH=$PATH:/usr/local/cuda/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64

(3)更新

source /etc/profile

(4)重启电脑

reboot

5.验证

(1)版本信息

nvcc -V

显示如下:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Tue_Mar__8_18:18:20_PST_2022
Cuda compilation tools, release 11.6, V11.6.124
Build cuda_11.6.r11.6/compiler.31057947_0

(2)库信息

cat /usr/local/cuda/version.json

显示如下:

{
   "cuda" : {
      "name" : "CUDA SDK",
      "version" : "11.6.20220318"
   },
   "cuda_cccl" : {
      "name" : "CUDA C++ Core Compute Libraries",
      "version" : "11.6.55"
   },
   "cuda_cudart" : {
      "name" : "CUDA Runtime (cudart)",
      "version" : "11.6.55"
   },
   "cuda_cuobjdump" : {
      "name" : "cuobjdump",
      "version" : "11.6.124"
   },
   "cuda_cupti" : {
      "name" : "CUPTI",
      "version" : "11.6.124"
   },
   "cuda_cuxxfilt" : {
      "name" : "CUDA cu++ filt",
      "version" : "11.6.124"
   },
   "cuda_demo_suite" : {
      "name" : "CUDA Demo Suite",
      "version" : "11.6.55"
   },
   "cuda_gdb" : {
      "name" : "CUDA GDB",
      "version" : "11.6.124"
   },
   "cuda_memcheck" : {
      "name" : "CUDA Memcheck",
      "version" : "11.6.124"
   },
   "cuda_nsight" : {
      "name" : "Nsight Eclipse Plugins",
      "version" : "11.6.124"
   },
   "cuda_nvcc" : {
      "name" : "CUDA NVCC",
      "version" : "11.6.124"
   },
   "cuda_nvdisasm" : {
      "name" : "CUDA nvdisasm",
      "version" : "11.6.124"
   },
   "cuda_nvml_dev" : {
      "name" : "CUDA NVML Headers",
      "version" : "11.6.55"
   },
   "cuda_nvprof" : {
      "name" : "CUDA nvprof",
      "version" : "11.6.124"
   },
   "cuda_nvprune" : {
      "name" : "CUDA nvprune",
      "version" : "11.6.124"
   },
   "cuda_nvrtc" : {
      "name" : "CUDA NVRTC",
      "version" : "11.6.124"
   },
   "cuda_nvtx" : {
      "name" : "CUDA NVTX",
      "version" : "11.6.124"
   },
   "cuda_nvvp" : {
      "name" : "CUDA NVVP",
      "version" : "11.6.124"
   },
   "cuda_samples" : {
      "name" : "CUDA Samples",
      "version" : "11.6.101"
   },
   "cuda_sanitizer_api" : {
      "name" : "CUDA Compute Sanitizer API",
      "version" : "11.6.124"
   },
   "libcublas" : {
      "name" : "CUDA cuBLAS",
      "version" : "11.9.2.110"
   },
   "libcufft" : {
      "name" : "CUDA cuFFT",
      "version" : "10.7.2.124"
   },
   "libcurand" : {
      "name" : "CUDA cuRAND",
      "version" : "10.2.9.124"
   },
   "libcusolver" : {
      "name" : "CUDA cuSOLVER",
      "version" : "11.3.4.124"
   },
   "libcusparse" : {
      "name" : "CUDA cuSPARSE",
      "version" : "11.7.2.124"
   },
   "libnpp" : {
      "name" : "CUDA NPP",
      "version" : "11.6.3.124"
   },
   "libnvjpeg" : {
      "name" : "CUDA nvJPEG",
      "version" : "11.6.2.124"
   },
   "nsight_compute" : {
      "name" : "Nsight Compute",
      "version" : "2022.1.1.2"
   },
   "nsight_systems" : {
      "name" : "Nsight Systems",
      "version" : "2021.5.2.53"
   },
   "nvidia_driver" : {
      "name" : "NVIDIA Linux Driver",
      "version" : "510.47.03"
   }
}

(3)计算能力

cd /usr/local/cuda/extras/demo_suite
./deviceQuery

显示:

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.6, CUDA Runtime Version = 11.6, NumDevs = 1, Device0 = NVIDIA GeForce RTX 3050 Laptop GPU
Result = PASS

到这里显卡驱动与cuda安装完毕

三、cudnn-8.4.1

1.文件下载

网址

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

依次点击

(1)“Download cuDNN v8.4.1 (May 27th, 2022), for CUDA 11.x”

(2)“Local Installer for Linux x86_64 (Tar)”

注:需要登陆,登陆成功后即可下载

2.安装

(1)进入下载文件所在目录

cd /home/heqingchun/softWare/files/CUDA

(2)解压文件

tar -xvf cudnn-linux-x86_64-8.4.1.50_cuda11.6-archive.tar.xz

(3)解压后文件夹

cd cudnn-linux-x86_64-8.4.1.50_cuda11.6-archive

(4)拷贝文件

sudo cp include/* /usr/local/cuda-11.6/include && \
sudo cp -P lib/* /usr/local/cuda-11.6/lib64 && \
sudo chmod a+r /usr/local/cuda-11.6/include/cudnn*.h /usr/local/cuda-11.6/lib64/libcudnn*

3.环境配置

与cuda同环境

4.验证

cat /usr/local/cuda/include/cudnn_version.h

显示如下:

/**
 * \file: The master cuDNN version file.
 */

#ifndef CUDNN_VERSION_H_
#define CUDNN_VERSION_H_

#define CUDNN_MAJOR 8
#define CUDNN_MINOR 4
#define CUDNN_PATCHLEVEL 1

#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#endif /* CUDNN_VERSION_H */

到这里cudnn安装完毕

4.ffmpeg-4.4.3

1.文件下载

网址

http://ffmpeg.org/download.html#releases

点击
FFmpeg 4.4.3 “Rao"下的"Download xz tarball”

2.安装SDK与依赖

(1)nvidia硬件加速SDK

SDK头文件

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make
sudo make install

(2)依赖

sudo apt install gnutls-dev \
libass-dev \
libdrm-dev \
libopus-dev \
libpulse-dev \
libspeex-dev \
libtheora-dev \
libtwolame-dev \
libv4l-dev \
libvorbis-dev \
libvpx-dev \
libx264-dev \
libx265-dev \
libxvidcore-dev \
libopenal-dev \
libomxil-bellagio-dev \
libxcb1-dev \
libsdl2-dev \
libva-dev \
libvdpau-dev

libfdk_aac
下载网址

https://www.linuxfromscratch.org/blfs/view/svn/multimedia/fdk-aac.html

解压

tar -xvf fdk-aac-2.0.2.tar.gz

编译安装

cd fdk-aac-2.0.2 && \
./configure --prefix=/usr --disable-static && \
make -j12 && \
sudo make install

libmp3lame
下载网址

https://sourceforge.net/projects/lame/files/lame/3.99/

解压

tar -xvf lame-3.99.5.tar.gz

编译安装

cd lame-3.99.5 && \
./configure --prefix=/usr --disable-static && \
make -j12 && \
sudo make install

libopencore_amrnb
下载网址

https://sourceforge.net/projects/opencore-amr/files/opencore-amr/

解压

 tar -xvf opencore-amr-0.1.6.tar.gz

编译安装

cd opencore-amr-0.1.6 && \
./configure --prefix=/usr --disable-static && \
make -j12 && \
sudo make install

3.安装准备

(1)解压进入ffmpeg文件夹

cd /home/heqingchun/softWare/files/ffmpeg && \
tar -xvf ffmpeg-4.4.3.tar.xz && \
cd ffmpeg-4.4.3

(2)修改configure文件

gedit configure

找到

nvccflags_default="-gencode arch=compute_30,code=sm_30 -O2"

改为

nvccflags_default="-gencode arch=compute_86,code=sm_86 -O2"

保存退出
注:75为显卡算力,根据自己的显卡输入即可,查看点这里

4.编译安装

(1)configure

./configure --prefix=/usr/local/ffmpeg \
--disable-debug \
--disable-doc \
--disable-static \
--enable-cuda-nvcc \
--enable-cuvid \
--enable-libdrm \
--enable-ffplay \
--enable-gnutls \
--enable-gpl \
--enable-libass \
--enable-libfdk-aac \
--enable-libfontconfig \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libnpp \
--enable-libopencore_amrnb \
--enable-libopencore_amrwb \
--enable-libopus \
--enable-libpulse \
--enable-sdl2 \
--enable-libspeex \
--enable-libtheora \
--enable-libtwolame \
--enable-libv4l2 \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-libxcb \
--enable-libxvid \
--enable-nonfree \
--enable-nvenc \
--enable-omx \
--enable-openal \
--enable-opencl \
--enable-runtime-cpudetect \
--enable-shared \
--enable-vaapi \
--enable-vdpau \
--enable-version3 \
--enable-xlib \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--libdir=/usr/lib/x86_64-linux-gnu \
--incdir=/usr/include/x86_64-linux-gnu \
--disable-asm \
--disable-x86asm \
--extra-cflags=-fPIC \
--toolchain=hardened \
--disable-stripping \
--extra-cflags=-I/usr/local/include/ffnvcodec

(2)make&make install

make -j12
sudo make install

5.环境配置

(1)链接

sudo ln -s /usr/local/ffmpeg/bin/ffmpeg /usr/bin/ffmpeg && \
sudo ln -s /usr/local/ffmpeg/bin/ffprobe /usr/bin/ffprobe && \
sudo ln -s /usr/local/ffmpeg/bin/ffplay /usr/bin/ffplay && \
sudo ln -s /usr/local/ffmpeg/bin/ffmpeg /usr/local/bin/ffmpeg && \
sudo ln -s /usr/local/ffmpeg/bin/ffprobe /usr/local/bin/ffprobe && \
sudo ln -s /usr/local/ffmpeg/bin/ffplay /usr/local/bin/ffplay

(2)库

打开文件

sudo gedit /etc/ld.so.conf.d/ffmpeg.conf

修改文件

/usr/local/ffmpeg/lib

保存更新

sudo ldconfig

6.验证

查看支持的硬件加速选项

ffmpeg -hwaccels

显示如下:

Hardware acceleration methods:
vdpau
cuda
vaapi
drm
opencl

测试:
HEVC->H.264

ffmpeg -vcodec hevc_cuvid -an -gpu 0 -i  -vcodec h264_nvenc -an -gpu 0 -y output.mp4

H.264->HEVC

ffmpeg -vcodec h264_cuvid -an -gpu 0 -i  -vcodec hevc_nvenc -an -gpu 0 -y output.mp4

到这里ffmpeg安装完毕

5.opencv-4.5.5

1.文件下载

网址

https://github.com/opencv

下载"opencv-4.5.5.zip"与"opencv_contrib-4.5.5.zip"
解压&整理

unzip opencv-4.5.5.zip && \
unzip opencv_contrib-4.5.5.zip && \
mv opencv_contrib-4.5.5 opencv-4.5.5 && \

2.下载依赖文件与依赖环境

(1)依赖文件

文件存放路径

mkdir /home/heqingchun/opencv_cmake_download_files
cd opencv_cmake_download_files

1、

wget https://github.com/opencv/ade/archive/refs/tags/v0.1.1f.zip

2、

wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i && \
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i && \
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i && \
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i && \
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i

3、

wget https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.caffemodel && \
wget https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.prototxt

4、

wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat

5、

wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_win_intel64_20191018_general.zip

6、

wget https://github.com/NVIDIA/NVIDIAOpticalFlowSDK/archive/edb50da3cf849840d680249aa6dbef248ebce2ca.zip

7、

wget https://github.com/01org/tbb/archive/v2020.2.tar.gz

8、

wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i && \
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i && \
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i

(2)依赖

gtk

sudo apt install libgtk2.0-dev \
libcanberra-gtk-module

opengl

sudo apt install libgl1-mesa-dev \
libglew-dev \
libsdl2-dev \
libsdl2-image-dev \
libglm-dev \
libfreetype6-dev \
libglfw3-dev \
libglfw3 \
libglu1-mesa-dev \
freeglut3-dev \
libgtkglext1 \
libgtkglext1-dev

gstreamer

sudo apt install libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-bad1.0-dev \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
gstreamer1.0-doc \
gstreamer1.0-tools \
gstreamer1.0-x \
gstreamer1.0-alsa \
gstreamer1.0-gl \
gstreamer1.0-gtk3 \
gstreamer1.0-qt5 \
gstreamer1.0-pulseaudio

3.安装准备

(1)如果使用nvcuvid

下载文件

https://developer.nvidia.com/nvidia-video-codec-sdk

依次点击" Get Started "、“I Agree to the Terms of the”、“DOWNLOAD NOW”
下载"Video_Codec_SDK_12.0.16.zip"文件
解压文件

unzip Video_Codec_SDK_12.0.16

复制文件

cd Video_Codec_SDK_12.0.16/Interface
sudo cp cuviddec.h nvcuvid.h nvEncodeAPI.h /usr/include

修改文件
opencv-4.5.5/cmake/OpenCVDetectCUDA.cmake文件中

PATHS "${CUDA_TOOLKIT_TARGET_DIR}" "${CUDA_TOOLKIT_ROOT_DIR}"

改为

PATHS "${CUDA_TOOLKIT_TARGET_DIR}" "${CUDA_TOOLKIT_ROOT_DIR}" "/usr/include"

(2)修改文件

将下列文件中网络路径改为前面下载文件的路径
opencv-4.5.5/modules/gapi/cmake/DownloadADE.cmake
opencv-4.5.5/opencv_contrib-4.5.5/modules/xfeatures2d/cmake/download_boostdesc.cmake
opencv-4.5.5/opencv_contrib-4.5.5/modules/wechat_qrcode/CMakeLists.txt
opencv-4.5.5/opencv_contrib-4.5.5/modules/face/CMakeLists.txt
opencv-4.5.5/3rdparty/ippicv/ippicv.cmake
opencv-4.5.5/opencv_contrib-4.5.5/modules/cudaoptflow/CMakeLists.txt
opencv-4.5.5/3rdparty/tbb/CMakeLists.txt
opencv-4.5.5/opencv_contrib-4.5.5/modules/xfeatures2d/cmake/download_vgg.cmake
如:
opencv-4.5.5/modules/gapi/cmake/DownloadADE.cmake中

"https://github.com/opencv/ade/archive/"

改为

"file:/home/heqingchun/opencv_cmake_download_files/"

4.编译安装

(1)cmake

cmake -D CMAKE_INSTALL_PREFIX=/usr/local \
-D CMAKE_BUILD_TYPE=Release \
-D BUILD_opencv_world=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=ON \
-D ENABLE_FAST_MATH=ON \
-D BUILD_ITT=OFF \
-D WITH_ITT=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.5.5/modules \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_GTK_2_X=ON \
-D WITH_OPENGL=ON \
-D WITH_VTK=ON \
-D WITH_OPENCL=ON \
-D WITH_V4L=ON \
-D BUILD_TBB=ON  \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D OPENCV_DNN_CUDA=ON \
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN=8.6 \
-D CUDA_ARCH_PTX=8.6 \
-D CUDA_FAST_MATH=1 \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D WITH_CUDNN=ON \
-D WITH_NVCUVID=ON \
-D WITH_CUBLAS=ON \
..

其中
-D CUDA_ARCH_BIN=8.6
-D CUDA_ARCH_PTX=8.6
需要按照自己显卡的算力来填写

(2)make&make install

make -j12
sudo make install

5.环境配置

(1)

打开文件

sudo gedit /etc/ld.so.conf.d/opencv.conf

修改文件

/usr/local/lib

保存退出&更新

sudo ldconfig

(2)

打开文件

sudo gedit /etc/profile

修改文件

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

保存更新

source /etc/profile

(3)

打开文件

/etc/ld.so.conf

修改文件

/usr/local/lib

保存更新

sudo ldconfig

6.验证

pkg-config --modversion opencv4

显示如下:

4.5.5

到这里opencv安装完毕
以下待更新:
darknet-master:yolo-v4
TensorRT:8.4.3.1
python:3.8
pytorch:1.12.1
MySql:8.24

你可能感兴趣的:(ffmpeg,opencv,yolov5,python,ubuntu,ffmpeg,pytorch,opencv)