So you want to install TensorFlow-text in Python on that new M1/M2 Laptop
It’s much more challenging than it sounds!
I was working on a project to embed documents in different languages in the same space. For this, I wanted to use Language-Agnostic BERT Sentence Embeddings (LaBSE), which depends on TensorFlow, TensorFlow Hub, and TensorFlow-text.
Now it’s no secret that Apple’s support for (Google’s) TensorFlow library has seemed… begrudging for a while now. I got it working for TensorFlow 2.9 on an M2, here’s how I did it.
First install xcode command line tools:
xcode-select --install
Then install miniconda from here https://docs.conda.io/en/latest/miniconda.html or using brew:
brew install miniconda
Use conda to create and activate a new environment for this, which will use Python 3.9 and TF 2.9 (the -y just means automatically say “yes” to prompts):
conda create --name tft_m2 python=3.9 -y
conda activate tft_m2
conda install -c apple tensorflow-deps=2.9 -y
Now install tensorflow for M1/M2. NOTE: normally people will tell you to install Tensorflow-metal to take advantage of GPU processing, but that caused errors with the LaBSE model so I’m excluding it.
We have to use pip for this:
pip install tensorflow-macos==2.9.2
Now let’s check that tensorflow was really installed:
python -c "import tensorflow; print(tensorflow.__version__)"
Now the tricky part. Download tensorflow_text-2.9.0-cp39-cp39-macosx_11_0_arm64.whl from this github repo: https://github.com/sun1638650145/Libraries-and-Extensions-for-TensorFlow-for-Apple-Silicon/releases. Make sure you download the version for python 3.9 (cp39) and TensorFlow 2.9 (-2.9.0). Or match whatever version of TF you’re installing!
Then install with pip (assuming you downloaded to the downloads folder):
pip install ~/Downloads/tensorflow_text-2.9.0-cp39-cp39-macosx_11_0_arm64.whl
And violla! You should be all set!