How to Install Tensorflow on Mac

Here are the steps to install TensorFlow on Mac.

Step 1: Install the packages

If you don’t have still Python3 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 the .bash_profile or .zshrc file.

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

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.

cd pythonenv

Activate the virtual environment using a shell-specific command.

source bin/activate

If you have installed the virtual environment perfectly, virtualenv is active, and your shell prompt is prefixed with (pythonenv). See the below image.

 

Install Tensorflow on Mac

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.

pip list

Create a virtual environment

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.

python3 tflow.py

 

Install the TensorFlow pip package

That’s it!

1 thought on “How to Install Tensorflow on Mac”

  1. 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

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.