In this tutorial, you will learn to install TensorFlow 2.0 on your macOS system running either Catalina or Mojave
There are a number of important updates in TensorFlow 2.0, including eager execution, automatic differentiation, and better multi-GPU/distributed training support, but the most important update is that Keras is now the official high-level deep learning API for TensorFlow.
Furthermore, if you own a copy of my book, Deep Learning for Computer Vision with Python, you should use this guide to properly install TensorFlow 2.0 on your macOS system.
Inside this tutorial, you’ll learn how to install TensorFlow 2.0 on macOS (using either Catalina or Mojave).
Alternatively, click here for my Ubuntu + TensorFlow 2.0 installation instructions.
To learn how to install TensorFlow 2.0 on macOS, just keep reading.
How to install TensorFlow 2.0 on macOS
In the first part of this tutorial, we’ll briefly discuss the pre-configured deep learning development environments that are a part of my book, Deep Learning for Computer Vision with Python.
We’ll then configure and install TensorFlow 2.0 on our macOS system.
Let’s begin.
Pre-configured deep learning environments
When it comes to working with deep learning and Python I highly recommend that you use a Unix-based environment.
Deep learning tools can be more easily configured and installed on Unix systems, allowing you to develop and run neural networks quickly.
Of course, configuring your own deep learning + Python + Linux development environment can be quite the tedious task, especially if you are new to Unix, a beginner at working the command line/terminal, or a novice when compiling and installing packages by hand.
In order to help you jump start your deep learning + Python education, I have created two pre-configured environments:
- Pre-configured VirtualBox Ubuntu Virtual Machine (VM) with all necessary deep learning libraries you need to be successful (including Keras, TensorFlow, scikit-learn, scikit-image, OpenCV, and others) pre-configured and pre-installed.
- Pre-configured Deep Learning Amazon Machine Image (AMI) which runs on Amazon Web Service’s (AWS) Elastic Compute (EC2) infrastructure. This environment is free for anyone on the internet to use regardless of whether you are a DL4CV customer of mine or not (cloud/GPU fees apply). Deep learning libraries are pre-installed including both those listed in #1 in addition to TFOD API, Mask R-CNN, RetinaNet, and mxnet.
I strongly urge you to consider using my pre-configured environments if you are working through my books. Using a pre-configured environment is not cheating — they simply allow you to focus on learning rather than the job of a system administrator.
If you are more familiar with Microsoft Azure’s infrastructure, be sure to check out their Ubuntu Data Science Virtual Machine (DSVM), including my review of the environment. The Azure team maintains a great environment for you and I cannot speak highly enough about the support they provided while I ensured that all of my deep learning chapters ran successfully on their system.
That said, pre-configured environments are not for everyone.
In the remainder of this tutorial, we will serve as the “deep learning systems administrators” installing TensorFlow 2.0 on our bare metal macOS machine.
Configuring your macOS TensorFlow 2.0 deep learning system
The following instructions for installing TensorFlow 2.0 on your machine assume either:
- You have administrative access to your system
- You can open a terminal and or you have an active SSH connection to the target machine
- You know how to operate the command line.
Let’s get started!
Step #1: Choose your macOS deep learning platform — either Catalina or Mojave
These TensorFlow 2.0 install instructions in this guide are compatible with the following operating systems:
- macOS: 10.15 “Catalina”
- macOS: 10.14 “Mojave”
Alternatively, you may follow my Ubuntu + TensorFlow 2.0 installation instructions.
Note: You may be wondering “What about Windows?” Keep in mind I do not support Windows on the PyImageSearch blog. You can read more about my “no Windows policy” in my FAQ.
Step #2 (Catalina only): Choose Bash or ZSH as your shell
This step is for macOS Catalina only. If you use Mojave, please ignore this step and skip to Step #3.
On macOS Catalina, you have the option of using a Bash shell or ZSH shell.
Bash is probably what you’re used to. Previous versions of macOS have used Bash and by default, Ubuntu uses Bash as well. Apple has now made a decision that going forward, their operating systems will use ZSH.
So what’s the big deal and should I switch?
Describing the differences between Bash and ZSH is outside the scope of this post — it’s entirely up to you to decide. I would recommend reading this ZSH vs. Bash tutorial to get you started, but again, the choice is up to you.
On one hand, you may discover that the changes do not impact you much and that using ZSH will be very comparable to Bash. Power users, on the other hand, may notice additional features.
If you upgraded from High Sierra or Mojave to Catalina, by default your system will likely use Bash unless you explicitly change it by going into your terminal profile settings.
If your computer came with Catalina installed or you installed Catalina from scratch, then your system will likely use ZSH by default.
Either way, if you decide to need make the switch on Catalina, you can follow these instructions to set your profile to use ZSH.
Changing your shell is relatively simple via your “Terminal Preferences” > “Profiles” > “Shell” menu as shown in Figure 4:
The shell you use determines which terminal profile you edit later in this install guide:
- ZSH:
~/.zshrc
- Bash:
~/.bash_profile
ZSH will accommodate ~/.bash_profile
if you source it from within ~/.zshrc
. Just keep in mind that not all settings will work. For example, in my ~/.bash_profile
, I had a custom bash prompt which showed only the current lowest level working directory (shorter) and also which branch of a Git repository I’m working on (useful for software development). The problem is that ZSH didn’t like my custom Bash prompt, so I had to remove it. I’ll have to set up a custom ZSH prompt in the future.
Realistically, it is probably better if you copy the settings from your Bash Profile that you need into your ZSH profile and ensure that they work. Alternatively, you can source your Bash Profile within your ZSH Profile (i.e. insert source ~/.bash_profile
as a line into your ~/.zshrc
file — then open a fresh shell or reload it with the source ~/.zshrc
command).
If you were previously working in Bash (i.e. you upgraded to Catalina), you may encounter the following message in your terminal:
The default interactive shell is now zsh. To update your account to use zsh, please run chsh -s /bin/zsh For more details, please visit https://support.apple.com/kb/HT208050.
This means that to switch shells, you should enter the command at the prompt:
$ chsh -s /bin/zsh
Note that the ZSH prompt is %
. The remainder of this tutorial will show $
at the beginning of the prompt, but you can think of it as %
if you are using ZSH.
Step #3: Install macOS deep learning dependencies
In either version of macOS, go ahead and open your macOS App Store and find and download/install Xcode (Figure 5).
From there, accept the Xcode license in the terminal:
$ sudo xcodebuild -license
Press the space
key as you read the agreement. Then type agree
at the prompt.
And then install Xcode select:
$ sudo xcode-select --install
Note: If you encounter this error message xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
you may need to follow these SOF instructions.
The unofficial, community-driven package manager for macOS is called Homebrew (brew for short). Many packages that you could install with Aptitude (apt) on Ubuntu are available via HomeBrew on macOS.
We’ll use Homebrew to install a handful of dependencies. It does not come pre-installed on macOS, so let’s install it now (only do this if you don’t already have Homebrew):
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
From there, update Homebrew:
$ brew update
Now go ahead and edit your ZSH Profile or Bash Profile. Be sure that you update the correct file depending on whether you use ZSH or Bash on macOS:
~/.zshrc
(ZSH)~/.bash_profile
(Bash)
$ nano ~/.zshrc # ZSH $ nano ~/.bash_profile # Bash
Again, you should only be editing one of the above files based on which shell your macOS system is using.
From there, insert the following lines at the end of your profile:
# Homebrew export PATH=/usr/local/bin:$PATH
Save the file (ctrl + x
, y
, enter
) and exit to your terminal.
Now go ahead and source the profile (i.e. reload it):
$ source ~/.zshrc # ZSH $ source ~/.bash_profile # Bash
Once again, only one of the above commands should be executed depending on which macOS shell you are using.
We’re now ready to install Python 3:
$ brew install python3
Let’s check that our Python 3 is properly linked at this point:
$ which python3 /usr/local/bin/python3
Warning: In our experience, Python 3.8 is not compatible with TensorFlow 2.0. We suggest installing TensorFlow 2.2+ in Step #5 if your system has Python 3.8 installed. Otherwise, sticking with Python 3.6 or 3.7 is a safe bet. Be sure to check your Python version by executing python3 --version
.
You should verify that the output path begins with /usr/local
. If it does not, then double-check that Homebrew’s Python is installed. We do not want to use the system Python as our virtual environments will be based on Homebrew’s Python.
At this point, Homebrew and Python are ready for us to install dependencies:
$ brew install cmake pkg-config wget $ brew install jpeg libpng libtiff openexr $ brew install eigen tbb hdf5
Our dependencies include compiler tools, image I/O, optimization tools, and HDF5 for working with large datasets/serialized files.
Great job installing dependencies on macOS — you can now proceed to Step #4.
Warning: In this section we worked with Homebrew. Never execute a brew
command while you are in a virtual environment described by Step #4 because it can be difficult to recover your system’s package and dependency tree from there. If you see that your bash/ZSH prompt begins with ({env_name})
then you are in a Python virtual environment and you should execute deactivate before running a brew
command.
Step #4: Install pip and virtual environments
In this step, we will set up pip and Python virtual environments.
We will use the de-facto Python package manager, pip.
Note: While you are welcome to opt for Anaconda (or alternatives), I’ve still found pip to be more ubiquitous in the community. Feel free to use Anaconda if you so wish, just understand that I cannot provide support for it.
Let’s download and install pip:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python3 get-pip.py
To complement pip, I recommend using both virtualenv and virtualenvwrapper to manage virtual environments.
Virtual environments are a best practice when it comes to Python development. They allow you to test different versions of Python libraries in sequestered development and production environments. I use them daily and you should too for all Python development.
In other words, do not install TensorFlow 2.0 and associated Python packages directly to your system environment. It will only cause problems later.
Let’s install my preferred virtual environment tools now:
$ pip3 install virtualenv virtualenvwrapper
Note: Your system may require that you use the sudo
command to install the above virtual environment tools. This will only be required once — from here forward, do not use sudo
.
From here, we need to update our bash profile:
$ nano ~/.zshrc # ZSH $ nano ~/.bash_profile # Bash
Only edit one of the files above based on which shell your macOS system uses.
Next, enter the following lines at the bottom of the file:
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 source /usr/local/bin/virtualenvwrapper.sh
Save the file (ctrl + x
, y
, enter
) and exit to your terminal.
Don’t forget to source the changes in your profile:
$ source ~/.zshrc # ZSH $ source ~/.bash_profile # Bash
You only need to execute one of the commands above — just verify which shell you are using first (either ZSH or Bash).
Output will be displayed in your terminal indicating that virtualenvwrapper
is installed.
Note: If you encounter errors here, you need to address them before moving on. Usually, errors at this point are due to typos in your Bash/ZSH profile.
Now we’re ready to create your Python 3 deep learning virtual environment named dl4cv
:
$ mkvirtualenv dl4cv -p python3
You can create similar virtual environments with different names (and packages therein) as needed. On my personal system, I have many virtual environments. For developing and testing software for my book, Deep Learning for Computer Vision with Python, I like to name (or precede the name of) the environment with dl4cv
. That said, feel free to use the nomenclature that makes the most sense to you.
Great job setting up virtual environments on your system!
Step #5: Install TensorFlow 2.0 into your dl4cv
virtual environment on macOS
In this step, we’ll install TensorFlow 2.0.
A prerequisite of TensorFlow is NumPy for numerical processing. Go ahead and install NumPy and TensorFlow 2.0 using pip:
$ pip install numpy $ pip install tensorflow==2.0.0
Great job installing TensorFlow 2.0!
Step #6: Install associated packages into your dl4cv
virtual environment
In this step, we will install additional packages needed for common deep learning development.
We begin with standard image processing libraries including OpenCV:
$ pip install opencv-contrib-python $ pip install scikit-image $ pip install pillow $ pip install imutils
These image processing libraries will allow us to perform image I/O, various preprocessing techniques, as well as graphical display.
From there, let’s install machine learning libraries and support libraries including scikit-learn and matplotlib:
$ pip install scikit-learn $ pip install matplotlib $ pip install progressbar2 $ pip install beautifulsoup4 $ pip install pandas
Great job installing associated image processing and machine learning libraries.
Step #7: Test your install
In this step, as a quick sanity test, we’ll test our install.
Fire up a Python shell in your dl4cv
environment (or whatever you named your Python virtual environment) and ensure that you can import the following packages:
$ workon dl4cv $ python >>> import tensorflow as tf >>> tf.__version__ 2.0.0 >>> import tensorflow.keras >>> import cv2 >>> cv2.__version__ 4.1.2
Note: If you are on Python 3.7.3 (Catalina) and Clang 11.0 you could encounter this error. This is a known issue with a solution is available here.
Accessing your TensorFlow 2.0 virtual environment
At this point, your TensorFlow 2.0 dl4cv
environment is ready to go. Whenever you would like to execute TensorFlow 2.0 code (such as from my deep learning book), be sure to use the workon
command to drop into the Python virtual environment where TensorFlow 2.0 is installed:
$ workon dl4cv
Your ZSH or Bash prompt will be preceded with (dl4cv)
indicating that you are “inside” the TensorFlow 2.0 virtual environment.
If you need to get back to your system-level environment, you can deactivate the current virtual environment:
$ deactivate
Frequently Asked Questions (FAQ)
Q: These instructions seem really complicated. Do you have a pre-configured environment?
A: Yes, the instructions can be daunting. I recommend brushing up on your Unix command line skills prior to following these instructions.
That said, I do offer two pre-configured environments for my book:
- Pre-configured Deep Learning Virtual Machine: My VirtualBox VM is included with your purchase of my deep learning book. Just download the VirtualBox and import the VM into VirtualBox. From there, boot it up and you’ll be running example code in a matter of minutes.
- Amazon Machine Image (EC2 AMI): Free for everyone on the internet. You can use this environment with no strings attached even if you don’t own my deep learning book (AWS charges apply, of course). Again, compute resources on AWS are not free — you will need to pay for cloud/GPU fees but not the AMI itself. Arguably, working on a deep learning rig in the cloud is cheaper and less time-consuming than keeping a deep learning box on-site. Free hardware upgrades, no system admin headaches, no calls to hardware vendors about warranty policies, no power bills, pay only for what you use. This is the best option if you have a few one-off projects and don’t want to invest in hardware.
Q: Why didn’t we install Keras?
A: Keras is officially part of TensorFlow as of TensorFlow v1.10.0. By installing TensorFlow 2.0 the Keras API is inherently installed.
Keras has been deeply embedded into TensorFlow and tf.keras
is the primary high-level API in TensorFlow 2.0. The legacy functions that come with TensorFlow play nicely with tf.keras
now.
In order to understand the difference between Keras and tf.keras
in a more detailed manner, check out my recent blog post.
You may now import Keras using the following statement in your Python programs:
$ workon dl4cv $ python >>> import tensorflow.keras >>>
Q: Do these instructions support macOS Mojave and macOS Catalina?
A: Yes, these instructions have been fully tested on Mojave and Catalina. That said, Homebrew changes often and can be the source of problems. If you are having an issue, please contact me or drop a comment below. Please be respectful of this webpage and my email inbox by not dumping large amounts of terminal output. Keep in mind this PyImageSearch policy on debugging development environments as well.
Q: I’m really stuck. Something is not working. Can you help me?
A: I really love helping readers and I would love to help you configure your deep learning development environment.
That said, I receive 100+ emails and blog post comments per day — I simply don’t have the time to get to them all
Customers of mine receive support priority over non-customers due to the number of requests myself and my team receive. Please consider becoming a customer by browsing my library of books and courses.
My personal recommend is that you to grab a copy of Deep Learning for Computer Vision with Python — that book includes access to my pre-configured deep learning development environments that have TensorFlow, Keras, OpenCV, etc. pre-installed. You’ll be up and running in a matter of minutes.
What's next? I recommend PyImageSearch University.
30+ total classes • 39h 44m video • Last updated: 12/2021
★★★★★ 4.84 (128 Ratings) • 3,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 30+ courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 30+ Certificates of Completion
- ✓ 39h 44m on-demand video
- ✓ Brand new courses released every month, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 500+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
In this tutorial, you learned how to installed TensorFlow 2.0 on macOS (either on Catalina or Mojave).
Now that your deep learning rig is configured, I would suggest picking up a copy of Deep Learning for Computer Vision with Python. You’ll be getting a great education and you’ll learn how to successfully apply Deep Learning to your own projects.
To be notified when future tutorials are published here on PyImageSearch, just enter your email address in the form below!
Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF
Enter your email address below to join the PyImageSearch Newsletter and download my FREE 17-page Resource Guide PDF on Computer Vision, OpenCV, and Deep Learning.