Install Homebrew (if not already installed)
- Homebrew is a popular package manager for macOS. Open Terminal and execute the following command to install it:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Update Homebrew and Install Python
- Ensure your Homebrew and installed packages are up-to-date, then install Python:
brew update
brew upgrade
brew install python
Set Up a Virtual Environment
- Create a virtual environment to manage your TensorFlow project's dependencies independently of other Python projects:
python3 -m venv tf_env
source tf_env/bin/activate
Install TensorFlow with pip
- TensorFlow can be installed directly from Python's package manager, pip:
pip install --upgrade pip
pip install tensorflow
Verify the Installation
- Run a Python script to verify that TensorFlow is installed correctly and operating:
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
Enable Metal API for GPU Acceleration
- For better performance, especially on Apple Silicon, ensure the Metal API for TensorFlow is enabled. This can be typically included in your main script:
tf.config.experimental.set_memory_growth(tf.config.list_physical_devices('GPU')[0], True)
Install Jupyter Notebook (Optional)
- Jupyter Notebook is a great tool for interactive experimentation:
pip install jupyterlab
Test Jupyter Notebook
- Run Jupyter Notebook within your active virtual environment:
jupyter notebook
Advanced Configuration and Troubleshooting
- For Apple Silicon users, ensure native TensorFlow is being used:
pip install tensorflow-macos
pip install tensorflow-metal
- If encountering issues with package versions or dependencies, consider pinning specific versions of TensorFlow and other libraries.