Install OpenCV inside virtualenv with Ubuntu 16.04

It is a best practice to have a separate sandbox python from the system python. The guide below probably is the simplest way to install opencv inside virtualenv. The drawback though is that it won’t install the latest opencv. But if you just need something quick to get things running, the following probably is sufficient.
  • Setup virtualenv
sudo apt install python-pip
sudo pip install virtualenv
virtualenv -p python3 ~/venv3  # create virtualenv named venv3
  • Install OpenCV and OpenCV-contrib
source ~/venv3/bin/activate # enter virtualenv venv3
pip install pip -U # update pip
pip install opencv-python opencv-contrib-python

Above we assumed the Python3 environment for the sandbox. If you prefer Python2 instead, just change

 virtualenv -p python3 ~/venv3

to

virtualenv -p python2 ~/venv2

and use

source ~/venv2/bin/activate

to enter the virtual environment instead.

Leave a Reply

Your email address will not be published. Required fields are marked *