How to install OpenCV 4 on Ubuntu

In this tutorial you will learn how to install OpenCV 4 on your Ubuntu system.

OpenCV 4 has not been officially released yet; however, a release is expected in Autumn 2018.

Update: OpenCV 4.0.0-alpha is released as of September 20th, 2018.

So, why bother installing OpenCV 4?

You may want to consider installing OpenCV 4 for further optimizations, C++11 support, more compact modules, and many improvements to the Deep Neural Network (DNN) module.

Don’t feel the need to upgrade immediately — but do keep these instructions in mind if and when you’re ready to make the switch to OpenCV 4.

Looking for the source code to this post?
Jump right to the downloads section.

How to install OpenCV 4 on Ubuntu

In this blog post, I’ll walk you through the six steps to install OpenCV 4 on your Ubuntu system.

I’ll also cover some Frequently Asked Questions (FAQs) that will help you troubleshoot in the event you run into an error message. I strongly urge you to read the FAQ prior to submitting a comment on this post.

Before we begin, you probably have two burning questions:

1. Which version of Ubuntu OS should I use with OpenCV 4?

Today I’ll be installing OpenCV 4 on Ubuntu 18.o4. I’ve also tested these instructions with Ubuntu 16.04 as well.

If you intend on using this machine for deep learning I might suggest you use Ubuntu 16.04 as the GPU drivers are more mature. If you intend on using this machine just for OpenCV and other computer vision tasks, Ubuntu 18.04 is perfectly fine.

2. Should I use Python 2.7 or Python 3 with OpenCV 4?

Python 3 has become the standard and I highly recommend you install Python 3 with OpenCV 4.

That being said, if you’re making the conscious choice to use Python 2.7, you can of course follow these instructions, just be sure you take care to install Python 2.7 development headers and libraries as well as specifying Python 2.7 when creating your virtual environment. Everything else will be the same. Refer to the FAQs down below if you need pointers for Python 2.7.

Let’s begin.

Step #1: Install OpenCV 4 dependencies on Ubuntu

I’ll be using Ubuntu 18.04 to install OpenCV 4 with Python 3 bindings on my machine.

To get the OpenCV 4 install party started, fire up your Ubuntu machine and open a terminal. Alternatively, you may SSH into the box for the install portion.

From there, let’s update our system:

And then install developer tools:

Next, let’s install a handful of image and video I/O libraries.

These libraries enable us to load image from disk as well as read video files.

From there, let’s install GTK for our GUI backend:

Followed by installing two packages which contain mathematical optimizations for OpenCV:

And finally, let’s install the Python 3 development headers:

Once you have all of these prerequisites installed you can move on to the next step.

Step #2: Download OpenCV 4

Our next step is to download OpenCV.

Let’s navigate to our home folder and download both opencv and opencv_contrib. The contrib repo contains extra modules and functions which we frequently use here on the PyImageSearch blog. You should be installing the OpenCV library with the additional contrib modules as well.

When you’re ready, just follow along to download both the opencv  and opencv_contrib  code:

Update 2018-09-25: OpenCV 4.0.0-alpha is released and I have removed the git clone  commands and replaced them with relevant wget  commands to download the source.

From there, let’s unzip the archives:

I also like to rename the directories:

If you skip renaming the directories, don’t forget to update the CMake paths.

Now that opencv  and opencv_contrib  are downloaded and ready to go, let’s set up our environment.

Step #3: Configure your Python 3 virtual environment for OpenCV 4

Let’s install pip, a Python Package Manager.

To install pip, simply enter the following in your terminal:

Make use of virtual environments for Python development

Python virtual environments allow you to work on your Python projects in isolation. They are a best practice for Python development.

For example, maybe you have a Python + OpenCV project that requires an older version of scikit-learn (v0.14), but you want to keep using the latest version of scikit-learn (0.19) for all of your newer projects.

Using virtual environments, you could handle these two software version dependencies separately, something that is not possible using just the system install of Python.

If you would like more information about Python virtual environments take a look at this article on RealPython or read the first half of the this blog post on PyImageSearch.

Note: My preferred way to work with Python virtual environment is via the  virtualenv  and virtualenvwrapper  packages; however if you are more familiar with conda or PyEnv, feel free to use them and skip this part.

Let’s go ahead and install   virtualenv  and virtualenvwrapper  — these packages allow us to create and manage Python virtual environments:

