In this tutorial, we will see How To Install Tensorflow on Mac. For this tutorial, you must have installed Python 3 on your mac machine. If not, go to the python.org website and install version 3 of Python.
Other than 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 the 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.
If you don’t have still Python 3 and other packages like Otherwise, install Python, the pip package manager, and Virtualenv then you can follow the below steps.
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.
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
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.
cd pythonenv
Activate the virtual environment using a shell-specific command.
source bin/activate
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.
pip list
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.
python3 tflow.py
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.
Getting error after running python3 tflow.py
[libprotobuf ERROR google/protobuf/descriptor_database.cc:394] Invalid file descriptor data passed to EncodedDescriptorDatabase::Add().
[libprotobuf FATAL google/protobuf/descriptor.cc:1356] CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size):
libc++abi.dylib: terminating with uncaught exception of type google::protobuf::FatalException: CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size):
Abort trap: 6