Title: Ubuntu 16.04: How to install OpenCV - PyImageSearch

URL Source: https://pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

Published Time: 2016-10-24T14:00:57+00:00

Markdown Content:
![Image 1: ubuntu1604_anpr](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20306'%3E%3C/svg%3E)

Over the past two years running the PyImageSearch blog, I’ve authored two tutorials detailing the required steps to install OpenCV (with Python bindings) on Ubuntu. You can find the two tutorials here:

*   [Install OpenCV 3.0 and Python 2.7+ on Ubuntu 14.04](https://pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/)
*   [Install OpenCV 3.0 and Python 3.4+ on Ubuntu 14.04](https://pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu/)

However, with _support of Ubuntu 14.04 winding down_ and **_Ubuntu 16.04 set as the next LTS_** (with support until April 2021), I thought it would be appropriate to create a **_new, updated Ubuntu + OpenCV install tutorial._**

Inside this tutorial, I will _document_, _demonstrate_, and _provide detailed steps_ to install OpenCV 3 on Ubuntu 16.04 with _either_ Python 2.7 or Python 3.5 bindings.

Furthermore, this document has been _fully updated_ from my previous Ubuntu 14.04 tutorials to use the latest, updated packages from the `apt-get` repository.

**To learn how to install OpenCV on your Ubuntu 16.04 system, keep reading.**

_**Note:** Don’t care about Python bindings and simply want OpenCV installed on your system (likely for C++ coding)? No worries, this tutorial will still work for you. Follow along with the instructions and perform the steps — by the end of this article you’ll have OpenCV installed on your system. From there, just ignore the Python bindings and proceed as usual._

Before we get into this tutorial, I want to mention that Ubuntu 16.04 actually ships out-of-the-box with _both_ Python 2.7 and Python 3.5 installed. The actual versions (as of 24 October 2016) are:

*   Python 2.7.12 (used by default when you type `python` in your terminal).
*   Python 3.5.2 (can be accessed via the `python3` command).

Again, it’s worth repeating that Python 2.7 is _still_ the default Python version used by Ubuntu. There are [plans to migrate to Python 3](https://wiki.ubuntu.com/Python) and use Python 3 by default; however, as far as I can tell, we are still a long way from that actually becoming a reality.

In either case, this tutorial will support _**both**_ Python 2.7 and Python 3. I’ve highlighted the steps that require you to make a _decision_ regarding which version of Python you would like to use. Make sure you are _consistent_ with your decision, otherwise you will inevitably run into compile, linking, and import errors.

Regarding _which_ Python version you should use…_I’m not getting into that argument_. I’ll simply say that you should use whichever version of Python you are comfortable with and use on a daily basis. Keep in mind that Python 3 is the future — but also keep in mind that porting Python 2.7 code to Python 3 isn’t terribly challenging either once you understand the differences between the Python versions. And as far as OpenCV goes, OpenCV 3 doesn’t care which version of Python you’re using: the bindings will work just the same.

All that said, let’s get started installing OpenCV with Python bindings on Ubuntu 16.04.

### Step #1: Install OpenCV dependencies on Ubuntu 16.04

Most (in fact, _all_) steps in this tutorial will be accomplished by using your terminal. To start, open up your command line and update the `apt-get` package manager to refresh and upgrade and pre-installed packages/libraries:

$ sudo apt-get update
$ sudo apt-get upgrade

Next, let’s install some developer tools:

$ sudo apt-get install build-essential cmake pkg-config

The `pkg-config` package is (very likely) already installed on your system, but be sure to include it in the above `apt-get` command just in case. The `cmake` program is used to automatically configure our OpenCV build.

OpenCV is an image processing and computer vision library. Therefore, OpenCV needs to be able to load various image file formats from disk such as JPEG, PNG, TIFF, etc. In order to load these images from disk, OpenCV actually calls other image I/O libraries that actually facilitate the loading and decoding process. We install the necessary ones below:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

Okay, so now we have libraries to load images from disk — _but what about video?_ Use the following commands to install packages used to process video streams and access frames from cameras:

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev

OpenCV ships out-of-the-box with a very limited set of GUI tools. These GUI tools allow us to display an image to our screen (`cv2.imshow` ), wait for/record keypresses (`cv2.waitKey` ), track mouse events (`cv2.setMouseCallback` ), and create simple GUI elements such as sliders and trackbars. Again, you shouldn’t expect to be building full-fledged GUI applications with OpenCV — these are just simple tools that allow you to debug your code and build very simple applications.

Internally, the name of the module that handles OpenCV GUI operations is `highgui` . The `highgui` module relies on the GTK library, which you should install using the following command:

$ sudo apt-get install libgtk-3-dev

Next, we install libraries that are used to optimize various functionalities inside OpenCV, such as matrix operations:

$ sudo apt-get install libatlas-base-dev gfortran

We’ll wrap up Step #1 by installing the Python development headers and libraries for both Python 2.7 and Python 3.5 (that way you have both):

$ sudo apt-get install python2.7-dev python3.5-dev

**_Note:_** _If you do not install the Python development headers and static library, you’ll run into issues during **Step #4** where we run `cmake` to configure our build. If these headers are not installed, then the `cmake` command will be unable to automatically determine the proper values of the Python interpreter and Python libraries. In short, the output of this section will look “empty” and you will not be able to build the Python bindings. When you get to **Step #4**, take the time to compare your output of the command to mine._

### Step #2: Download the OpenCV source

At the time of this article’s publication, the most recent version of OpenCV is `3.1.0` , which we download a `.zip` of and unarchive using the following commands:

$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
$ unzip opencv.zip

When new versions of OpenCV are released you can check the official [OpenCV GitHub](https://github.com/opencv/opencv) and downloaded the latest release by changing the version number of the `.zip` .

However, we’re not done downloading source code yet — we also need the [opencv\_contrib](https://github.com/opencv/opencv_contrib) repository as well:

$ wget -O opencv\_contrib.zip https://github.com/Itseez/opencv\_contrib/archive/3.1.0.zip
$ unzip opencv\_contrib.zip

Why are we bothering to download the contrib repo as well?

Well, we want the _full install_ of OpenCV 3 to [have access to features (no pun intended) such as SIFT and SURF](https://pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/). In OpenCV 2.4, SIFT and SURF were included in the _default_ installation of OpenCV. However, with the release of OpenCV 3+, these packages have been moved to contrib, which houses either (1) modules that are currently in development or (2) modules that are marked as “non-free” (i.e., patented). You can learn more about the reasoning behind the SIFT/SURF restructuring in [this blog post](https://pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/).

_**Note:** You might need to expand the commands above using the “<=>” button during your copy and paste. The `.zip` in the `3.1.0.zip` may be cutoff in smaller browser windows. For convenience, I have included the full URL of both the `opencv` archive as well as the `opencv_contrib` archive below:_

*   _[https://github.com/Itseez/opencv/archive/3.1.0.zip](https://github.com/Itseez/opencv/archive/3.1.0.zip)_
*   _[https://github.com/Itseez/opencv\_contrib/archive/3.1.0.zip](https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip)_

_I also want to mention that **both** your `opencv` and `opencv_contrib` versions should be the same (in this case, `3.1.0` ). If the versions numbers do not matchup, you could very easily run into compile time errors (or worse, runtime errors that are near impossible to debug)._

### Step #3: Setup your Python environment — Python 2.7 or Python 3

We are now ready to start configuring our Python development environment for the build. The first step is to install `pip` , a Python package manager:

$ cd ~
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

I’ve mentioned this in _every single OpenCV + Python install tutorial I’ve ever done_, but I’ll say it again here today: I’m a _huge fan_ of both [virtualenv](https://virtualenv.pypa.io/en/latest/) and [virtualenvwrapper](https://virtualenvwrapper.readthedocs.org/en/latest/). **These Python packages allow you to create _separate, independent_ Python environments for _each_ project that you are working on.**

In short, using these packages allows you to solve the _“Project X depends on version 1.x, but Project Y needs 4.x_ dilemma. A fantastic side effect of using Python virtual environments is that you can keep your system Python neat, tidy, and free from clutter.

While you can certainly install OpenCV with Python bindings without Python virtual environments, **_I highly recommend you use them_** as other PyImageSearch tutorials leverage Python virtual environments. I’ll also be assuming that you have both `virtualenv` and `virtualenvwrapper` installed throughout the remainder of this guide.

If you would like a full, detailed explanation on why Python virtual environments are a _best practice_, you should absolutely [give this excellent blog post on RealPython a read](https://realpython.com/blog/python/python-virtual-environments-a-primer/). I also provide some commentary on why I personally prefer Python virtual environments [in the first half of this tutorial](https://pyimagesearch.com/2016/05/02/accessing-rpi-gpio-and-gpio-zero-with-opencv-python/).

Again, let me reiterate that it’s _**standard practice**_ in the Python community to be leveraging virtual environments of some sort, so I suggest you do the same:

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/get-pip.py ~/.cache/pip

Once we have `virtualenv` and `virtualenvwrapper` installed, we need to update our `~/.bashrc` file to include the following lines at the _bottom_ of the file:

\# virtualenv and virtualenvwrapper
export WORKON\_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

The `~/.bashrc` file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called `WORKON_HOME` to point to the directory where our Python virtual environments live. We then load any necessary configurations from `virtualenvwrapper` .

To update your `~/.bashrc` file simply use a standard text editor. I would recommend using `nano` , `vim` , or `emacs` . You can also use graphical editors as well, but if you’re just getting started, `nano` is likely the easiest to operate.

A more simple solution is to use the `cat` command and avoid editors entirely:

$ echo -e "\\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
$ echo "export WORKON\_HOME=$HOME/.virtualenvs" >> ~/.bashrc
$ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

After editing our `~/.bashrc` file, we need to reload the changes:

$ source ~/.bashrc

_**Note:**_ _Calling `source` on `.bashrc` only has to be done **once** for our current shell session. Anytime we open up a new terminal, the contents of `.bashrc` will be **automatically** executed (including our updates)._

Now that we have installed `virtualenv` and `virtualenvwrapper` , the next step is to actually _create_ the Python virtual environment — we do this using the `mkvirtualenv` command.

But before executing this command, you need to make a choice: Do you want to use Python 2.7 or Python 3?

The outcome of your choice will determine which command you run in the following section.

#### Creating your Python virtual environment

If you decide to use Python 2.7, use the following command to create a Python 2.7 virtual environment:

$ mkvirtualenv cv -p python2

Otherwise, use this command to create a Python 3 virtual environment:

$ mkvirtualenv cv -p python3

Regardless of which Python command you decide to use, the end result is that we have created a Python virtual environment named `cv` (short for “computer vision”).

You can name this virtual environment whatever you like (and create as many Python virtual environments as you want), but for the time bing, I would suggest sticking with the `cv` name as that is what I’ll be using throughout the rest of this tutorial.

#### Verifying that you are in the “cv” virtual environment

If you ever reboot your Ubuntu system; log out and log back in; or open up a new terminal, you’ll need to use the `workon` command to re-access your `cv` virtual environment. An example of the `workon` command follows:

$ workon cv

To validate that you are in the `cv` virtual environment, simply examine your command line — _if you see the text `(cv)` preceding your prompt, then you **are**_ _in the `cv` virtual environment:_

![Image 2: Figure 1: Make sure you see the "(cv)" text on your prompt, indicating that you are in the cv virtual environment.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20363'%3E%3C/svg%3E)

**Figure 1:** Make sure you see the _“(cv)”_ text on your prompt, indicating that you _**are**_ in the _cv_ virtual environment.

Otherwise, if you _**do not**_ see the `cv` text, then you _**are not**_ in the `cv` virtual environment:

![Image 3: Figure 2: If you do not see the "(cv)" text on your prompt, then you are not in the cv virtual environment and need to run the "workon" command to resolve this issue. ](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20360'%3E%3C/svg%3E)

**Figure 2:** If you do not see the _“(cv)”_ text on your prompt, then you _**are not**_ in the _cv_ virtual environment and need to run the _“workon”_ command to resolve this issue.

To access the `cv` virtual environment simply use the `workon` command mentioned above.

#### Install NumPy into your Python virtual environment

The final step before we compile OpenCV is to install [NumPy](http://www.numpy.org/), a Python package used for numerical processing. To install NumPy, ensure you are in the `cv` virtual environment (otherwise NumPy will be installed into the _system_ version of Python rather than the `cv` environment). From there execute the following command:

$ pip install numpy

### Step #4: Configuring and compiling OpenCV on Ubuntu 16.04

At this point, all of our necessary prerequisites have been installed — we are now ready to compile and OpenCV!

But before we do that, double-check that you are in the `cv` virtual environment by examining your prompt (you should see the `(cv)` text preceding it), and if not, use the `workon` command:

$ workon cv

After ensuring you are in the `cv` virtual environment, we can setup and configure our build using CMake:

$ cd ~/opencv-3.1.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE\_BUILD\_TYPE=RELEASE \\
    -D CMAKE\_INSTALL\_PREFIX=/usr/local \\
    -D INSTALL\_PYTHON\_EXAMPLES=ON \\
    -D INSTALL\_C\_EXAMPLES=OFF \\
    -D OPENCV\_EXTRA\_MODULES\_PATH=~/opencv\_contrib-3.1.0/modules \\
    -D PYTHON\_EXECUTABLE=~/.virtualenvs/cv/bin/python \\
    -D BUILD\_EXAMPLES=ON ..

The above commands change directory to `~/opencv-3.1.0` , which if you have been following this tutorial is where you downloaded and unarchived the `.zip` files.

_**Note:** If you are getting an error related to `stdlib.h: No such file or directory` during either the `cmake` or `make` phase of this tutorial you’ll also need to include the following option to CMake: `-D ENABLE_PRECOMPILED_HEADERS=OFF` . In this case I would suggest deleting your `build` directory, re-creating it, and then re-running CMake with the above option included. This will resolve the `stdlib.h` error. Thank you to Carter Cherry and Marcin for pointing this out in the comments section!_

Inside this directory we create a sub-directory named `build` and change into it. The `build` directory is where the actual compile is going to take place.

Finally, we execute `cmake` to configure our build.

Before we move on to the actual compilation of OpenCV, _**make sure you examine the output of CMake!**_

To do this, scroll down the section titled `Python 2` and `Python 3` .

_**If you are compiling OpenCV on Ubuntu 16.04 with Python 2.7 support**_, make sure the `Python 2` section includes valid paths to the `Interpreter` , `Libraries` , `numpy` , and `packages path` . Your output should be similar to mine below:

![Image 4: Figure 3: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for Ubuntu 16.04.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20378'%3E%3C/svg%3E)

**Figure 3:** Ensuring that Python 2.7 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Examining this output, you can see that:

1.  The `Interpreter` points to the Python 2.7 binary in the `cv` virtual environment.
2.  `Libraries` points to the Python 2.7 library (which we installed during the final step of Step #1).
3.  The `numpy` value points to our NumPy installation in the `cv` virtual environment.
4.  And finally, the `packages path` points to `lib/python2.7/site-packages` . When combined with the `CMAKE_INSTALL_PREFIX` , this means that after compiling OpenCV, we’ll find our `cv2.so` bindings in `/usr/local/lib/python2.7/site-packages/` .

Similarly, _**if you’re compiling OpenCV 16.04 with Python 3 support**_, you’ll want to ensure your `Python 3` section looks similar to mine below:

![Image 5: Figure 4: Validating that Python 3 will be used when compiling OpenCV 3 for Ubuntu 16.04.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20376'%3E%3C/svg%3E)

**Figure 4:** Validating that Python 3 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Again, notice how my `Interpreter` , `Libraries` , `numpy` and `packages path` have all been correctly set.

If you **_do not_** see the `cv` virtual environments in these variable paths, **_it’s almost certainly because you are NOT in the `cv` virtual environment prior to running CMake!_**

If that is indeed the case, simply access the `cv` virtual environment by calling `workon cv` and re-run the CMake command mentioned above.

Assuming your CMake command exited without any errors, you can now compile OpenCV:

$ make -j4

The `-j` switch controls the number of processes to be used when compiling OpenCV — you’ll want to set this value to the number of processors/cores on your machine. In my case, I have a quad-core processor, so I set `-j4` .

Using multiple processes allows OpenCV to compile faster; however, there are times where race conditions are hit and the compile bombs out. While you can’t really tell if this is the case without _a lot_ of previous experience compiling OpenCV, if you do run into an error, my first suggestion would be to run `make clean` to flush the build, followed by compiling using only a single core:

$ make clean
$ make

Below you can find a screenshot of a successful OpenCV + Python compile on Ubuntu 16.04:

![Image 6: Figure 5: Successfully compiling OpenCV 3 for Ubuntu 16.04.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20379'%3E%3C/svg%3E)

**Figure 5:** Successfully compiling OpenCV 3 for Ubuntu 16.04.

The last step is to actually _install_ OpenCV 3 on Ubuntu 16.04:

$ sudo make install
$ sudo ldconfig

### Step #5: Finish your OpenCV install

You’re coming down the home stretch, just a few more steps to go and your Ubuntu 16.04 system will be all setup with OpenCV 3.

#### For Python 2.7:

After running `sudo make install` , your Python 2.7 bindings for OpenCV 3 should now be located in `/usr/local/lib/python-2.7/site-packages/` . You can verify this using the `ls` command:

$ ls -l /usr/local/lib/python2.7/site-packages/
total 1972
-rw-r--r-- 1 root staff 2016608 Sep 15 09:11 cv2.so

_**Note:** In some cases, you may find that OpenCV was installed in `/usr/local/lib/python2.7/dist-packages` rather than `/usr/local/lib/python2.7/site-packages` (note `dist-packages` versus `site-packages` ). If your `cv2.so` bindings are not in the `site-packages` directory, be sure to check `dist-pakages` ._

The final step is to [sym-link](https://en.wikipedia.org/wiki/Symbolic_link) our OpenCV `cv2.so` bindings into our `cv` virtual environment for Python 2.7:

$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

#### For Python 3.5:

After running `sudo make install` , your OpenCV + Python 3 bindings should be located in `/usr/local/lib/python3.5/site-packages/` . Again, you can verify this using the `ls` command:

$ ls -l /usr/local/lib/python3.5/site-packages/
total 1972
-rw-r--r-- 1 root staff 2016816 Sep 13 17:24 cv2.cpython-35m-x86\_64-linux-gnu.so

I’ve been puzzled regarding this behavior ever since OpenCV 3 was released, but for some reason, when compiling OpenCV with Python 3 support, the output `cv2.so` filename is different. The actual filename might vary for you, but it should look something similar to `cv2.cpython-35m-x86_64-linux-gnu.so` .

Again, I have no idea exactly _why_ this happens, but it’s a very easy fix. All we need to do is rename the file:

$ cd /usr/local/lib/python3.5/site-packages/
$ sudo mv cv2.cpython-35m-x86\_64-linux-gnu.so cv2.so

After renaming `cv2.cpython-35m-x86_64-linux-gnu.so` to simply `cv2.so` , we can sym-link our OpenCV bindings into the `cv` virtual environment for Python 3.5:

$ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

### Step #6: Testing your OpenCV install

**Congratulations, you now have OpenCV 3 installed on your Ubuntu 16.04 system!**

To verify that your installation is working:

1.  Open up a new terminal.
2.  Execute the `workon` command to access the `cv` Python virtual environment.
3.  Attempt to import the Python + OpenCV bindings.

I have demonstrated how to perform these steps below:

$ cd ~
$ workon cv
$ python
Python 3.5.2 (default, Jul  5 2016, 12:43:10) 
\[GCC 5.4.0 20160609\] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.\_\_version\_\_
'3.1.0'
>>>

As you can see, I can import my OpenCV bindings into my Python 3.5 shell.

Below follows a screenshot of me utilizing the same steps outlined in this tutorial and importing OpenCV bindings into a Python 2.7 shell:

![Image 7: Figure 6: Ensuring that I can successfully import my Python + OpenCV bindings on Ubuntu 16.04.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20380'%3E%3C/svg%3E)

**Figure 6:** Ensuring that I can successfully import my Python + OpenCV bindings on Ubuntu 16.04.

Thus, regardless of which Python version you decide to use, simply follow the steps detailed in this tutorial and you’ll be able to install OpenCV + Python on your Ubuntu 16.04 system.

Once OpenCV has been installed, you can delete both the `opencv-3.1.0` and `opencv_contrib-3.1.0` directories (along with their associated `.zip` files):

$ cd ~
$ rm -rf opencv-3.1.0 opencv\_contrib-3.1.0 opencv.zip opencv\_contrib.zip

But again, _be careful when running this command!_ You’ll want to make sure you have properly installed OpenCV on your system prior to blowing along these directories. Otherwise, you’ll need to restart the entire compile process!

Troubleshooting and FAQ
-----------------------

In this section, I address some of the common questions, problems, and issues that arise when installing OpenCV on Ubuntu 16.04.

_**Q.**_ When I execute `mkvirtualenv`  or `workon` , I get a “command not found error”.

_**A.**_ There are three primary reasons why you would be getting this error message, all of which are related to **Step #3:**

1.  First, make sure you have installed `virtualenv`  and `virtualenvwrapper`  using the `pip`  package manager. You can verify this by running `pip freeze` , examining the output, and ensuring that you see both `virtualenv`  and `virtualenvwrapper`  in the list of installed packages.
2.  Your `~/.bashrc`  file may not be updated correctly. To diagnose this, use a text editor such as `nano`  and view the contents of your `~/.bashrc`  file. At the bottom of the file, you should see the proper `export`  and `source`  commands are present (again, check **Step #3** for the commands that should be appended to `~/.bashrc` ).
3.  After editing your `~/.bashrc`  file, you may have forgotten to `source`  it and reload its contents. Make sure you run source `~/.bashrc`  after editing it to ensure the contents are reloaded — this will give you access to the `mkvirtualenv`  and `workon`  commands.

_**Q.**_ Whenever I open a new terminal, logout, or reboot my Ubuntu system, I cannot execute the `mkvirtualenv`  or `workon`  commands.

_**A.**_ See **reason #2** from the previous question.

_**Q.**_ When I (1) open up a Python shell that imports OpenCV or (2) execute a Python script that calls OpenCV, I get an error: `Import Error: No module named cv2` .

_**A.**_ Unfortunately, the exact cause of this error message is extremely hard to diagnose as there are _multiple_ reasons this could be happening. In general, I recommend the following suggestions to help diagnose and resolve the error:

1.  Make sure you are in the `cv`  virtual environment by using the `workon cv`  command. If this command gives you an error, then see the first question in this FAQ.
2.  If after you’ve ensured your `~/.bashrc`  file has been updated properly and `source` ‘d, then try investigating the contents of the `site-packages`  directory in your `cv`  virtual environment. You can find the `site-packages`  directory in `~/.virtualenvs/cv/lib/python2.7/site-packages/`  or `~/.virtualenvs/cv/lib/python3.5/site-packages/`  depending on your Python version. Make sure that (1) there is a `cv2.so`  file in this `site-packages`  directory and (2) that it’s properly sym-linked to a valid, existing file.
3.  Be sure to check the `site-packages`  (and even `dist-packages` ) directory for the system install of Python located in `/usr/local/lib/python2.7/site-packages/`  and `/usr/local/lib/python3.5/site-packages/` , respectively. Ideally, you should have a `cv2.so`  file there.
4.  If all else fails, check in your `build/lib`  directory of your OpenCV build. There _should_ be a `cv2.so`  file there (provided that both `cmake`  and `make`  executed without error). If the `cv2.so`  file is present, _manually copy it_ into both the system `site-packages`  directory as well as the `site-packages`  directory for the `cv`  virtual environment.

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In today’s blog post, I demonstrated how to install _**OpenCV 3**_ with either Python 2.7 or Python 3 bindings on your _**Ubuntu 16.04 system**_.

For more OpenCV install tutorials on other operating systems (such as OSX, Raspbian, etc.), [please refer to this page](https://pyimagesearch.com/opencv-tutorials-resources-guides/) where I provide additional links and resources.

_But before you go…_
--------------------

If you’re interested in learning more about OpenCV, computer vision, and image processing, _**be sure to enter your email address in the form below to be notified when new blog posts are published!**_

![Image 8](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.