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



Python 3.8
OpenCV 4.2.0
Tensorflow 2.5.0
Nvidia Cuda Toolkit 10.1.243


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



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__)


(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 '*'


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*



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/] Could not load dynamic library ''; dlerror: 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/] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
>>> print(tf.__version__)