To finish the install of these tools, we need to update our  ~/.bashrc  file.

Using a terminal text editor such as vi / vim  or nano , add the following lines to your ~/.bashrc :

Alternatively, you can append the lines directly via bash commands:

Next, source the ~/.bashrc  file:

Create a virtual environment to hold OpenCV 4 and additional packages

Now we’re at the point where we can create your OpenCV 4 + Python 3 virtual environment:

This command simply creates a Python 3 virtual environment named cv .

You can (and should) name your environment(s) whatever you’d like — I like to keep them short and sweet while also providing enough information so I’ll remember what they are for. For example, I like to name my environments like this:

  • py3cv4
  • py3cv3
  • py2cv2
  • etc.

Here my py3cv4  virtual environment can be used for Python 3 + OpenCV 4. My py3cv3  virtual environment is used for Python 3 and OpenCV 3. And my py2cv2  environment can be used to test legacy Python 2.7 + OpenCV 2.4 code. These virtual environment names are easy to remember and allow me to switch between OpenCV + Python versions (nearly) seamlessly.

Let’s verify that we’re in the cv  environment by using the workon  command:

Figure 1: We use the workon command to activate our cv virtual environment on Ubuntu prior to installing NumPy and OpenCV 4.

Install NumPy

The first package and only Python prerequisite we’ll install is NumPy:

We can now prepare OpenCV 4 for compilation on our Ubuntu machine.

Step #4: CMake and compile OpenCV 4 for Ubuntu

For this step, we’re going to setup our compile with CMake followed by running make  to actually compile OpenCV. This is the most time-consuming step of today’s blog post.

Navigate back to your OpenCV repo and create + enter a build  directory:

Run CMake for OpenCV 4

Now let’s run CMake to configure the OpenCV 4 build:

Be sure to update the above command to use the correct OPENCV_EXTRA_MODULES_PATH  and  PYTHON_EXECUTABLE  in your virtual environment that you’re working in. If you’re using the same directory structure Python virtual environment names these paths should not need to be updated.

Once CMake finishes, it’s important that you inspect the output. Your output should look similar to mine below:

Figure 2: It is critical to inspect your CMake output when installing OpenCV 4 on Ubuntu prior to kicking off the compile process.

Take a second now to ensure that the Interpreter  points to the correct Python 3 binary. Also check that numpy  points to our NumPy package which is installed inside the virtual environment.

Compile OpenCV 4

Now we’re ready to compile OpenCV 4:

Note: In the make  command above, the -j4  argument specifies that I have 4 cores for compilation. Most systems will have 2, 4, or 8 cores. You should update the command to use the number of cores on your processor for a faster compile. If you encounter compilation failures, you could try compiling with 1 core to eliminate race conditions by skipping the optional argument altogether.

Here you can see OpenCV 4 has compiled without any errors:

Figure 3: When compiling OpenCV 4 for Ubuntu, once the make output reaches 100% we can move on.

And from there, let’s install OpenCV 4 with two additional commands:

Step #5: Link OpenCV 4 into your Python 3 virtual environment

Before we make a symbolic link to link OpenCV 4 into our Python virtual environment, let’s determine our Python version:

Using the Python version, we can easily navigate to the correct site-packages  directory next (although I do recommend tab-completion in the terminal).

Let’s create a symbolic link from the OpenCV install directory to our virtual environment:

Pro tip: Use tab completion to ensure that your sym-link points (correctly) to your OpenCV bindings.

I cannot stress this step enough — it is very important to correctly create your sym-link. If you don’t correctly create a symbolic link, you won’t be able to import OpenCV in your Python scripts.

Step #6: Test your OpenCV 4 install on Ubuntu

Let’s do a quick sanity test to see if OpenCV is ready to go.

Open a terminal and perform the following:

The first command activates our virtual environment. Then we run the Python interpreter associated with the environment.

Note: It is not necessary to specify python3  as Python 3 is the only Python executable in the environment.

If you see that you have version 4.0.0 installed, then a “Congratulations!” is in order. Have a swig of your favorite beer or bourbon and let’s get to something more fun than installing libraries and packages.

Let’s perform object tracking in video with OpenCV 4

I know you’re itching to test out your installation with a practical example. Tracking motion in video is a fun, small project to get you working with OpenCV 4 on your Ubuntu machine.

