How to install OpenCV, Nvidia CUDA, Tensorflow in Ubuntu 20.04 on Windows 10.

By Jason Tzu-Cheng Chuang 2021-07-07

Purpose: Easily setting up OpenCV environment for Deep Neural Network

This demonstration has been tested on Windows Subsystem for Linux, WSL2 Ubuntu 20.04 LTS on Windows 10 x64 (Version:20H2, OS module 19042.1052)

Check WSL2 in Windows PowerShell

      
PS C:\Users\ai4quant> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Stopped         2

    

Requirement

Python 3.8
OpenCV 4.2.0
Tensorflow 2.5.0
Nvidia Cuda Toolkit 10.1.243

Installation

      
sudo su -
root@jason_chuang:~# apt-get update
root@jason_chuang:~# apt-get install -y python3-pip
root@jason_chuang:~# pip3 install --upgrade pip
root@jason_chuang:~# sudo apt-get install python3-numpy
root@jason_chuang:~# sudo apt-get install python3.8-dev
root@jason_chuang:~# python3.8-config --includes
#The output would be
-I/usr/include/python3.8 -I/usr/include/python3.8
root@jason_chuang:~# sudo apt-get install python3-opencv

    

Checking

      
root@jason_chuang:~/# python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> print(cv.__version__)
4.2.0

    

(Optional)Install tqdm for model evaluation

      
root@jason_chuang:~# pip3 install tqdm

    

Install numpy matplotlib pandas

      
root@jason_chuang:~# pip3 install numpy
root@jason_chuang:~# pip3 install matplotlib
root@jason_chuang:~# pip3 install pandas

    

Install tensorflow (CPU version)

      
root@jason_chuang:~# sudo apt install nvidia-cuda-toolkit
root@jason_chuang:~# pip3 install tensorflow

    

Change back to normal user Find shared object and add to PATH

      
jasontgi@jason_chuang:~$ find /usr/lib/x86_64-linux-gnu/ -name 'libcudart.so*'
/usr/lib/x86_64-linux-gnu/libcudart.so.10.1
/usr/lib/x86_64-linux-gnu/libcudart.so.10.1.243
/usr/lib/x86_64-linux-gnu/libcudart.so

    

Add the folder to path, so that python finds it, by modifyint ~/.bashrc

      
export PATH=/usr/lib/x86_64-linux-gnu:$PATH
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH

    

Modify permissions

      
sudo chmod a+r /usr/lib/x86_64-linux-gnu/libcuda*

    

Checking

      
jasontgi@jasonchuang:~$ python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-07-07 20:47:52.972333: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/lib/x86_64-linux-gnu::/usr/lib/x86_64-linux-gnu/:/usr/lib/x86_64-linux-gnu:
2021-07-07 20:47:52.972412: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
>>> print(tf.__version__)
2.5.0