How To Install Tensorflow
Learn how to install TensorFlow and start building machine learning models. This guide covers installation steps for various processors.
In our data-driven world, machine learning has become the backbone of innovation across industries. From smartphones that recognize your face to medical algorithms that help doctors detect disease, the applications are as vast as they are transformative.
TensorFlow, an open-source library developed by Google, is a pivotal tool for uses like this. It empowers developers and researchers to build and deploy machine-learning models with unprecedented ease and efficiency.
This article will guide you through the process of installing TensorFlow, opening the door to a world of possibilities in artificial intelligence and data science. Whether you're a seasoned developer looking to expand your toolkit or a curious beginner taking your first steps into machine learning, mastering TensorFlow installation is your gateway to creating cutting-edge AI solutions.
Table of contents:
- TensorFlow installation requirements
- Installing TensorFlow on Windows
- Installing TensorFlow on Linux
- Installing TensorFlow on Mac
- How to install TensorFlow with Anaconda
- Installing TensorFlow in a virtual environment
- Using Docker to install TensorFlow
- Verifying your installation
- Updating TensorFlow
- Troubleshooting common installation issues
Tensorflow installation requirements
To install Tensorflow, you’ll need to make sure your system meets the minimum requirements, including the required Python version. Verify that your system meets the following requirements, and install pip if necessary:
- Python 3.9–3.12 (64-bit)
- pip 19.0 or higher (20.3 or higher for macOS)
- Windows 7 or later, Ubuntu 16.04 or later, or macOS 10.12.6 (Sierra) or later
- For GPU support (Linux only):some text
- CUDA Toolkit 11.8
- cuDNN SDK 8.6 or higher
- NVIDIA GPU drivers version 450.80.02 or higher
Verifying existing installations and dependencies
Before proceeding with a new installation or upgrade, check your current setup. Enter the following commands in your terminal or command prompt to do each of the tasks below:
If necessary, install Python. Otherwise, check your Python version:
--CODE language-markup line-numbers--
python3 --version
Verify pip version:
--CODE language-markup--
pip --version
Check TensorFlow version (if already installed):
--CODE language-markup--
pip show tensorflow
Or in a Python environment:
--CODE language-markup--
import tensorflow as tf
print(tf.__version__)
List all installed packages (confirming these can help you check for potential conflicts and verify dependencies related to package versions):
--CODE language-markup--
pip list
Check for available GPUs (in Python):
--CODE language-markup--
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
Verify CUDA version:
--CODE language-markup--
nvcc --version
This command checks for cuDNN (so you can verify the version) in the default installation location when installed manually or via NVIDIA's official packages:
--CODE language-markup line-numbers--
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
This command checks an alternative location where cuDNN headers might be installed, especially when using distribution-specific packages.
--CODE language-markup line-numbers--
cat /usr/include/cudnn.h | grep CUDNN_MAJOR -A 2
Remember to create and use a virtual environment for your TensorFlow projects to avoid conflicts with other Python packages.
Once you have verified that you meet the system requirements, you’re ready to begin the setup procedure. We’ll walk you through how this on the major operating systems.
Installing TensorFlow on Windows
If you have a Windows operating system, you’ll want to follow these steps to properly install TensorFlow.
Windows
1. Install Microsoft Visual Studio. You’ll want to begin by installing Microsoft Visual Studio (not the Visual Studio Code). In versions starting in 2019, MS Visual Studio comes with the Microsoft Visual C++ Redistributable package, which is required to run TensorFlow.
As you let the executable file run, it will ask you what workloads you’ll want to install. A workload is an integrated development environment where you can develop software, but you don’t need any software for this project, so you can install it without a workload.
2. Install NVIDIA CUDA toolkit. This may already be installed on your system, depending on how your Windows is set up. If you already have it installed, then verify that it’s compatible with your desired version of TensorFlow. You can check which version you have by going to “Apps & Features” under your Settings.
For TensorFlow 2.16, you need to have NVIDIA CUDA Toolkit 11.2. If you do not have NVIDIA or have an older version and need to update it, visit the NVIDIA website to get the correct toolkit. Then, install the drivers using the default parameters.
3. Install cuDNN. You also need to have cuDNN 8.1 on your system for TensorFlow 2.16.1 You can also get this on the NVIDIA website. If you are not a member, you may have to create an account to complete this download.
4. Extract the ZIP folder and copy the core directories. Now, you want to extract the ZIP folder you have downloaded. You will want to open the CUDA folder, copy the top three folders (bin, include, and lib), and paste them in C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2.
You’ll be asked if you want to replace the files in the destination, and the answer is yes. This will have your system fully prepared.
5. Add your CUDA toolkit to PATH. In the same program file you opened above, copy the bin folder. Then, copy the file path. Go to the “Environment Variables” under the Windows Start button and choose the “Path.”
Say you want to edit the path and paste the copied file path. Go to the GPU toolkit folder and open the libnvvp folder. You will also want to copy its path and paste it into the Environment variables using the same process you just followed. Save all your changes.
6. Install TensorFlow with Jupyter Lab, from Jupyter Notebook, in a virtual environment. Create your virtual environment, such as a conda environment. Make sure that TensorFlow and JupyterLab are installed with pip or conda.
--CODE language-markup line-numbers--
conda create -n tf python==3.10
conda activate tf
pip install –upgrade tensorflow
pip install jupyterlab ipykernel
7. Add your virtual environment to JupyterBab, so it appears as a valid kernel. Create a new notebook to see if TF can detect your GPU.
Installing TensorFlow on Linux
To show you how to install TF on a Linux operating system, we will use Ubuntu as an example (Ubuntu is a distribution of Linux). Note that TensorFlow officially only supports Ubuntu, but these instructions may also work with other Linux distributions.
You must first verify that Python, specifically the pip package, has been installed. Remember, pip3 will allow you to access Python3 and higher. You’ll also want to make sure you have a virtual environment set up.
1. Create a virtual environment. Go to your home directory (or another directory where you can read and write permissions) to set up your Python 3.0 virtual environment.
You can enter the commands:
--CODE language-markup--
mkdir tensorflow_files
cd tenssorflow_files
Then, run the command: python3 -m venv virtualenv. This will create a directory with a copy of the Python binary, the standard Python library, the pip package manager, and other supporting files.
2. Activate your virtual environment. Type the following code into the directory prompt:
--CODE language-markup--
source virtualenv/bin/activate
This will activate your environment and add the virtual bin directory to the $PATH variable. You should now see the name of your virtual environment in your shell’s prompt.
3. Upgrade pip. Go to your pip install and upgrade pip to make sure it is the latest version.
--CODE language-markup--
pip install --upgrade pip
4. Install TensorFlow. Under pip install, select to upgrade the TensorFlow.
--CODE language-markup--
pip install --upgrade TensorFlow
5. Double-check your installation. To make sure your CPU has been properly set up, run the following code. You should see a tensor returned.
--CODE language-markup line-numbers--
python3 -c "import tensorflow as tf;
print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
Then, to verify that the GPU has been properly set up,run:
--CODE language-markup line-numbers--
python3 -c "import tensorflow as tf;
print(tf.config.list_physical_devices('GPU'))"
You should have a list of GPU devices returned.
You can also use the following lines of code to check for proper installation.
--CODE language-markup line-numbers--
import tensorflow as tf
print(tf,__version__)
2.3.0
print(tf.reduce_sum(tf.random.normal([1000, 1000])))
tf.Tensor(-505.04108, shape=(), dtype=float32)
Installing TensorFlow on Mac
To install TF on your macOS, you must do the following.
Note: If you’re running newer versions of MacOS, consider also installing the tensorflow-metal plugin to leverage Mac GPUs in training machine learning models.
1. Verify your installation of Python and Homebrew. Go to your Finder to open Applications and then the Utilities folder. You can then open Terminal.
You can enter the following command prompt to check the version of the Python package you have:
--CODE language-markup--
$ python3 --version
Then, you can type the command to see that the brew has been properly installed:
--CODE language-markup--
$ brew --version
[ABHINAVs-Air:- abhinavjain194$ python3 --version
Python 3.10.3
ABHINAVs:- abhinavjain194$
[ABHINAVs-Air:~ abhinavjain194$ brew --version
Homebrew 3.1.7
Homebrew/homebrew-core (git revision 83ee448ece; last commit 2021-05-14)
Homebrew/homebrew-cask (git revision e733258bb4; last commit 2021-05-14)
ABHINAVs-Air:~ abhinavjain194$
2. Create a virtual environment and a directory to hold it. Now, you will need to create a virtual environment for TF.
You can then enter the following command to start setting up your virtual environment:
--CODE language-markup--
$ brew install virtualenv
Your next step is to employ the following in your Terminal to create your ./pythonenv director. This will hold your virtual environment:
--CODE language-markup--
$ virtualenv --system-site-packages -p python3 ./pythonenv
3. Go into your directory folder and activate your virtual environment. To get inside your folder, submit the code:
--CODE language-markup--
$ cd ./pythonenv
Then, enter the following command to activate your virtual environment:
--CODE language-markup--
source bin/activate
4. Install TensorFlow and create a file to begin running the program. Now you have reached the point where you will install TF. The following code will allow you to do so:
--CODE language-markup--
brew install tensorflow
Once you have finished the install, create a new folder called tfdemo. Inside the folder, create a file with the name tfdemofile.py.
5. Run the file you have created. Now, run the file with:
--CODE language-markup--
$ python3 tfdemofile.py
Once you have finished these steps, you should find that the program has been fully installed.
--CODE language-markup--
# importing tensorflow
import tensorflow as tf
# creating nodes in computation graph
node1 = tf.constant(3, dtype = tf.int32)
node2 = tf.constant(5, dtype = tf.int32)
node3 = tf.add(node1, node2)
# create tensorflow session object
sess = tf.Session()
# evaluating node3 and printing the result
print("Sum of node1 and node2 is:", sess.run(node3))
# closing the session
sess.close()
How To Install TensorFlow with Anaconda
Anaconda offers an open-source distribution that you can access for Python and R. To employ Anaconda, you will need to follow the process specific to your machine.
Installing with a Windows OS
Start the process by installing Anaconda on your system. You can do so directly from their website. Once you install Anaconda, go to Windows Start to open the Anaconda PowerShell prompt.
--CODE language-markup--
base) PS C:\Users\wale obembe>
Then, you will create a TensorFlow environment. You can name this environment anything you like, although many will elect to implement either TensorFlow or TF. Enter the following command to create the environment.
--CODE language-markup--
conda create --name tensorflow python=3.10
base) PS C:\Users\wale obembe> conda create --name tensorflow python-3.10
Begin to download the Tensorflow and any dependencies.
Once you have finished the download, you need to activate your new environment.
Enter this code in Anaconda Powershell Prompt to complete the activation:
--CODE language-markup--
conda activate tensorflow
base) PS C:\Users\wale obembe> conda activate tensorflow
tensorflow) PS C:\Users\wale obembe>
With an activated environment, you now want to install TensorFlow using conda.
To get the CPU version, you can employ this code:
--CODE language-markup--
conda install --upgrade tensorflow
If you want the GPU version, use:
--CODE language-markup--
conda install --upgrade tensorflow-gpu
If you want to use PIP to install your tensor flow, type pip instead of conda in either of those lines of code.
If you have Ubuntu, Linux, or Mac OS
Your process will be similar to the process for Windows.
Make sure that Anaconda has been downloaded. You then want to create a separate environment to run the TF. You will then need to activate your TF in the environment. You can employ either pip or conda to activate the environment from within Anaconda.
Once everything has been activated, install the TF into the TensorFlow environment.
Installing TensorFlow in a virtual environment
Virtual environments are essential tools for Python developers, especially when working with complex libraries like TensorFlow. They provide isolated spaces where you can install packages without affecting your system-wide Python installation. This isolation prevents conflicts between different project dependencies and makes it easier to manage multiple projects with varying requirements.
To set up a virtual environment for TensorFlow:
On Windows:
1. Open Command Prompt and run:
--CODE language-markup--
python -m venv tensorflow_env
2. Activate the environment:
--CODE language-markup--
tensorflow_env\Scripts\activate
On macOS and Linux:
1. Open Terminal and run:
--CODE language-markup line-numbers--
python3 -m venv tensorflow_env
2. Activate the environment:
--CODE language-markup--
source tensorflow_env/bin/activate
3. Once your virtual environment is active, install TensorFlow using pip:
--CODE language-markup--
pip install tensorflow
Remember to activate your virtual environment each time you work on your TensorFlow project.
Using Docker to install TensorFlow
Docker offers a consistent and reproducible environment for running TensorFlow, regardless of your host system. It simplifies dependency management and ensures that your TensorFlow setup works identically across different machines.
To install TensorFlow using Docker:
1. Install Docker on your system from the Docker website.
Pull the official TensorFlow Docker image:
--CODE language-markup--
docker pull tensorflow/tensorflow:latest-gpu
Run a TensorFlow container:
--CODE language-markup line-numbers--
docker run -it --gpus all -p 8888:8888 tensorflow/tensorflow:latest-gpu
This command starts a Jupyter Notebook server accessible at http://localhost:8888. The --gpus all flag enables GPU support if available.
Verifying your installation
To verify your TensorFlow installation:
1. Open a Python interpreter or a new Python file.
2. Run the following code:
--CODE language-markup--
import tensorflow as tf
print(tf.__version__)
print("TensorFlow is installed successfully!")
# Check for GPU support
print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT available")
If TensorFlow is installed correctly, you'll see the version number and a success message. The last line will indicate whether GPU support is available.
To check hardware compatibility more thoroughly:
--CODE language-markup--
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
This will list all available devices, including CPUs and GPUs.
Updating TensorFlow
To update TensorFlow to the latest version:
1. If using a virtual environment, activate it.
2. Run:
--CODE language-markup--
pip install --upgrade tensorflow
When updating, be aware of potential compatibility issues with existing projects. Always test your code after updating. If you need to maintain multiple TensorFlow versions, consider using separate virtual environments for different projects.
Troubleshooting common installation issues
The operating systems above are large ecosystems with numerous libraries and dependencies, which may cause potential failures during TensorFlow installation. We cover some common errors and provide troubleshooting tips below:
- Version incompatibility. Tensorflow installation may fail when an incorrect Python version is used. So, ensure you have the required Python version for the respective TensorFlow framework installed before installation. Check the Python version on your machine using the python --version command, and use pip list to check installed packages.
- OS architecture. TensorFlow only supports a 64-bit architecture, so installation could fail if you use the 32-bit architecture.
- Missing dependencies. TensorFlow relies on other dependencies for a smooth installation and operation. Ensure you have dependencies like the Microsoft Visual C++ redistributable installed.
- GPU support issues. Verify that CUDA and cuDNN are correctly installed and configured.
- ImportError. If you encounter "No module named tensorflow," ensure you're in the correct virtual environment (if it’s being used).
- Memory errors. Reduce batch sizes or use a machine with more RAM.
For additional help:
- Visit the official TensorFlow forums
- Check the TensorFlow GitHub issues page
- Consult the TensorFlow documentation
The TensorFlow community is active and supportive. Don't hesitate to reach out if you encounter persistent issues.
Get help from a Tensorflow developer
While TensorFlow offers many benefits, it can feel complicated for those who don’t have much experience with data science. But you don’t need to be a data scientist to realize its benefits; independent, dedicated professionals can help you achieve your machine learning goals.
Independent TensorFlow developers help their clients take full advantage of this powerful software. With Upwork, you can easily browse the portfolios and credentials of world-class TF professionals. Simply create your job, receive bids from developers, and find the expert who best suits your needs. You can also look at the list of top performers and then reach out to them directly with your job. Find the professional who works best for you.
If you are an independent contractor specializing in TF, you can use the Upwork platform to find and bid on numerous jobs for companies needing a specific project completed. Finding the clients you need has never been easier as an independent professional.
Upwork is not affiliated with and does not sponsor or endorse any of the tools or services discussed in this section. These tools and services are provided only as potential options, and each reader and company should take the time needed to adequately analyze and determine the tools or services that would best fit their specific needs and situation. Prices are current at the time of writing and may change over time based on each service’s offerings.