Compiling OpenCV with CUDA support


Alight, so you have the NVIDIA CUDA Toolkit and cuDNN library installed on your GPU-enabled system.

What next?

Let’s get OpenCV installed with CUDA support as well.

While OpenCV itself doesn’t play a critical role in deep learning, it is used by other deep learning libraries such as Caffe, specifically in “utility” programs (such as building a dataset of images). Simply put, having OpenCV installed makes it easier to write code to facilitate the procedure of pre-processing images prior to feeding them into deep neural networks.

Because of this, we should install OpenCV into the same environment as our deep learning libraries, to at the very least, make our lives easier.

Furthermore, in a GPU-enabled CUDA environment, there are a number of compile-time optimizations we can make to OpenCV, allowing it to take advantage of the GPU for faster computation (but mainly for C++ applications, not so much for Python, at least at the present time).

I’ll be making the assumption that you’ll be installing OpenCV into the same environment as last week’s blog post — in this case, I’ll be continuing my example of using the Ubuntu 14.04 g2.2xlarge instance on Amazon EC2.

Truth be told, I’ve already covered installing OpenCV on Ubuntu in many previous blog posts, but I’ll explain the process here as well. Overall, the instructions are near identical, but with a few important changes inside the cmake  command, allowing us to compile OpenCV with CUDA support.

By the time you finish reading this blog post, you’ll have OpenCV with CUDA support compiled and installed in your deep learning development environment.

Installing OpenCV with CUDA support

Before we can compile OpenCV with CUDA support, we first need to install some prerequisites:

If you’re a follower of the PyImageSearch blog, then you’ll also know that I’m a big fan of using pip , virtualenv , and virtualenvwrapper  to create sequestered, independent Python virtual environments for each of our projects. You can install the virtual environment packages using the commands listed below (or you can skip this step if you already have Python virtual environments setup on your machine):

If this is your first time using Python virtual environments, I would suggest reading the first half of this blog post to familiarize yourself with them. The blog also has an excellent article on Python virtual environments for the uninitiated.

Next, let’s use update our ~/.bashrc  file. Open this file using your favorite command line text editor (such as nano , vi , or emacs ):

Then, scroll down to the bottom of the file, append the following lines, and save + exit the editor:

At this point, we can create our cv  virtual environment:

Note: Again, you’ll want to read the first half of this blog post to better understand Python virtual environments if this is your first time using them. I also explain them more thoroughly and how to properly use them in other OpenCV installation guides on this website.

Now, let’s download and unpack OpenCV. If you’re using the default Amazon EC2 g2.2xlarge instance, then I highly suggest that you download the OpenCV sources and do your compiling on /mnt .

The default g2.2xlarge instance has only ~8GB of space, which once you factor in the system files, NVIDIA drivers, etc., is not enough room to compile OpenCV from source:

Figure 1: The default disk size for the g2.2xlarge instance is only 8GB, which doesn't leave enough space to compile OpenCV from source.

Figure 1: The default disk size for the g2.2xlarge instance is only 8GB, which doesn’t leave enough space to compile OpenCV from source.

However, the /mnt  volume has 64GB of space, which is more than enough for our compile:

Figure 2: However, if we use the '/mnt' volume instead, we have 64GB -- far more than what is required to compile OpenCV.

Figure 2: However, if we use the ‘/mnt’ volume instead, we have 64GB — far more than what is required to compile OpenCV.

If you are indeed on an Amazon EC2 instance, be sure to change directory to /mnt  and create a directory specifically for your OpenCV compile prior to downloading the source:

The above command will create a new directory named opencv_compile  in the /mnt  volume, followed by giving the ubuntu  user permission to modify it at their will.

Note: The /mnt  volume is what Amazon calls “ephemeral storage”. Any data put on this volume will be lost when your system is stopped/rebooted. You don’t want to use /mnt  to store long-term data, but it’s perfectly fine to use /mnt  to compile OpenCV. Once OpenCV is compiled, it will be installed to the system drive — your OpenCV installation will not disappear between reboots.

For this tutorial, I’ll be using OpenCV 3.1. But you could also use OpenCV 2.4.X or OpenCV 3.0. Use the following commands to download the source:

In case the URLs of the .zip  archives are cutoff, I’ve included them below:

We are now ready to use cmake  to configure our build. Take special care when running this command, as I’m introducing some configuration variables you may not be familiar with:

