tensorflow: Cannot run TensorFlow 2.7 in Docker on M1 (Apple Silicon)

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS 12.0 Monterey
  • TensorFlow installed from (source or binary): pre-built binary (Docker and pip)
  • TensorFlow version (use command below): 2.7
  • Python version: 3.8.10

Describe the current behavior

Our team needs to run TensorFlow as part of a larger application in Docker. However, this doesn’t seem possible on an M1 Mac.

For example, if I use the default TF Docker image (for x86-64 only, an ARM64 image is not available):

> docker run -it tensorflow/tensorflow /bin/bash
> python -c "import tensorflow"
The TensorFlow library was compiled to use AVX instructions, but these aren't available on your machine.
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted

I get the same error when installing from pip on an x86-64 Linux container:

> docker run -it --platform=linux/amd64 python:3.8-buster /bin/bash
> pip install --upgrade pip && pip install tensorflow
> python -c "import tensorflow"
The TensorFlow library was compiled to use AVX instructions, but these aren't available on your machine.
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted

Ostensibly, this is because the pre-built TensorFlow requires the CPU to support AVX instructions, but this is not supported by Docker / QEMU when emulating an x86-64 container on M1.

Describe the expected behavior

There should be a way to run TensorFlow in Docker on M1! (Without building from source.)

Every other ML/DS library works with on Docker on M1: PyTorch, Scikit-Learn, Numpy, Scipy, etc.

Standalone code to reproduce the issue

See code snippets above.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 22
  • Comments: 18 (5 by maintainers)

Most upvoted comments

Got it – personally I’d like to avoid building from source since it complicates our dependency management.

Most other ML libraries work fine out-of-the-box in x86 emulation on M1: PyTorch, Scikit-Learn, Numpy, Scipy, etc. It would be great if TF supported it as well.

Are you satisfied with the resolution of your issue? Yes No

I can’t believe this issue still exists in 2024. I tried installing TensorFlow with Anaconda, Docker, and specific TensorFlow versions, but none worked. Finally, this is how I solved it:

Step 1: Install Miniforge

Miniforge is optimized for Apple Silicon.

curl -L -O https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh
bash Miniforge3-MacOSX-arm64.sh
source ~/miniforge3/bin/activate

Step 2: Create a New Environment and Install TensorFlow

1. Create and activate a new Conda environment:

conda create -n tensorflow_env python=3.10 -y
conda activate tensorflow_env

2. Install TensorFlow:

conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal

Step 3: Test the Installation

Run the following script to verify:

import tensorflow as tf
print("TensorFlow Version:", tf.__version__)
print("Is GPU available:", tf.config.list_physical_devices('GPU'))