An Ansible playbook to install OpenCV and Python3 on Ubuntu Linux
As a graduate student in Georgia Tech's awesome Online Master of Science in Computer Science (OMSCS) program, I recently completed CS 6476, Computer Vision. The required software for the course was Python 3.4+ with OpenCV 3.0+, to which I added the desire to work on Ubuntu 14.04 using Python virtual environments. I found this to be pretty tricky to set up, but was fortunate enough to discover Adrian Rosebrock's excellent blog post explaining how to do it.
I wrote this Ansible playbook in an effort to automate his steps in an easily repeatable way.
- An installation of Ubuntu 14.04 Desktop edition (Server may also work)
- Ansible 2.0 or higher. See the Ansible installation page for installation instructions. It may be as simple as
$ sudo pip install ansible
.
- Clone this repository to your Ubuntu machine.
- Verify the contents of the
variables
section of theplaybook.yml
file (discussed under Variables, below). - From the project root, run
$ ansible-playbook playbook.yml
.
This playbook contains all the steps from Adrian's blog post to:
- Install Python 3 virtualenv, virtualenvwrapper, and numpy, and create a virtual environment.
- Download OpenCV and its various dependencies.
- Compile and install OpenCV within the Python virtual environment.
Some of the steps, especially compiling OpenCV, take quite a bit of time. The whole playbook takes about 30 minutes to run on my 2-core Mac.
Please verify the variables in the playbook.yml
file have the values you want for your setup. They are:
download_dir
- this is the path to the directory where any downloaded files will be placed during installation. If the directory doesn't exist, it will be created before use, and removed when the installation is complete.opencv_version
- this is the version of OpenCV that will be installed.3.1.0
is the current version. This value can be a tag or a specific commit hash from the OpenCV github project.virtualenv_home
- this is the path to the directory where all of your virtual environments will be stored.virtualenv_name
- this is the name of the virtual environment that will be created for you, with OpenCV available in it.num_processors
- this is the number of processors to use when compiling OpenCV, up to the number of processors in your local machine. More processors reduces the time it takes to comple OpenCV.
Virtual environments are a Python best-practice technique for preventing dependency conflicts while developing multiple Python projects on the same machine. If you're unfamiliar with them, this guide provides good coverage of the rationale and basic usage.
Assuming the value of virtualenv_home
is cv
, just use
$ workon cv
to activate your virtual environment. OpenCV will be available to your Python code with import cv2
. Any other Python packages you install using pip
while your virtual environment is active will only be installed in your virtual environment, and not system-wide.
When you want to exit your virtual environment, just use
$ deactivate
See the virtualenvwrapper documentation for more instruction.
If you have Vagrant installed (or if you install it), you can optionally spin up a vagrant virtual machine and run this playbook inside it. Just run
$ vagrant up
and vagrant will create and provision the virtual machine with OpenCV installed.
I'm pretty new at Ansible. If there's a smarter way to do any of this, please send me a pull request with an explanation of your propsed change. If something doesn't work, please create a github issue against this project including specific steps to reproduce the problem.
Use of this software is governed by the included MIT license.