How to install tensorflow under MacOS (M1 chip arm architecture)

Introduction

Because the current anconda official website only supports the X86 structure for the macos system, the MacOS based on the arm structure cannot use anconda to configure tensorflow for the time being. Although Anconda cannot run on M1, the corresponding alternative is Miniforge, which can also achieve similar effects. In addition, Apple officially said that it also supports the addition of metal plug-ins, so that the GPU part integrated in the M1 chip can be called, but currently only supports tensorflow2.5 and 2.6 versions, and the version used in this section is tensorflow2.4 python3.8, Therefore, the acceleration plugin is not installed. The official method of installing metal is as follows:

Tensorflow Plugin – Metal – Apple Developer

The following will start to explain how to install tensorflow on Mac (M1)

1. Download the Miniforge3-MacOSX-arm64.sh script file and run it

The miniforge download address is GitHub – conda-forge/miniforge: A conda-forge distribution.

The path where my Miniforge3-MacOSX-arm64.sh script file is installed is

/Users/xiewenhui/MiniforgeShell/Miniforge3-MacOSX-arm64.sh

We open the terminal, first enter the folder, enter the folder where Miniforge3-MacOSX-arm64.sh is located

cd ~xiewenhui

cd MiniforgeShell

Run script file

sh Miniforge3-MacOSX-arm64.sh

The result is shown in the figure below, and then press Enter until you need to manually enter yes or no, for the question of yes or no , always enter yes, and then press Enter until the end.

The successful operation diagram is shown below, which means that Miniforge3-MacOSX-arm64.sh has finished running, and it will be in the folder named after the user name ( My folder here is xiewenhui) to generate a Miniforge3 folder, which represents the installation file generated after the script runs (we put Miniforge3 is used as a package manager like anconda, which includes python and conda, so conda does not need to be installed separately).

2. Create a virtual environment

(1) Create a virtual environment with conda and set the python version at the same time

The following tensorflow is the name of the virtual environment I created, of course it can also be written as tf24, etc.

conda create -n virtual environment name python=version number
conda create -n tensorflow python=3.8

(2) Activate the virtual environment

conda activate virtual environment name
conda activate tensorflow

Tip: If you need to deactivate the status, just enter conda deactivate

(3) View the python location of the current environment

At this time, the python interpreter supporting tensorflow is located in the virtual environment envs, and its version is consistent with the version created in (1), so we will be in pycharm later Wait for the IDE to directly import the following address (will be mentioned later).

Understanding: miniforge3 is the package management environment created after the Miniforge script runs. miniforge3 is similar to Anconda. In Anconda, we can create multiple virtual environments, and it is the same in miniforge3 The virtual environment we created is located in the …/miniforge3/envs folder. Each folder under envs corresponds to an independent virtual environment. When we need to switch the python interpreter in the IDE, we will find the required python interpreter: …/miniforge3/envs/virtual environment name (it is a folder, the folder name is the same as your conda create -n virtual environment name python=xxx is a name)/bin/python, and then import the IDE .

3. Install tensorflow

When the virtual environment is created and the preparations are done, we need to install tensorflow-macos, which is our real purpose.

(1) Download the tensorflow installation package (supporting the arm architecture version)

The download link is Releases · apple/tensorflow_macos · GitHub. After the download is complete, find a folder to save it, and then directly extract it to the current folder.

(2) Keep tensorflow activated, enter the tensorflow_macos folder just unzipped, and manually pip install the whl file

Be careful not to enter conda deactivate to close the activation state of tensorflow, just cd to find the folder

My tensorflow_macos folder location is /Users/xiewenhui/tensorflow_macos

Find the tensorflow_macos folder:

Continue to enter the next level folder arm64:

Before manual pip installation, first look at the whl files under arm64 to prevent omissions during pip:

There are 5 whl files that need to be installed

Before pip installation, we need to modify the lib path and envs path (very important) to facilitate our next installation:

# Change to tensorflow_macos to download and decompress the folder
libs="/Users/xiewenhui/tensorflow_macos/arm64/"

# Replace with the virtual environment path of tensorflow, tensorflow is your previousCreate the name of the virtual environment, what is the name of the virtual environment, and what is the name of the folder under envs
env="/Users/xiewenhui/miniforge3/envs/tensorflow"

To manually install these files, enter the following commands in sequence:

pip install --upgrade pip wheel setuptools cached-property six

pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl"

pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl"

pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl"

pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/tensorflow_macos-0.1a1-cp38-cp38-macosx_11_0_arm64.whl"

pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/tensorflow_addons_macos-0.1a2-cp38-cp38-macosx_11_0_arm64.whl”

pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard

If a pip installation error occurs, the file path may be wrong, the lib path and envs path must be correct, and the second is that the whl file name you installed is inconsistent with the above code, and your own whl file name is the main one. The whl file names included in different versions of tensorflow_macos may be different.

If dquote appears on the command line, it is best to paste the file name displayed by ls directly, and the pip command can be copied, but pip install …”$libs/filename.whl” It is recommended that the file name .whl be copied from the whl file name displayed by ls.

(3) The above installation steps are basically completed, but note that because Miniforge3 is the same as Mini Conda, it mainly includes python and conda, and some other scientific computing libraries such as pandas, etc. need to be installed manually, as follows:

conda install -y pandas matplotlib scikit-learn jupyterlab

The above installation steps have been completed, you can test whether the installation is successful

4. Test

Enter python on the command line to open the python shell environment

Enter the following codes one by one:

import tensorflow as tf

print(tf.__version__)

When tensorflow is introduced without error and the tensorflow version is printed out, it means that tensorflow is installed successfully

5. Pycharm imports the python interpreter containing the tensorflow package

Find the python interpreter according to the path in the figure, under the envs folder, the location is ~/miniforge3/envs/your virtual environment name (mine is tensorflow)/bin/python

Pycharm test:

import tensorflow as tf
print(tf.__version__)
print(tf.config.list_physical_devices())

Supplement: In fact, you can follow the method of Apple’s official website for online pip installation, Tensorflow Plugin – Metal – Apple Developer

python -m pip install tensorflow-macos

However, due to the fact that h5py may not be installed during installation, we directly download the tensorflow_macos installation package, and then use pip to install the local file after downloading it locally. Not only is the speed fast, but there will be no h5py related errors

A small record of some problems I have:

Because the tensorflow version at the time of installation is 2.4.0-rc0, but after creating some projects later, the version inexplicably becomes 2.7.0. If you want to reinstall the previous 2.4.0-rc0, just follow the above steps to re-pip the whl file.

Leave a Reply

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