Tensorflow Installation M1 Mac fails due to termcolor build not working

Issue

I have tried to install tensorflow on my M1 Silicon (macOS 12.4) by setting up a new anaconda environment and forcing it to use arm64 builds:

 CONDA_SUBDIR=osx-arm64 conda create -n=NAME python=3.9 -c conda-forge --override-channels

and have then followed the developer instructions to use the M1 GPU:

conda install -c apple tensorflow-deps        
pip3 install tensorflow-macos  

But then I get an error for termcolor:

  Building wheel for termcolor (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [34 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/private/var/folders/4n/3tkzf7k15n91rlbqlkh8_3t40000gn/T/pip-install-vihezj4s/termcolor_36abf77c455d4254a5130d8246e91754/setup.py", line 37, in <module>
          setup(name='termcolor',
        File "/Users/.../opt/anaconda3/envs/c.../lib/python3.9/site-packages/setuptools/_distutils/core.py", line 135, in setup
          ok = dist.parse_command_line()
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/setuptools/_distutils/dist.py", line 484, in parse_command_line
          args = self._parse_command_opts(parser, args)
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/setuptools/dist.py", line 1104, in _parse_command_opts
          nargs = _Distribution._parse_command_opts(self, parser, args)
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/setuptools/_distutils/dist.py", line 540, in _parse_command_opts
          cmd_class = self.get_command_class(command)
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/setuptools/dist.py", line 951, in get_command_class
          self.cmdclass[command] = cmdclass = ep.load()
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 194, in load
          module = import_module(match.group('module'))
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/importlib/__init__.py", line 127, in import_module
          return _bootstrap._gcd_import(name[level:], package, level)
        File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
        File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
        File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
        File "<frozen importlib._bootstrap_external>", line 850, in exec_module
        File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/wheel/bdist_wheel.py", line 26, in <module>
          from .macosx_libfile import calculate_macosx_platform_tag
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/wheel/macosx_libfile.py", line 41, in <module>
          import ctypes
        File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/ctypes/__init__.py", line 8, in <module>
          from _ctypes import Union, Structure, Array
      ImportError: dlopen(/Users/nils/opt/anaconda3/envs/care-reconstruction/lib/python3.9/lib-dynload/_ctypes.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libffi.7.dylib
        Referenced from: /Users/.../opt/anaconda3/envs/.../lib/python3.9/lib-dynload/_ctypes.cpython-39-darwin.so
        Reason: tried: '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/python3.9/lib-dynload/../../libffi.7.dylib' (no such file), '/Users/nils/opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/python3.9/lib-dynload/../../libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../bin/../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../bin/../lib/libffi.7.dylib' (no such file), '/usr/local/lib/libffi.7.dylib' (no such file), '/usr/lib/libffi.7.dylib' (no such file)
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for termcolor
  Running setup.py clean for termcolor
Failed to build termcolor

Finishing the installation by adding pip3 install tensorflow-metal and then loading the module tensorflow in the python3 shell produces the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/tensorflow/__init__.py", line 37, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/Users/.../opt/anaconda3/envs/.../lib/python3.9/site-packages/tensorflow/python/__init__.py", line 24, in <module>
    import ctypes
  File "/Users/.../opt/anaconda3/envs/care-reconstruction/lib/python3.9/ctypes/__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ImportError: dlopen(/Users/.../opt/anaconda3/envs/.../lib/python3.9/lib-dynload/_ctypes.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libffi.7.dylib
  Referenced from: /Users/.../opt/anaconda3/envs/.../lib/python3.9/lib-dynload/_ctypes.cpython-39-darwin.so
  Reason: tried: '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/python3.9/lib-dynload/../../libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/python3.9/lib-dynload/.../libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/care-reconstruction/bin/../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../lib/libffi.7.dylib' (no such file), '/Users/.../opt/anaconda3/envs/.../bin/../lib/libffi.7.dylib' (no such file), '/usr/local/lib/libffi.7.dylib' (no such file), '/usr/lib/libffi.7.dylib' (no such file)

Running pip3 uninstall termcolor and pip3 install termcolor produces the same installation error.

Am I just lost here and there is no functioning M1 Silicon built for termcolor at the moment? Or did I mess up somewhere?

Solution

Ok got it. Somehow the issue was with pip:

conda uninstall termcolor
conda install termcolor

solved all issues and tensorflow appears to be working (finger’s crossed!)

Note that tensorflow-macos can only be installed via pip at the moment, so I first need to let the pip built of termcolor fail, remove it with conda and then re-install it.

Answered By – Narusan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published