To start, take note of the WITH_CUDA=ON  flag. Technically, this flag will be set to ON  by default since CMake is smart enough to detect that the CUDA Toolkit is installed. But, just in case, we’ll manually set the variable to WITH_CUDA=ON  to ensure CUDA support is compiled.

From there, we add in a few more optimizations, mainly around using cuBLAS, an implementation of the BLAS (Basic Linear Algebra Subprograms) library in the CUDA runtime.

We also indicate that we want to utilize the “fast math” optimizations, a series of extremely fast mathematical routines that are optimized for speed (they are written in Assembly) — and essentially perform little-to-no error checking. Again, the FastMath libraries are geared towards pure speed and nothing else.

After running cmake , take a look at the “NVIDIA CUDA” section — it should look similar to mine, which I have included below:

Figure 3: Examining the output of CMake to ensure OpenCV will be compiled with CUDA support.

Figure 3: Examining the output of CMake to ensure OpenCV will be compiled with CUDA support.

Notice how CUDA support is going to be compiled using both cuBLAS and “fast math” optimizations.

Provided that your own CMake command exited without error, you can now compile and install OpenCV:

If all goes well, the make  command should run successfully:

Figure 4: OpenCV with CUDA support has successfully compiled.

Figure 4: OpenCV with CUDA support has successfully compiled.

Again, assuming your compile finished without error, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages . You can verify this using the ls  command:

Note: You’ll want to find and make note of where your  file is on your system! Whenever we create a virtual environment (which we’ll be doing lots of to explore various deep learning libraries), you’ll want to sym-link the  file into the site-packages  directory of your Python virtual environment so you have access to OpenCV.

The last step is to sym-link the  file (our Python bindings) into the cv  virtual environment:

To verify our installation, open up a new terminal, access the cv  virtual environment using the workon  command, fire up a Python shell, and then import OpenCV:

Finally, now that OpenCV is installed, let’s perform a bit of cleanup and remove the source files used for installation:

Again, I can’t stress this point enough — you need to get comfortable working with Python virtual environments, the site-packages  directory, and how to use symbolic links. I recommend the following tutorials to help understand each of them:


In today’s blog post, I detailed how to install OpenCV into our deep learning environment with CUDA support. While OpenCV itself isn’t directly used for deep learning, other deep learning libraries (for example, Caffe) indirectly use OpenCV.

Furthermore, by installing OpenCV with CUDA support, we can take advantage of the GPU for further optimized operations (at least from within C++ applications — there isn’t much support for Python + OpenCV + GPU, yet).

Next week, I’ll detail how to install the Keras Python package for deep learning and Convolutional Neural Networks — from there, the real fun will start!

, , , , , ,

