How to build OpenCV with GPU support via MacPorts in OS X 10.8

First thing first, install the NVIDIA CUDA libraries from (take notice that last night, OS X 10.9 Mavericks has got its update too!)

Second, open Terminal and add the two paths below

export PATH=/Developer/NVIDIA/CUDA-5.5/bin:$PATH

if previously installed, uninstall opencv.

sudo port uninstall opencv @

If you have a different version installed, MacPorts will tell you and you’ll need to change the one above with your own, of course. Now you need to edit the opencv’s portfile

sudo port edit --editor nano opencv

nano will open up, scroll down and change the appropriate settings to


and optionally


you “could” also add


(although I’m not completely sure this is the right syntax for cusparse though! Please do let me know if this is the case)

Now, it’s time to clean the previous installation

sudo port clean opencv

And at last, type

sudo port upgrade -s -n --force opencv

to rebuild the amended portfile. The flag -n avoids rebuilding of the dependencies and -s starts building from the source code only with no binaries whatsoever and now you can go get yourself a cuppa, even two, as this step will take a bit (about half a hour on a rMBP).

These links below can be helpful too:

Post a comment or leave a trackback: Trackback URL.


  • Cao Minh Vũ  On November 12, 2013 at 6:25 pm

    It’s not working. After change -DWITH_CUDA=OFF to -DWITH_CUDA=ON, Macport report error:
    Error: org.macports.configure for port opencv returned: configure failure: command execution failed
    Please see the log file for port opencv for details:

    • ivanoras  On November 12, 2013 at 6:56 pm

      You’re right, Cao. Since the update to Xcode 5.0.1 I’m getting the same error. I even updated to opencv 2.7 but no joy. I posted a question on opencv.answers and will update this post as soon as possible.

      • wwward  On December 11, 2013 at 5:24 am

        Does the error begin with a reference to “LoadFromXML?” in the cascadeclassifier.cpp file? I’m encountering this as well, and have Xcode 5.0.2 loaded on Mavericks. Though I also encountered this when compiling OpenCV 2.4.7 from Github source against CUDA SDK 5.5. I figured I would work through it further but I have a class project due Friday so I may have to back out of CUDA acceleration until I have time to work through the linker errors more thoroughly.

        Thanks for posting the original instructions, ivanoras, since it helped me to understand that things did work at one time.

      • ivanoras  On December 11, 2013 at 6:49 am

        Hey William, glad post helped somehow. From what I gathered throughout the past 2 1/2 months, the latest Nvidia CUDA drivers (5.5) available for OS X 10.9.0 may be broken.

        I joined the Nvidia developer team and waiting on them to post the link to CUDA 6.0 RC, the one with unified memory facility plus blah blah blah that should be due soon but… if I had a class project due Friday, I would do one of the following:

        1) go without acceleration if you can.
        2) reinstall Mountain Lion with the old Xcode bundle and appropriate cuda drivers
        3) install either Ubuntu 12.04 or Windows in another partition and install the appropriate cuda drivers.

        I hope this helps,

      • wwward  On December 18, 2013 at 5:10 am

        Thank you! I made do without it this time, but the machine I was testing on is my most powerful and most convenient to use, so I do look forward to the update.

        Best of luck, take care!

      • ivanoras  On December 18, 2013 at 6:32 am

        Cool. Thanks, you too. I will update the instructions as soon as there are news.

  • Giorgio  On March 6, 2014 at 4:32 pm

    Hi, I got the same error on Mavericks, CUDA 5.5.47 and Xcode 5.0.2…Let us know if there are some news. Thank again.

    • ivanoras  On March 6, 2014 at 6:50 pm

      Hi Giorgio, I’m aware of that. I recently did try with the CUDA 6.0 RC and it doesn’t work either. I raised a ticket at (sign-up required) and honestly more than arguing with the developer in charge of the opencv port, I don’t know how else I can quickly help.

      He reminded me that nvcc doesn’t compile in clang, so I need to compile the code separately, …yada yada yada. All I know is that if you guys would raise a ticket too, the issue could be maybe taken more seriously and possibly sorted. All the best.

  • John  On June 3, 2014 at 7:53 pm

    How do you change the configure.compiler property?

    • ivanoras  On June 4, 2014 at 4:19 am

      I left the default one, not sure switching to gcc 4.2 works. The additional parameter to add to the sudo port install would be configure.compiler=apple-gcc-4.2

      Let me know if it works for you please.

  • ivanoras  On October 14, 2014 at 1:53 pm

    Hi dude, this link (sign-up required may provide you with hints about the tool chain to stick to. MacPorts is no longer helpful to get OpenCV up to speed with CUDA out of the box. Alternatively, you could use OpenCL within OpenCV.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: