Here are the steps to install TensorFlow on Mac.
Step 1: Install the packages
First, Install using the Homebrew package manager.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then add a global path. Modify and add the following line inside the .bash_profile or .zshrc file.
Hit the one-by-one, following the command.
brew update brew install python # Python 3 sudo pip3 install -U virtualenv
Step 2: Create a virtual environment
Create a new virtual environment by choosing a Python interpreter and making a ./pythonenv directory to hold it. Type the following command, then.
virtualenv --system-site-packages -p python3 ./pythonenv
Go inside that folder.
Activate the virtual environment using a shell-specific command.
If you have installed the virtual environment perfectly, virtualenv is active, and your shell prompt is prefixed with (pythonenv). See the below image.
Install packages within a virtual environment without affecting the host system setup. Start by upgrading the pip. See the following command to upgrade pip.
pip install --upgrade pip
After that, you can see all the packages by typing the following command.
Step 3: Install the TensorFlow pip package
If your system has not tensorflow package, then you can install it using the following command.
pip install tensorflow
Package dependencies are already installed. If you do not have the newer tensorflow version, you can upgrade using the following command.
pip install --upgrade tensorflow
Tensorflow is installed on your machine.
Let’s check if it has been installed or not.
Create a new folder inside the pythonenv folder called tflow, and inside that, create a new file called tflow.py and add the following code inside it.
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' hello = tf.constant('Kudos, TensorFlow!') sess = tf.Session() print(sess.run(hello))
We have imported the tensorflow and the os module in the above code.
If you don’t import the os module and use the environ function, then it will give us a warning and avoid the warning, we have applied the setting export TF_CPP_MIN_LOG_LEVEL=2.
For more information, you can check out this StackOverflow link.
Finally, run the tflow.py file using the following command and see the output.