To get started, scroll to the “Downloads” section of this blog post to download the source code and example video.

From there, install the imutils  library in your fresh virtual environment:

And then navigate to where you stored the zip file and unzip it. For example, you might save it in ~/Downloads  and then perform the following steps:

Now we’re ready to start our OpenCV + Python script.

You can execute the script using the following command:

Ready to try with your own ball and your webcam? Here’s the command:

At this point, you should see yourself.  Bring the ball into the frame and move it around to see the red tracking trail!

To find out how this object tracking example works, be sure to refer to this blog post.

Troubleshooting and FAQ

Did you encounter an error installing OpenCV 4 on Ubuntu?

Don’t jump ship yet. The first time you install OpenCV, it can be very frustrating and the last thing I want for you to do is to end the learning process here.

I’ve put together a short list of frequently asked questions (FAQs) and I suggest that you familiarize yourself with them.

Q. Can I use Python 2.7?

A. Python 3 is what I suggest for development these days, but I do understand the need for Python 2.7 to work with legacy code.

Starting with Ubuntu 18.04, Python 2.7 isn’t even included. If you’re on Ubuntu 18.04 (or greater) you can still install it manually at the end of  Step #1:

From there, when you create your virtual environment in Step #3, first install pip for Python 2.7:

And then (also in Step #3) when you create your virtual environment, simply use the relevant Python version flag:

From there everything should be the same.

Q. Why can’t I just pip to install OpenCV 4?

A. There are a number of pip-installable versions of OpenCV available depending on your operating system and architecture. The problem you may run into is that they may be compiled without various optimizations, and image/video I/O support. Use them — but use them at your own risk. This tutorial is meant to give you the full install of OpenCV 4 on Ubuntu while giving you complete control over the compile.

Q. Why can’t I just apt-get install OpenCV?

A. I’d avoid this “solution” at all costs even though it might work. You’ll end up with an older, outdated version of OpenCV on your system. Secondly, apt-get doesn’t play nice with virtual environments and you won’t have control over your compile and build.

Q. When I execute mkvirtualenv  or workon , I encounter a “command not found error”. What should I do?

A. There a number of reasons why you would be seeing this error message, all of come from to Step #3:

  1. First, ensure you have installed virtualenv  and virtualenvwrapper  properly using the pip  package manager. Verify by running pip freeze , and ensure that you see both virtualenv  and virtualenvwrapper  in the list of installed packages.
  2. Your ~/.bashrc  file may have mistakes. View the contents of your ~/.bashrc  file to see the proper export  and source  commands are present (check Step #3 for the commands that should be appended to ~/.bashrc ).
  3. You might have forgotten to source  your ~/.bashrc . Make sure you run  source ~/.bashrc  after editing it to ensure you have access to the mkvirtualenv  and workon  commands.

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

A. Refer to #2 from the previous question.

Q. When I try to import OpenCV, I encounter this message:  Import Error: No module named cv2 .

A. There are several reasons this could be happening and unfortunately, it is hard to diagnose. I recommend the following suggestions to help diagnose and resolve the error:

  1. Ensure your  cv  virtual environment is active by using the workon cv  command. If this command gives you an error, then verify that virtualenv  and virtualenvwrapper  are properly installed.
  2. 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/python3.5/site-packages/  depending on your Python version. Make sure (1) there is a cv2.so  file in the  site-packages  directory and (2) it’s properly sym-linked to a valid 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/python3.5/site-packages/ , respectively. Ideally, you should have a cv2.so  file there.
  4. As a last resort, check in your build/lib  directory of your OpenCV build. There should be a cv2.so  file there (if both cmake  and make  executed without error). If the cv2.so  file is not present, manually copy it into both the system site-packages  directory as well as the site-packages  directory for the cv  virtual environment.

Summary

Today we learned how to install OpenCV 4 from source on Ubuntu.

Compiling from source allows you to have full control over the install process, including adding any additional optimizations that you may wish to use.

We then tested the installation with a simple ball tracking demo.

Do you run macOS? I’ll be back on Friday with a macOS + OpenCV 4 installation guide so stay tuned!

If you enjoyed today’s installation tutorial and ball tracking demo, be sure to fill out the form below so that you receive updates when new blog posts go live!

Downloads:

If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , , ,

76 Responses to How to install OpenCV 4 on Ubuntu

  1. Harrison August 15, 2018 at 10:51 am #

    Adrian,

    Nice post! I found a small typo (I think), “Don’t feed the need to upgrade immediately”, should be, “Don’t ‘feel’ the need to upgrade immediately”. Also, have you found any specific improvements to the DNN aspects of Opencv in this version? For instance, has the support for Tensorflow models improved at all?

    • Adrian Rosebrock August 15, 2018 at 10:56 am #

      Thanks Harrison! Typo fixed. To address your question:

      The DNN module was introduced in OpenCV 3.3. The 3.4 release included a number of optimizations, making it faster, easier to use, and additional functionality. The OpenCV 4 release is even faster and more functionality, including the makings of actual “backends” to OpenCV for faster processing. I can’t comment directly on the TensorFlow models with OpenCV though as I haven’t tried them.

  2. ateet August 15, 2018 at 9:28 pm #

    Getting VIDIOC_QBUF: Invalid argument error when i run python ball_tracking.py. Help?

    • Adrian Rosebrock August 16, 2018 at 5:29 am #

      Can you add some debugging print statements or use Python’s “pdb” to nail down exactly what line is throwing that error? Additionally, what version of Ubuntu and Python are you using?

  3. Drail August 17, 2018 at 11:05 am #

    Thank you so much adrian,Did these instructions could be apply to the raspberry pi 3?

    • Adrian Rosebrock August 17, 2018 at 11:07 am #

      Since Raspbian is Debian-based you can use the vast majority of the instructions for the Raspberry Pi. I’ll be posting a tutorial dedicated exclusively to the Raspberry Pi next week though! 🙂

  4. Drail August 17, 2018 at 11:13 am #

    Wowww its awesome,
    Does this version of the opencv have a speed up of video processing in real-time than previous versions ??
    I’m reasearching real-time facial recognition in a raspberry pi.

    • Adrian Rosebrock August 17, 2018 at 12:44 pm #

      I haven’t had a chance to fully benchmark speedups between the versions. I’m going to hold off on that until the official OpenCV 4 release. If you’re interested in face recognition on the Pi, you should read this post.

  5. wally kulecz August 17, 2018 at 11:31 am #

    Looking forward to the compile instructions on the Pi where any and all performance improvements would be most welcome!

    I had good results with openCV 3.4.2:

    pip install opencv-contrib-python

    on Windows 7 (after a lot of hassles getting the “right” version of some DLLs). On Windows 10 it “just worked”. The dnn and highgui seem to be working perfectly for me so far. I’m not a Windows guy, but thought I’d pass this along as some Windows only friends asked about running the AI code I’d made with the help of your great tutorials. So far seems to be running fine.

    It’d be great if versions for the Pi could be made available through PyPi.org as it takes many hours to compile on the Pi3.

    • Adrian Rosebrock August 17, 2018 at 12:45 pm #

      Hey Wally — there actually is a Pi version available. I’ll be discussing how to use it and access it in either next week or the week after’s post. I’ll also be discussing some of the prons/cons of using the pip-based install, along what other gotchas to look out for.

  6. Pranav August 18, 2018 at 2:35 am #

    Hi Adrian and all,
    I am using python 3.6.5 and am having trouble with the following line.
    ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
    The import is failing with the following error.
    ImportError: dynamic module does not define module export function (PyInit_cv4)
    >
    Is there an issue with python 3.6.5 or with my simlink?

  7. Pranav Lal August 18, 2018 at 2:57 am #

    Hi Adrian and all,
    I found the problem. My simlink line should be
    ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

    • Adrian Rosebrock August 19, 2018 at 8:24 am #

      Nice job resolving the issue, Pranav!

  8. Steve Fischer August 18, 2018 at 8:37 am #

    This was the best guide on how to build OpenCV. Lots of folks post scripts on Github without context or explanation on why certain cmake params are needed, or a full explanation of prerequisites. This worked exactly as described on my Ubuntu 18.04 build–thanks!

    • Adrian Rosebrock August 19, 2018 at 8:23 am #

      Thank you for the kind words Steve, I really appreciate that! And congrats on getting OpenCV 4 installed.

  9. vijay singh August 18, 2018 at 2:39 pm #

    Nice post Adrian, what are major changes you noticed with this version.

    • Adrian Rosebrock August 19, 2018 at 8:23 am #

      I’ve noticed that the library can run faster and in particular the DNN module is becoming more easy to use and supports more models. C++11 is now required as well.

  10. Ebrahim August 18, 2018 at 3:51 pm #

    Hey Adrian, thanks for the amazing work you do for us.

    I’m trying to install OpenCV on my WSL ubuntu 18.04, but facing many issues. Here are the issues in detail:

    https://github.com/Microsoft/WSL/issues/3480

    Any help from your side will be appreciated! Thank u in advance.

    • Adrian Rosebrock August 19, 2018 at 8:22 am #

      Sorry Ebrahim, I don’t have any experience with WSL so I’m not sure what the problem may be. Could you use VirtualBox or VMware to virtualize Ubuntu instead? That would likely solve the problem.

  11. Ivo August 19, 2018 at 1:52 am #

    Great tutorial – followed it step by step and it just worked – thanks a lot! And I’m getting really fond of using virtualenv’s. Looking forward to some examples that make the difference between cv3 and 4.

    • Adrian Rosebrock August 19, 2018 at 8:21 am #

      Congrats on getting OpenCV 4 installed, Ivo! Nice job! 😀

  12. Mark Car August 19, 2018 at 2:40 am #

    I build it all correctly as shown and it looked like it worked yet when I try to import cv it does not find it. I did it on a virtual machine

    • Adrian Rosebrock August 19, 2018 at 8:21 am #

      Hey Mark — be sure to refer to the Troubleshooting and FAQ section of this post where I describe reasons why the import may fail.

  13. Jagnoor August 20, 2018 at 1:30 pm #

    Hi Adrian!

    I want to use install opencv 4 in my anaconda environment. What are the specific steps do I need to take care of while installing?

    • Adrian Rosebrock August 22, 2018 at 9:51 am #

      Sorry, I do not have any tutorials related to Anaconda and OpenCV 4 nor do I have Anaconda installed on my system so I can’t provide any suggestions there. I might publish one in the future but I currently don’t have any plans to. In the meantime I would kindly encourage you to follow my tutorial exactly if you would like OpenCV 4 installed on your system.

  14. Simonl Hyde August 21, 2018 at 1:18 am #

    I have the cv2.so in my virtual enviroment symbolically linked to this file cv2.cpython-35m-x86_64-linux-gnu.so in the build folder “cv2.so -> /usr/local/lib/python3.6/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so” and I have cv2.so in my /usr/local/lib/python3.6/site-packages/ but python still says no to import cv2.

    I found a typo in the FAQ on problem solving in the last question section 4 last hint ” 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.”

    • Adrian Rosebrock August 22, 2018 at 9:43 am #

      Thanks for reporting the typo. Additionally, are you in the “cv” Python virtual environment before trying to import the cv2 bindings? That would be the most likely reason.

    • ChrisO November 5, 2018 at 8:37 pm #

      I’m having this problem, please elaborate on what you did exactly! Thank you

  15. Laszlo August 21, 2018 at 5:13 am #

    Hey, I have a windows machine and I know you don’t recommend using windows so can you tell me what should I do? I don’t really want to use virtualbox.

    Thanks

    • Adrian Rosebrock August 22, 2018 at 9:35 am #

      If you don’t want to use a VM you could either:

      1. Dual boot Ubuntu on your machine
      2. Use a cloud-based instance and then SSH into it from your Windows machine

  16. Akaps August 24, 2018 at 2:23 am #

    Hey, I have Ubuntu 14.04 machine and there is a error “fatal error:opencv2/xfeatures2d/cuda.hpp: there is no file” when i use make -j4 command. How can i solve this problem?

    Thanks

    • Adrian Rosebrock August 24, 2018 at 8:31 am #

      For whatever reason it appears CMake found (or thinks it found) CUDA drivers on your system. Add the following configuration to your “cmake” command:

      -D WITH_CUDA=OFF

  17. Tham August 24, 2018 at 1:40 pm #

    Do the opencl backend of opencv4 support nvidia/amd gpu?

    • Adrian Rosebrock August 30, 2018 at 9:41 am #

      No, there will be separate functionality added for CUDA-specific functions.

  18. Bruce August 26, 2018 at 9:10 am #

    This whole use of virtual environment seems to be a hassle ever since Open CV3. There are already a thousand of steps to install this and if a step goes wrong we have to start over.

    Can we just skip the virtual environment part.

    • Adrian Rosebrock August 30, 2018 at 9:28 am #

      Python virtual environments are a best practice, Bruce. No one is forcing you to use them and you can absolutely skip them if you want. Secondly, the OpenCV version doesn’t have anything to do with Python virtual environments. They are totally independent and not related.

  19. Ali August 27, 2018 at 11:44 am #

    Hey,

    i did follow your tutorial but for some reason, i can not find the final .so file in my system to link the cv2.so to that (I mean this file: cv2.cpython-35m-x86_64-linux-gnu.so).

    The make command and everything finishes without error and i see many .so files built but not the one i mentioned above.

    Thanks for your help.

    Regards

    • Adrian Rosebrock August 30, 2018 at 9:15 am #

      Hey Ali — can you check your “build” directory as well? My guess is that your “Python 3” section from the “cmake” command did not match mine and ultimately the Python bindings were not built.

      • Rick September 17, 2018 at 12:39 am #

        I also have no cv2.so and no Python 3 section from cmake. Python (for build) is there, with OpenCL immediately preceding it.

        • Rick September 17, 2018 at 12:46 am #

          fyi I am running a new install of Ubuntu 18.04 which has Python 3.6.5. /usr/local/lib/python3.6 contains dist-packages but no site-packages directory.

          • Adrian Rosebrock September 17, 2018 at 2:10 pm #

            If the “Python 3” section is not filled out like mine is then your OpenCV bindings will not be built. Go back and double-check that you are in the “cv” Python virtual environment (or whatever you named it) before continuing. You should also delete your “build” directory, re-create it, and re-run “cmake” to ensure there are no cached files from your previous configuration.

      • Rick September 17, 2018 at 3:14 pm #

        Thanks Adrian. But still no Python 3 section after running cmake in a new build directory.
        “workon cv” appears to work. Running python starts 3.6.5 instead of 2.7 without virtualenv, and my prompt begins with “(cv) “.

        • Adrian Rosebrock September 17, 2018 at 3:18 pm #

          Just to clarify, this tutorial doesn’t use Python 2.7 either. Would you be able to check with a Python virtual environment? Happy to try to help further but unfortunately these issues are super tricky to diagnose without having physical access to your machine.

          • Rick September 17, 2018 at 3:39 pm #

            Yes, I installed virtualenv as instructed and it appears to be correctly working with python3 from the behavior I mentioned above.

            I understand. I’m not familiar with cmake but will try to determine why it doesn’t build for python.

            Thanks.

          • Adrian Rosebrock September 17, 2018 at 4:06 pm #

            Thanks Rick, best of luck with it! 🙂

          • Rick September 17, 2018 at 9:20 pm #

            My problem was with virtualenv. I don’t know why but cmake built for python fine by skipping “workon cv” and after pointing the “-D PYTHON_EXECUTABLE=” line to /usr/bin/python3.

            I can use virtualenv now after creating the link with ln -s, except my cv2 file went to dist-packages instead of site-packages.

            Thanks again Adrian. Fun stuff. 🙂

          • Adrian Rosebrock September 18, 2018 at 7:18 am #

            Awesome, I’m glad you were able to resolve the issue, Rick! Great job 😀

          • Slava Klevchenia November 5, 2018 at 8:34 pm #

            Hello Adrian,

            I faced with the similar issue as Rick, but as I find for myself – the reason was miniconda installed. At the step “cmake -D CMAKE_BUILD_TYPE=RELEASE \…” I didn’t get Python information and got miniconda python executable as executable for build.
            Maybe there is some better solution, but I could solve it only by deletion of miniconda.

            Maybe it helps for somebody.

          • Adrian Rosebrock November 6, 2018 at 1:09 pm #

            Thank you for sharing the solution, Slava.

  20. rajeev August 31, 2018 at 11:55 pm #

    make -j4. i am having issue with this command. i can’t compile. please help.

    • Adrian Rosebrock September 5, 2018 at 9:14 am #

      What is the exact issue you are having? Without knowing the exact error I cannot provide any suggestions.

  21. John September 4, 2018 at 5:30 am #

    I’m able to get the test video to work but I am unable to try it with my own webcam. (Ubuntu 18.04 on VmWare, Windows Host) I’m able to view the webcam with Cheese but can’t capture any frames with openCV. I’ve searched around but haven’t found any fixes yet. Thoughts?

    • Adrian Rosebrock September 5, 2018 at 8:42 am #

      A VM by definition cannot access your webcam. Some combinations of VM software and hosts allow you to choose a setting to allow the VM to access your webcam but you’ll want to refer to your VMWware documentation to confirm this.

  22. Corby September 7, 2018 at 4:44 pm #

    If I download this what about my existing CV3? Do I delete a folder or series of folders? Or do I simply delete the build folder and run Make again? Or create a new environment say, cv4 and when completely satisfied delete the cv environment?

    • Adrian Rosebrock September 11, 2018 at 8:25 am #

      I highly recommend you create a new Python virtual environment along with a brand new “build” directory for the compile.

  23. Heiko September 11, 2018 at 6:01 am #

    Hi Adrian,
    you are doing a really great job. It is awesome to see how your work is influencing others.

    I’m an absolute rookie on linux and opencv.
    I’ve got some small projects already working on raspberry, but due to the limited performance I was wondering if I could install opencv on my computer.
    So I used Windows 10 and the linux subsystem. Installed there the Ubuntu 18.04 from microsoft store.
    Updated / upgraded and everything works fine until the start of cmake.
    End of your #3 you suggest to check the “workon cv” if it is working.
    Later it is not clear for me, if I have to do the next steps inside of (CV) or in the regular linux environment.
    So I did the numpy successfully sand tried cmake inside of (CV) afterwards,but got the very content less message “command not found”. Staring cmake with help switch, it works.

    > workon cv
    (CV) > installing numpy > making directory > starting cmake = “command not found”

    Maybe you have an idea what I’m doing wrong.
    But it could be waste of time, due to my rookie status.

    If my English sounds strange sorry, it is not my first language 😉

    • Adrian Rosebrock September 11, 2018 at 7:59 am #

      Hi Heiko — your English is very understandable 🙂 As for your error, I have not tried using the Linux subsystem on Windows so I’m not sure if these instructions will work. Theoretically they should but I’m not 100% sure. That said, if the “cmake” command is not found I think you may have forgotten this step where we install “cmake”:

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

  24. Marcelo September 12, 2018 at 12:18 am #

    Hi Adrian, I received an error similar to this of the pastebin https://pastebin.com/RHHqW6ZM, in make -j4, and i don’t have idea how to solve, can you help me?

    • Adrian Rosebrock September 12, 2018 at 2:09 pm #

      It looks like building the examples is causing the problem. Try turning off the BUILD_EXAMPLES switch:

      -D BUILD_EXAMPLES=OFF

  25. Nurettin September 14, 2018 at 2:18 pm #

    Hi Adrian, I’m using CLion (for c++) on Ubuntu 16.04 but I’m getting errors during build with OpenCv 4. Tried with C++11 and C++14. I couldn’t imread. Is there any way to install opencv 3.4 because wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip doesn’t work anymore? How can I do this

    • Adrian Rosebrock September 17, 2018 at 2:58 pm #

      You can actually just grab .zip files (i.e., the “Sources”) for whatever version of OpenCV you want from the official OpenCV Releases page.

  26. mky September 26, 2018 at 10:43 am #

    Hi Adrian ! Your tutorial is great!
    I have just started to google some information about computer vision and luckily found your web page, and then after make this tracking_ball example I start to feel more motivated to study deeper about computer vision.
    Thanks a lot.

    PS. just sharing a small issue of my configuration (ubuntu 16.04 and python 3.5.2)
    Step #4, cmake failed with “fatal error: linux/videodev.h: No such file or directory”
    Fixed using: “sudo ln – s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h”

  27. Nasir October 15, 2018 at 2:59 am #

    Hi Adrian! Thanks for such a great post.

    My question is I have already OpenCV 3.4.0 installed on my system with python 3.5.2. It works fine. However, I want to upgrade my OpenCV to 4.0.0. Just following these steps are enough for upgrading my OpenCV or do I need to remove OpenCV 3 first and then should follow these steps?

    Thanks

    • Adrian Rosebrock October 16, 2018 at 8:34 am #

      No need to uninstall OpenCV 3. Just follow along with these steps but make sure you use a separate Python virtual environment for OpenCV 4. Otherwise everything else is the same.

  28. raph October 24, 2018 at 8:01 am #

    I did like you concerning the installation of opencv 4 but for me doesn’t work again,it put at the end :no module import
    please help me
    thank you

  29. raph October 24, 2018 at 8:38 am #

    I found thank you for your this tutoriel

    • Adrian Rosebrock October 29, 2018 at 1:59 pm #

      Congrats on resolving the issue, raph!

  30. JP October 30, 2018 at 10:15 am #

    Adrian:

    After some struggling I was able to get going on OpenCV using your tutorial.

    Mistakes I made:
    1) Took me a while to realize I was using Python 3.6, not 3.5.
    2) Ignoring your warning I was able to bungle the sym link (related to mistake 1) ).
    3) I had to compile using only 1 core (using an AMD Phenom). Otherwise it bombed on some error, I forget which.

    Congratulations on an excellent tutorial.
    Once I get a little acclimatized you will surely be hearing from me again (that’s a warning, ha!).

    Thank you very much!
    JP.

    P.S.: I am an old programmer, but am fairly new to C, Python, Linux, and obviously OpenCV.
    JP.

    • Adrian Rosebrock November 2, 2018 at 8:20 am #

      Congrats on getting OpenCV installed on your Ubuntu machine, JP! And thank you for sharing what tripped you up — I’m sure that information will help other readers as well.

  31. QM November 6, 2018 at 5:12 am #

    Hi !
    I recently install OpenCV 4.0 on my ubuntu 16.04 machine. It work perfectly but…
    When I use surf = cv2.xfeatures2d.SURF_create() or SIFT
    I get :


    surf = cv2.xfeatures2d.SURF_create()
    AttributeError: module ‘cv2’ has no attribute ‘xfeatures2d’

    do you have any idea ?

    • Adrian Rosebrock November 6, 2018 at 1:04 pm #

      Your path to the OpenCV contrib modules directory is incorrect. Double-check your director paths in “cmake” and try again.

  32. Akhilesh November 6, 2018 at 6:40 am #

    Hello Adrian
    Thanks for a well-written installation guide. I am trying to install OpenCV4 with python in anaconda environment. However, I checked the opencv/build/lib folder, and didn’t find cv2.so file. How do I resolve this?

    Thanks and Regards
    Akhilesh Mahajan

    • Adrian Rosebrock November 6, 2018 at 12:32 pm #

      It sounds like it may be specific to Anaconda. You’ll either need to

      1. Configure Anaconda to point to your Python libraries (which is something I only recommend advanced users do)
      2. Not use Anaconda and instead use a different Python distribution
      3. Wait until OpenCV 4 is officially released

      I hope that helps point you in the right direction!

      • vladimircape November 12, 2018 at 6:35 am #

        or try this )))

        cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/home/user/anaconda3 \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D BUILD_opencv_python2=OFF \
        -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
        -D PYTHON_DEFAULT_EXECUTABLE=/home/user/anaconda3/bin/python3 \
        -D PYTHON_EXECUTABLE=/home/user/anaconda3/bin/python3 \
        -D PYTHON3_EXECUTABLE=/home/user/anaconda3/bin/python3 \
        -D PYTHON_INCLUDE_DIR=/home/user/anaconda3/include/python3.6m \
        -D PYTHON_PACKAGES_PATH=/home/user/anaconda3/lib/python3.6/site-packages \
        -D PYTHON_EXECUTABLE=/home/user/anaconda3/bin/python3 \
        -D PYTHON_LIBRARY=/home/user/anaconda3/lib/libpython3.6m.so \
        -D ZLIB_LIBRARY_RELEASE=/home/user/anaconda3/lib/libz.so \
        -D PNG_LIBRARY_RELEASE=/home/user/anaconda3/lib/libpng.so \
        -D JPEG_LIBRARY=/home/user/anaconda3/lib/libjpeg.so \
        -D TIFF_LIBRARY_RELEASE=/home/user/anaconda3/lib/libtiff.so \
        -D BUILD_EXAMPLES=ON ..

        make -j4 VERBOSE=1

  33. Ramjan November 7, 2018 at 1:46 am #

    Hi! Arian
    I’m not able to do ~/.bashrc .
    can you tell me properly?
    or any link
    Please.

    • Adrian Rosebrock November 10, 2018 at 10:20 am #

      What specifically are you having trouble with regarding the .bashrc file?

Leave a Reply