Setup Machine Learning Environment/OpenCV/TF On M1 Mac (Apple Silicon)

Trayansh Dewangan
3 min readMar 2, 2021

Setup Xcode

Open Terminal and Execute

sudo xcode-select --install

Installing HomeBrew (Native Apple Silicon M1)

Open Terminal and Write One By One

This Will Install The Latest Brew For M1 Chip

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"echo “export PATH=/opt/homebrew/bin:$PATH” >> ~/.zshrc

//Restart The Terminal
brew install gccbrew install cmakebrew install wget

Installing Miniforge and Setting up Conda Environment

Click On the Link Below And Download The (Apple Silicon) Version

https://github.com/conda-forge/miniforge

Open Terminal and Execute The Following

// If the Downloaded File Stored in Download
cd Downloads
bash Miniforge3-MacOSX-arm64.sh//After Installation Completes Restart Terminal//Creating Conda Environment named ml You can use any name in place of "ml"conda create --name mlconda activate mlconda install -y python==3.8.6conda install -y pandas matplotlib scikit-learn jupyterlab

Installing Tensorflow

Click On the Link Below And Download The file

https://github.com/apple/tensorflow_macos/releases

Am Using The latest TF alpha 3 build for M1 As of 4/18/2021.

//if Download Directory is Downloads
cd Downloads
tar xvf tensorflow_macos-0.1alpha3.tar.gz
cd tensorflow_macos/arm64
//Dont Forget To Activate Conda Environment conda activate ml// Install specific pip version and some other base packages
pip install --force pip==20.2.4 wheel setuptools cached-property six
// Install all the packages provided by Apple but TensorFlow
pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
// Install additional packages
pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard
// Install TensorFlow
pip install --upgrade --force --no-dependencies tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl

Installing Additional Packages

pip install matplotlib
conda install -c conda-forge scikit-learn
pip install keras
pip install notebook

Compiling and Installing OpenCV

//I Suggest To Do all this Inside miniforge3 dir for that
// cd miniforge3
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.0mkdir build && cd build//Here Take Care Of Paths of OPENCV_EXTRA_MODULES_PATH and
// PYTHON3_EXECUTABLE If you're Beginner watch the YouTube video
//And If Inside miniforge3 just place your <username>.
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=/Users/<username>/miniforge3/opencv_contrib-4.5.0/modules \
-D PYTHON3_EXECUTABLE=/Users/<username>/miniforge3/envs/ml/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
//"8" is the number of cores To be used(This Step Takes Time)
sudo make install//Linking OpenCV To Conda Environment mdfind cv2.cpython
//From the output Copy the Path similar to the below one
"/usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so cv2.so"cd cd miniforge3/envs/ml/lib/python3.8/site-packagesln -s PasteYourCopiedPathHere

--

--