Install Miniconda on Mac M1/M2 chip and build tensorflow2 environment

Install Miniconda

  • Enter the official website

  • Download the M1 version

  • Note: enter the terminal and enter uname -a to know what processor you are. M1/M2 chips are all arm processors, so download the MacOSX-arm64 version

  • After the download is complete, find the .sh file in the ~/Downloads/ directory, and enter it in the terminal

sh Miniconda3-py39_4.11.0-MacOSX-arm64.sh
  • A software agreement clause will pop up for you to read. At this time, you can directly press Ctrl+C to skip the reading process. Follow the prompts, enter yes, and press Enter to agree to the software agreement terms.

  • My installation path is /Users/qianying/miniconda3

  • Next, continue to press Enter to install miniconda on the computer. After installation, the prompt is as follows:

Configure environment variables

  • At this time, the conda command cannot be called. For example, when you enter conda --version, it prompts that it is not found.

  • Type in terminal

vim ~/.zshrc
  • Add this sentence in it, that is, add the path where conda is located to the environment variable

export PATH=/Users/qianying/miniconda3/bin:$PATH
  • After saving, exit and enter in the terminal:

source ~/.zshrc
  • The word (base) appears in the terminal, because during installation, the base environment is automatically activated every time the terminal is opened. Enter in the terminal to cancel the automatic activation of the base environment.

conda config --set auto_activate_base false
  • After restarting the terminal, there will be no (base) words. Check the version number of conda, indicating that miniconda3 is installed successfully.

Configure domestic sources

  • Enter vim .condarc in the terminal, copy the following content into the .condarc file, save and exit.

ssl_verify: true
show_channel_urls: true

channels:
  - https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
  - https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
  - https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults

Create a tensorflow environment

Create an environment named tf2, python version 3.8

conda create -n tf2 python=3.8

Note: The sources of University of Science and Technology of China and Tsinghua University added by conda do not support python3.7 and below versions, so a python3.8 environment is created here.

Enter the virtual environment

conda activate tf2

Install the dependency tensorflow-deps of tensorflow on mac

conda install -c apple tensorflow-deps

Install the mac version of tensorflow

python -m pip install tensorflow-macos -i c
  • Note: This is not installed with conda here, because there is no such installation package in the conda source, so use pip to install the mac version of tensorflow instead.

Install related plug-ins to accelerate training and rely on tensorflow-metal (enabling mac to support gpu training)

python -m pip install tensorflow-metal -i https://pypi.douban.com/simple

conda command

  • View some information about conda

conda info
  • View conda environment

conda env listconda info --env
  • Create environment (example is to create an environment called py36, python version 3.6)

conda create -n py36 python=3.6
  • Remove the environment named py36

conda remove -n py36 --all
  • Activate the environment

conda activate py36
  • exit environment

conda deactivate
  • Downloading from the Tsinghua mirror source (for example, downloading numpy) will significantly increase the download speed

pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
  • Add a single image

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/conda config --set show_channel_urls yes
  • Delete all conda configurations

conda config --remove-key channels
  • Enter the python environment

python
  • Exit the python environment, all three commands are available

exit()
quit()
ctrl + z
  • Enter the python environment to check the version number of tf, and check the physical equipment supported by mac for training

python
import tensorflow as tf
tf.__version__
tf.config.list_physical_devices()
  • At this point, the environment is fully configured.

Tags: Python TensorFlow macOS

Posted by SocomNegotiator on Tue, 10 Jan 2023 01:01:28 +0300