How to install TensorFlow in Jupyter Notebook

TensorFlow is one of the most popular machine learning frameworks. Its popularity can be attributed to its ease of use and its large community worldwide.

Like most machine learning frameworks, TensorFlow code is usually written inside Jupyter Notebooks using the Python language.

Jupyter.org defines a Jupyter Notebook as the original web application for creating and sharing computational documents. It offers a simple, streamlined, document-centric experience. Code in Jupyter Notebooks is written in cells which are usually executed sequentially.

Before one can begin writing TensorFlow code inside a Jupyter Notebook, the TensorFlow library must be installed first in the development environment.

Developers usually leverage the power of virtual environments for developing machine learning tools because virtual environments are stable, reproducible, and portable, which saves a lot of time in the future.

In this answer, we'll look at how TensorFlow can be installed in one of those environments, the Anaconda environment. Anaconda is a program for managing Python packages and environments and contains the most common data science packages.

Step 1: Install Anaconda

  1. Go to https://www.anaconda.com/download and choose the appropriate version for your operating system. Download the setup and follow the installation instructions.

  2. After the installation, open the terminal and type the command below to confirm which version of Anaconda is installed:

conda --version
Check the Anaconda version
nuruddeen@nuruddeen-Latitude-E7440:~$ conda --version
conda 22.11.0
nuruddeen@nuruddeen-Latitude-E7440:~$

Step 2: Create a virtual environment

Next, we will create a virtual environment named -tensorflow_env using the command below:

conda create -n tensorflow_env
Creating a virtual environment

Activate the virtual environment:

conda activate tensorflow_env
Activating a virtual environment
Creating and activating a virtual environment
Creating and activating a virtual environment

Step 3: Installing packages

Inside the environment, we’ll install the Jupyter Notebook and TensorFlow packages using the commands below:

conda install -c anaconda jupyter
conda install -c conda-forge tensorflow
Install Jupyter Notebook and TensorFlow

If the packages are already installed in our environment, nothing will be installed. Otherwise, they will be downloaded and installed.

To get the list of all packages installed in our environment, use the conda list command:

conda list
List of packages installed in the current virtual environment

Step 4: Using TensorFlow

Finally, after installing all the necessary packages, we launch our Jupyter Notebook from the terminal using the jupyter notebook command:

jupyter notebook
Launching Jupyter Notebook
Starting the Jupyter server
Starting the Jupyter server

A browser window should automatically open launching the Jupyter server. If the browser did not start automatically, open a new browser window and type http://localhost:8888 to launch the server.

To create a new notebook, select "Python3 (ipykernel)."

Jupyter Notebook environment
Jupyter Notebook environment

We can start using TensorFlow by importing it from inside our Jupyter Notebook with the following command:

import tensorflow as tf
Import TensorFlow inside Jupyter Notebook
View inside Jupyter Notebook
View inside Jupyter Notebook

Run the cell and TensorFlow will now be imported and is ready for use inside our notebook.

We have successfully installed TensorFlow in our environment and imported it into our notebook. We can start building our machine learning models.

Free Resources