40 Responses to Compiling OpenCV with CUDA support

  1. Mahendra Prabhu July 11, 2016 at 12:51 pm #


    I have struggling to compile OpenCV on EC2. Thank you for your detailed walk through. I will try this.

    Have you tried installing OpenCV on Google compute engine IaaS platform? I was initially leaning on that route to leverage using TensorFlow.

    • Adrian Rosebrock July 12, 2016 at 4:36 pm #

      I have not tried compiling OpenCV on any of Google’s services yet. I’ll consider that in the future.

  2. Hide Sirai July 24, 2016 at 4:28 am #

    Thanks for your tutorial.

    I just tried, and I think one thing should be noted for beginners.

    After making OpenCV, is located under build/lib.
    So you should move it to /usr/local/lib/python2.7/site-packages/
    with the following:
    sudo cp lib/ /usr/local/lib/python2.7/site-packages/

    • Adrian Rosebrock July 27, 2016 at 2:41 pm #

      After running sudo make install your file should be automatically copied to /usr/local/lib/python2.7/site-packages/. But if not, yes, absolutely manually copy it there.

      • Grant October 1, 2016 at 12:50 pm #

        I also have to copy the manually.

  3. Grant October 1, 2016 at 12:54 pm #

    Need to manually download ippicv_linux_20151201.tgz and copy to
    Else build will report error, said download failed. (maybe because of poor net connection? but when I manually download the file, the speed is fast)

    • Adrian Rosebrock October 2, 2016 at 8:58 am #

      This sounds like a poor internet connection issue. Normally when compiling OpenCV this file is automatically downloaded via the CMake process.

  4. Marcin October 4, 2016 at 7:16 am #

    Hi everyone,

    i just want to add if u have an issue with “‘memcpy’ was not declared in this scope” just add to cmake before running :cmake …. -DCUDA_NVCC_FLAGS=”-D_FORCE_INLINES” ….

    worked on ubt 16.04, cuda 7.5 gcc 4.9


    • Adrian Rosebrock October 6, 2016 at 7:03 am #

      Thanks for sharing Marcin!

  5. brocolpif December 2, 2016 at 7:37 am #

    Hi all,

    I had to apply these patches manually

    Don’t know why the 3..1 do not contains these patches.

  6. Joseph March 22, 2017 at 8:06 am #

    I don’t understand why you give python examples while OpenCV will NOT use Cuda functions via python

    • Adrian Rosebrock March 22, 2017 at 8:28 am #

      Correct, the GPU bindings with OpenCV are not available with Python, but you could call them within a Python script as a compiled C++ extension or you might be using CUDA support in another program. In either case, it’s important to see how OpenCV is hand compiled with CUDA support.

  7. Naif Allehyani April 1, 2017 at 6:51 pm #

    hello.. its been a rough day with opencv … cuda is installed and when i run nvcc -V it prints the cuda 7.5 that i am using.. then i tried to compile opencv with cuda by following this tutorial.. i had no problem and no errors and followed all the steps, cmake, make -j4, and sudo make install.. all worked fine.. but when i try to import cv2 it seems that its not installed.. when i list the files that are in /usr/local/lib/python2.7/site-packages its zero no files are there.. so i ran this command to locate the file and this was the results:
    naif@naif-Z170-D3H:~$ sudo find / -name “**”
    [sudo] password for naif:

    what went wrong?? any suggestions?

    • Adrian Rosebrock April 3, 2017 at 2:05 pm #

      Are you in the “cv” virtual environment before importing?

      Make sure you are in the “cv” virtual environment and that should resolve the issue.

      • naif allehyani April 6, 2017 at 12:29 pm #


        actually i didnt set up a virtual environment so i didnt need it, i reinstall and compile it , and got error is cmake, and found this in opencv docs that in some systems it might need to fix cmake macro, so i ran

        $ git cherry pick cdb9c

        and it worked fine.


  8. Moe June 25, 2017 at 7:18 pm #

    $ are so stupid and annoying.

    • Adrian Rosebrock June 27, 2017 at 6:30 am #

      The “$” sign in the commands simply indicates the terminal prompt. This is a standard practice when including and discussing commands that need to be executed inside a shell.

  9. Mustafa July 7, 2017 at 10:32 am #

    No module named cv2


    When I tried to show Total, it is 0…. and there is no What should i do? Platform: JetsonTK1, JetPack 3.0

    • Adrian Rosebrock July 11, 2017 at 6:56 am #

      1. Make sure you are in the “cv” virtual environment before importing cv2.
      2. Check the “build/lib” directory to ensure your Python bindings are compiled.
      3. Read through the troubleshooting suggestions in this guide.

  10. Melvyn Drag July 30, 2017 at 2:22 pm #

    For me installed in dist-packages. Seems fine as per

    I’m on Ubuntu16.10

    • Melvyn Drag July 30, 2017 at 2:30 pm #

      Oh and its worth mentioning that I installed 3.2 from the github source

      • Adrian Rosebrock August 1, 2017 at 9:43 am #

        Thanks for sharing Melvyn!

  11. Ariba February 9, 2018 at 6:55 am #

    does this work for CUDA 9.1 too? i’m getting errors.

  12. Martin Faucheux March 29, 2018 at 5:21 am #

    Hi ! I’m having some trouble compiling openCV with CUDA.
    I am on Ubuntu 17.10 with python 3.6.4, environment managed by anaconda. My CUDA version is 9.1
    I followed your instructions, expecting some errors as I don’t have the same setup.
    My cmake command looks like this

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D WITH_CUBLAS=1 \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.1.0/modules \
    -D CUDA_ARCH_BIN=5.2 \
    -D CUDA_NVCC_FLAGS=”-ccbin gcc-6″ \

    I manually added my cuda arch because I had some trouble with the autodetect parameter.
    I set the compiler to gcc-6 because I read cuda doesn’t support newer versions.
    Also, after wandering on loads of forums including one of your other tutorial, I read that enabling precompiled headers could leads to some problems so I deactivated it. My cmake doesnt show any error but I still cant compile opencv. Currently, the error is the following one, which doesn’t help me much…

    /home/martin/opencv/opencv-3.1.0/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function(“abs”) from a __device__ function(“abs”) is not allowed. The experimental flag ‘–expt-relaxed-constexpr’ can be used to allow this.

    Any idea or advice taking in account my set up ? cheers

    • Adrian Rosebrock March 30, 2018 at 7:02 am #

      Hey Martin — thanks for the comment. I haven’t tested this tutorial with Ubuntu 17.04 or Python 3.6 so I’m not sure what the issue is off the top of my head. Which version of CUDA are you using? From there I might be able to point you in the right direction.

      • Martin Faucheux March 30, 2018 at 7:09 am #

        I installed cuda 9.1 and I have been able to use it with pyTorch so I assume it is correctly installed on my machine.

        • Adrian Rosebrock March 30, 2018 at 7:36 am #

          If you can use CUDA 9 with Pytorch then it certainly sounds like CUDA is properly configured on your machine. Unfortunately I’m not sure what the exact problem is. I would post on the official OpenCV forums as well as open a issue on the OpenCV GitHub. It sounds like it may be a CUDA + OpenCV incompatibility error.

          After you post on both places try to compile with OpenCV 3.3 and OpenCV 3.4 to see if the newer versions are compatible with CUDA 9.

          • Martin Faucheux March 30, 2018 at 8:24 am #

            yeah ok I’ll try that. Thanks a lot !

        • Ideo G April 28, 2018 at 4:08 am #

          I have successfully compiled opencv 3.4.1 version with CUDA 9.1. My cmake build looks like:
          cmake -D CMAKE_BUILD_TYPE=RELEASE \
          -D CMAKE_INSTALL_PREFIX=/usr/local \
          -D WITH_CUDA=ON \
          -D ENABLE_FAST_MATH=1 \
          -D CUDA_FAST_MATH=1 \
          -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-9.1 \
          -D WITH_CUBLAS=1 \
          -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules \
          -D BUILD_EXAMPLES=ON ..

          • cbasavaraj June 22, 2018 at 2:07 pm #

            Hi, do you have to specify gcc-6 as well for CUDA?
            And have you tried running and deep models using cv2.dnn.readNetFromCaffe(protoFile, weightsFile)?
            Do they run at the rates you would expect from CUDA?

  13. Guru June 26, 2018 at 10:30 am #

    Folks, Need an advice, I am using OpenCV 3.4, Python 3, CUDA enabled and 1080 TI GPU, i am executing caffe model for object detection using a CCTV camera, but I see instead of GPU, Intel i7 is taking the processing 13% … can you please help me find a solution to only use GPU for processing …

  14. Guru June 30, 2018 at 8:54 am #

    Hi Adrian,

    Can you please enlighten me, if opencv 3.4 python has CUDA support now; as i see when i execute caffe model in opencv for object detection using webcam, only proccessor is used and not the GPU and T-API UMat(OpenCL) also not faster. Can you please help me.


    • Adrian Rosebrock July 3, 2018 at 8:45 am #

      OpenCV, CUDA, and OpenCL are different beasts. OpenCV hasn’t supported CUDA as a backend to their “dnn” module as far as I know.

      • Elena March 24, 2019 at 1:02 pm #

        Could you please explain what is OpenCL, I have an Nvidia GPU and I am confused about how OpenCL is related to Nvidia?
        Thanks in advance.

        • Adrian Rosebrock March 27, 2019 at 9:02 am #

          I don’t have any tutorials on OpenCL at the moment but I will certainly consider it for a future post.

  15. Tom Cipollone August 31, 2018 at 1:17 pm #

    Sorry if I am repeating this post. I thought that I had sent it but I do not see it as having been posted.

    My virtual environment does not find My normal shell does find it. is in the proper directory, at least I think so.


    When in the non virtual python environment, cv2 is imported properly. When in the virtual environment I get:

    “ImportError: No module named cv2”

    Where has the link gone wrong.

    Thank You

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

      There are a few reasons why this may happen, but unfortunately it’s incredibly hard to diagnose. Make sure you refer to the “Troubleshooting and FAQ” section of this tutorial.

  16. Elena March 16, 2019 at 1:38 am #

    Thank you for the useful information.
    If I install OpenCV with this method would I be able to run YOLO v3 (dnn- implementation- your blog post)on GPU?
    I have an Nvidia GPU, and I’d like to run YOLO’s OpenCV implementation on GPU

    • Adrian Rosebrock March 19, 2019 at 10:17 am #

      Unfortunately, no. OpenCV’s “dnn” module does not yet support NVIDIA GPUs. Support is hopefully coming this summer though.


  1. Installing Keras for deep learning - PyImageSearch - July 24, 2016

    […] Compiling OpenCV with CUDA support […]

Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply