Installing OpenCV on M1 MacBook

I have explained all the steps in this video tutorial. So while watching it, you can use the needed commands.

Open Terminal on mac and run the following commands:

Apple command line tools installation:

sudo xcodebuild -license
sudo xcode-select --install

brew installation:

/usr/bin/ruby -e "%(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
nano ~/.zshrc
export PATH=$PATH:/opt/homebrew/bin
source ~/.zshrc

Installing Miniforge:

brew install miniforge

Create Python environment in Conda:

conda create --name <environment_name>
conda activate <environment_name>
conda install -y python==3.8.6

Install Numpy:

% wget https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha0/tensorflow_macos-0.1alpha0.tar.gz
% tar xvf tensorflow_macos-0.1alpha0.tar.gz
% cd tensorflow_macos/arm64
% pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl 
% cd ~

Compile OpenCV:

% wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
% wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.0.zip
% unzip opencv.zip
% unzip opencv_contrib.zip
% cd opencv-4.5.0
% mkdir build && cd build
% cmake \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D OPENCV_EXTRA_MODULES_PATH=.../opencv_contrib-4.5.0/modules \
  -D PYTHON3_EXECUTABLE=.../miniforge/base/envs/TestOpenCv/bin/python3 \
  -D BUILD_opencv_python2=OFF \
  -D BUILD_opencv_python3=ON \
make -j8
sudo make install

Sym-link OpenCV 4 on macOS to virtual environment site-packages:

% mdfind cv2.cpython
% cd /.../miniforge3/envs/dev/lib/python3.8/site-packages
% ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so cv2.so
% python
>>> import cv2
>>> cv2.__version__

10 thoughts on “Installing OpenCV on M1 MacBook”

  1. Hello,

    I was just wondering if you could perhaps show what your .zshrc file looks like? I might have removed a couple of lines from the file and added the new line (export PATH…) in the wrong location.

    Kind regards,

    1. Hello,
      This is my .zshrc filr:

      # >>> conda initialize >>>
      # !! Contents within this block are managed by 'conda init' !!
      __conda_setup="$('/opt/homebrew/Caskroom/miniforge/base/bin/conda' 'shell.zsh' $
      if [ $? -eq 0 ]; then
      eval "$__conda_setup"
      if [ -f "/opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh" ]; t$
      . "/opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh"
      export PATH="/opt/homebrew/Caskroom/miniforge/base/bin:$PATH"
      unset __conda_setup
      # <<< conda initialize <<<

  2. Hi,

    My .zshrc file looks like:

    # >>> conda initialize >>>
    # !! Contents within this block are managed by ‘conda init’ !!
    __conda_setup=”$(‘/Users/seankoh/opt/anaconda3/bin/conda’ ‘shell.zsh’ ‘hook’ 2$
    if [ $? -eq 0 ]; then
    eval “$__conda_setup”
    if [ -f “/Users/seankoh/opt/anaconda3/etc/profile.d/conda.sh” ]; then
    . “/Users/seankoh/opt/anaconda3/etc/profile.d/conda.sh”
    export PATH=”/Users/seankoh/opt/anaconda3/bin:$PATH”
    unset __conda_setup
    # <<< conda initialize <<<

    How do I implement this code (export PATH=$PATH:/opt/homebrew/bin) as you said?

    Thank you for your help in advance.

  3. Hello,

    I have followed your tutorial (thank you) and everything went smoothly and uneventful until I try to run an application.

    I get this (see below) error (my application is called ‘Camera’ and is supposed to take continuous shots from the computer camera and show them in a JFrame on the screen).

    I use java 17 on a MacBook Pro (M1)

    java -Djava.library.path=/Users/liviu/Documents/Java/Libraries/opencv_build/lib Camera 0
    Exception in thread “main” java.lang.UnsatisfiedLinkError: /Users/liviu/Documents/Java/Libraries/opencv_build/lib/libopencv_java454.dylib: dlopen(/Users/liviu/Documents/Java/Libraries/opencv_build/lib/libopencv_java454.dylib, 1): no suitable image found. Did find:
    /Users/liviu/Documents/Java/Libraries/opencv_build/lib/libopencv_java454.dylib: mach-o, but wrong architecture
    /Users/liviu/Documents/Java/Libraries/opencv_build/lib/libopencv_java454.dylib: mach-o, but wrong architecture
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:384)
    at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:228)
    at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:170)
    at java.base/jdk.internal.loader.NativeLibraries.findFromPaths(NativeLibraries.java:311)
    at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:283)
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2422)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818)
    at java.base/java.lang.System.loadLibrary(System.java:1989)
    at Camera.main(Camera.java:28)
    (test_opencv) liviu@Livius-MacBook-Pro Camera %

  4. Hi, thanks a lot for the tutorial!
    I’m trying to follow your instructions, but when running the cmake command I immediately get the error

    CMake Error: The source directory “/Users/cdemasi/projects/opencv-4.5.0/build/

    even if I’m literally inside the opencv-4.5.0/build folder.
    Any clues why?

  5. Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I had written and say,
    I’m thoroughly enjoying your blog. I as well am an aspiring
    blog writer but I’m still new to everything.

    Do you have any points for inexperienced blog writers? I’d definitely appreciate it.

    1. Hey, I am so sorry about that. Thanks for your comments. Just keep writing and you will find out your way. Nothing is better than learning by practice. 😉

Leave a Reply

Your email address will not be published. Required fields are marked *