How To Install Tensorflow on Mac Tutorial From Scratch
In this tutorial, we will see How To Install Tensorflow on Mac. For this tutorial, you must have installed Python 3 in your mac machine. If not then go to python.org website and install version 3 of Python. Other then that, you need to have an editor like Visual Studio Code. You can always welcome with PyCharm IDE as well. I am using VSCode and have installed Python extension.
How To Install Tensorflow on Mac
I have installed version 3 of the Python. You can check it by going to the terminal and see the output by python3 -v command.
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 .bash_profile or .zshrc file.
The hit the one by one following command.
brew update brew install python # Python 3 sudo pip3 install -U virtualenv
Create a virtual environment
I have already created a Virtual Environment, but you can also create by following steps.
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 virtual environment perfectly then virtualenv is active, 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 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.
You can see that, I have already installed the TensorFlow package.
Install the TensorFlow pip package
If your system has not tensorflow package then you can install it using the following command.
pip install tensorflow
Basically, package dependencies are already installed, if you have not the newer version of tensorflow then you can upgrade using the following command.
pip install --upgrade tensorflow
So, finally, the Tensorflow is installed on your machine.
Let’s check if it has 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))
In the above code, we have imported the tensorflow as well as the os module.
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.
If you have seen like this then congratulations!! You have successfully installed Tensorflow on your mac.
Finally, How To Install Tensorflow on Mac Tutorial From Scratch is over.