Introduction
In this article, I can show you how to install TensorFlow on your M1/M2 macbook. To install TensorFlow, you can follow the step-by-step instruction below. If you already installed xcode and/or homebrew, skip step 1 and/or step 2 below.
Step 1: Install Xcode
Before you install TensorFlow, you need to install well-known compiler xcode first. To install xcode, you need to open Terminal App and type
>> xcode-select –install
Click Install in the popup window. Review the terms and conditions and click Agree if you do.
Step 2: Install Homebrew
Next step is to install software packaging management homebrew. To install homebrew, type
>> /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once installed, you need to add path by typing
>> echo "export Path=/usr/local/bin:$PATH" >> /Users/your user name/.zprofile
>> echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/ your user name/.zprofile
>> eval "$(/opt/homebrew/bin/brew shellenv)"
Note that you need to use your user name for your mac in the place of “your user name” above.
You can check if homebrew is successfully installed by typing
>> brew -v
If you see the homebrew version in your terminal, it means it is installed successfully.
Step 3: Install Miniconda
Miniconda is a python package management system, which is the minimal set of features from the extensive Anaconda Python distribution including various key data science related packages. To install Miniconda, type
>> brew install --cask miniconda
Step 4: Modify .bash_profile File
To initialize Miniconda, you need to modify .bash_profile file. If you previously installed Miniforge, you need to remove the following (everything between and including the following) from .bash_profile. If you did not install Miniforge before, you can skip this step, i.e., directly run >> conda init
. Note that you can open .bash_profile using text editor, e.g., by typing, >> nano .bash_profile
.
Then, you need to remove the following from .bash_profile.
# >>> conda initialize >>>
...
# <<< conda initialize <<<
and then run
>> conda init
Now, type
source .bash_profile
Step 5: Create Virtual Environment
To create a virtual environment, type
>> conda create -n env_tf python=3.10
Note that env_tf is the virtual environment name, but you can choose your own. In this example, I used Python version 3.10, but you can use different version as you wish by changing 3.10 with different version, e.g., 3.9, etc.
After the environment is created, type the following to activate the virtual environment.
>> conda activate env_tf