Install FFmpeg with Nvidia nvdec and nvenc on Ubuntu 20.04 with CUDA

Install FFmpeg with Nvidia nvdec and nvenc on Ubuntu 20.04 with CUDA

Install FFmpeg with Nvidia nvdec and nvenc on Ubuntu 20.04 with CUDA

FFmpeg is one of the most popular open-source multimedia manipulation tools with a library of plugins that can be applied to various parts of the audio and video processing pipelines and have achieved wide adoption across the world.

Install CUDA

First make sure you have nvidia VGA driver installed: https://linuxconfig.org/how-to-install-the-nvidia-drivers-on-ubuntu-20-04-focal-fossa-linux

  • Enable CUDA repository
wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
$ sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
  • Install CUDA:
sudo apt-get --purge remove "*cublas*" "*cufft*" "*curand*" \
 "*cusolver*" "*cusparse*" "*npp*" "*nvjpeg*" "cuda*" "nsight*" 
sudo apt-get --purge remove *nvidia*
sudo apt-get autoremove

After this step you could try installing CUDA like so:

sudo apt install cuda

but I receive error:

The following packages have unmet dependencies:
 cuda : Depends: cuda-11-1 (>= 11.1.1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

My first instinct is that my driver version is somehow not compatible with the CUDA. My driver version is 455.38, while CUDA 11.1.1 references 455.32.

So instead I followed the instructions on Nvidia CUDA site: https://developer.nvidia.com/cuda-download

wget https://developer.download.nvidia.com/compute/cuda/11.1.1/local_installers/cuda_11.1.1_455.32.00_linux.run
sudo sh cuda_11.1.1_455.32.00_linux.run

So I decided to remove existing Nvidia drivers and let CUDA package install its own (also switch back to default video driver):

sudo dpkg -P $(dpkg -l | grep nvidia-driver | awk '{print $2}')
sudo apt autoremove

Now restart your computer.

Now you can run the CUDA installation again:

sudo sh cuda_11.1.1_455.32.00_linux.run

But still no fucking luck.

So I revert to previous method installing it from CUDA repository:

sudo apt install cuda

After this is finished make yourself Windows and reboot again otherwise on nvidia-smi you'll get an error:

Failed to initialize NVML: Driver/library version mismatch

In the process you'll likely have to setup password for enrolling to MOK. Depending on your BIOS setup.

Not add CUDA to PATH and LD_LIBRARY_PATH to end of your ~/.bashrc file:

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

Apply changes to environment variables:

source ~/.bashrc

Install FFmpeg with nvdec, nvenc

FFmpeg with NVIDIA GPU acceleration is supported on all Linux platforms.

To compile FFmpeg on Linux, do the following:

  • Clone ffnvcodec
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
  • Install ffnvcodec
cd nv-codec-headers && sudo make install
  • Clone FFmpeg's public GIT repository.
cd .. 
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
  • Install necessary packages.
sudo sudo apt-get -y install build-essential pkg-config checkinstall git libfaac-dev libgpac-dev ladspa-sdk-dev libunistring-dev libbz2-dev \
  libjack-jackd2-dev libmp3lame-dev libsdl2-dev libopencore-amrnb-dev libopencore-amrwb-dev libvpx-dev libx264-dev libx265-dev libxvidcore-dev libopenal-dev libopus-dev \
  libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev \
  libxfixes-dev texi2html yasm zlib1g-dev
  • Configure (here you can add some other codecs too):
./configure --pkg-config-flags="--static" --enable-nonfree --enable-gpl --enable-version3 \
--enable-libmp3lame --enable-libvpx --enable-libopus \
--enable-opencl --enable-libxcb \
--enable-opengl --enable-nvenc --enable-vaapi \
--enable-vdpau --enable-ffplay --enable-ffprobe \
--enable-libxvid \
--enable-libx264 --enable-libx265 --enable-openal \
--enable-openssl --enable-cuda-nvcc --enable-cuvid --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64

Here I got error:

ERROR: failed checking for nvcc.

Checking the logs under ffbuild/config.log discovers this error: Cuda.o Error 255 / unsupported gpu architecture 'compute_30' during installation: https://github.com/lvaccaro/truecrack/issues/32. It's said you need to support architecture by your nvidia card.

So I changed the number 30 to 35. This is configure file under ffmpeg:

if enabled cuda_nvcc; then
    nvcc_default="nvcc"
    nvccflags_default="-gencode arch=compute_35,code=sm_35 -O2"
else
    nvcc_default="clang"
    nvccflags_default="--cuda-gpu-arch=sm_35 -O2"
    NVCC_C=""
fi

Now I cloud finally run ffmpeg configuration. After configuration is done run the build process:

make -j 8

Then install:

sudo make install