Tutorials

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 \
  -DCMAKE_SYSTEM_PROCESSOR=arm64 \
  -DCMAKE_OSX_ARCHITECTURES=arm64 \
  -DWITH_OPENJPEG=OFF \
  -DWITH_IPP=OFF \
  -D CMAKE_BUILD_TYPE=RELEASE \
  -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 \
  -D INSTALL_PYTHON_EXAMPLES=ON \
  -D INSTALL_C_EXAMPLES=OFF \
  -D OPENCV_ENABLE_NONFREE=ON \
  -D BUILD_EXAMPLES=ON ..
make -j8
sudo make install

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

% mdfind cv2.cpython
/.../opencv-4.5.0/build/lib/python3/cv2.cpython-38-darwin.so
/usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so
% 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__

2 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.

    Thanks!
    Kind regards,
    Tin

    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"
      else
      if [ -f "/opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh" ]; t$
      . "/opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh"
      else
      export PATH="/opt/homebrew/Caskroom/miniforge/base/bin:$PATH"
      fi
      fi
      unset __conda_setup
      # <<< conda initialize <<<

Leave a Reply

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