How to download, install and configure Tensorflow on Windows and Linux
TensorFlow is an open source platform developed by Google for machine learning and AI (artificial intelligence). It helps with a number of tasks for developers working in the field.
To get started, you need to have an understanding of machine learning, or in particular deep learning, before you can use TensorFlow.
Here let me highlight a few things about TensorFlow, its features and quick ways to install it on Windows and Linux.
Overview of TensorFlow
Technically, TensorFlow is an open source platform that helps with deep learning applications and any other machine learning use cases.
This simplifies the creation and deployment of machine learning applications. If you want to solve a problem with machine learning, you can get help with TensorFlow.
TensorFlow provides tools for developing and training models using either Python or JavaScript. Although I'm not a developer, you can check out its documentation to see how this affects your machine learning application deployment workflow.
Features of TensorFlow
TensorFlow is famous for several reasons, and you can appreciate it for yourself by knowing its top feature offerings.
If we are going to discuss technical advantages, you will have to compare them for what you are doing. So, we will focus on the general features that are useful to most.
1. Open source
Google decided to open source TensorFlow in 2015 to allow the community to improve it and provide transparency on how it works.
Developers can customize the library in a variety of ways to solve problems you might not expect.
Without the open source framework, it might not have been as popular as it is today. Consequently
2. Easy debugging
TensorFlow aims to help you with simple model building; therefore, light debugging is part of the process.
The intuitive user interface is the cherry on top.
3. Supports both CPUs and GPUs
With TensorFlow, you get the ability to train computing data on the CPU or GPU. Typically, the GPU speeds up deep learning applications compared to the CPU.
So, if you have a powerful GPU in your arsenal, TensorFlow can help you get the most out of it.
4. Useful Machine Learning APIs
APIs help developers integrate various features into their applications. And TensorFlow provides access to a good collection of stable APIs.
Some of them may also offer performance benefits. According to its official statements, you shouldn't have any problems with the ones available in Python. If you're working with other languages, you'll need to check with the TensorFlow maintainers about how good they are for your use case.
5. Ready models for production
TensorFlow offers many pre-trained models. Whether you're a professional or a beginner, you can use them to save time and build machine learning models faster.
In addition to these features, you get flexibility, ease of use, a set of visualization tools, and more that can help your machine learning development workflow.
Now that you have a good understanding of TensorFlow, where can you download it? How to install and configure it on Windows and Linux systems?
Let's discuss this below.
Download and Install TensorFlow
Unlike other programs, here you don't get an .exe installation file. First of all, you will need to download the package using the recommended package manager.
In general, there are different installation methods. We can list them as follows:
- Using Miniconda and pip
- Using Miniconda and pip on WSL 2
- Using a Docker container
- Building from source
How to install TensorFlow on Windows?
Unlike other programs, here you don't get an .exe installation file. You will need to download the package using the recommended package manager.
No. 1. Using Miniconda and pip (recommended method)
Note. At the time of this writing, TensorFlow 2.10 was the latest version to support GPUs on Windows (originally). If you are working with newer packages, TensorFlow recommends installing TensorFlow on WSL 2, which will be discussed next.
If you want to use TensorFlow with GPU support, TensorFlow recommends using Miniconda (an installer for the conda package manager) to get started.
With Miniconda, you can create a separate environment to avoid conflict with any other software on your system.
To get started, you need to download the latest Miniconda installer for Windows and follow the on-screen instructions to complete the installation.
After that, you need to run the Miniconda prompt as shown in the screenshot:
Here's what it looks like:
After you see the Anaconda prompt window, you can enter the following command to make sure the conda package manager is up to date:
conda update -n base -c defaults conda
With that in mind, here are the steps to follow to install TensorFlow:
First, to create a new environment (named tf):
conda create --name tf python=3.9
Tip: you can enable/disable it with the commands: conda enable tf and conda deactivate
You will need to activate it to continue. To enable GPU support in the process, you must make sure you have the graphics driver installed (NVIDIA GPU) and then install a few packages with the following command:
conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
It downloads about 1 gigabyte of packages that include tools that allow you to deploy GPU machine learning applications with a deep neural network.
Finally, you will need to use the pip package manager to install the TensorFlow package. You can use conda to install Tensorflow, but it may not require the latest stable release.
Before proceeding, make sure pip has been updated with the command:
pip install --upgrade pip
After that, install TensorFlow with:
pip install tensorflow
You will notice that numerous packages are being built/installed. It may seem stuck in the process, but give it a minute and it should resume and complete the installation.
#2 Using Conda and pip on WSL 2
Assuming you already have WSL 2 installed on your system, you can install TensorFlow using the following commands in the distribution's terminal:
conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/ python3 -m pip install tensorflow # Verify install: python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
You can paste all at once and they will be processed one by one.
If you have not installed WSL 2 on Windows. Go to the command prompt with administrator rights and type the following:
wsl.exe --install
It should boot Ubuntu and enable the WSL feature for your system. To complete, you will need to restart your computer.
If you cannot find Ubuntu on your system, you can go to the Microsoft Store and install Ubuntu WSL.
No. 3. Building from source
Given that TensorFlow is open source, you can build it from scratch with your own configuration options.
Hence, it is recommended for advanced users who are aware of all parameters and know how to adjust nuts and bolts. Refer to the official documentation to learn more about this.
How to install TensorFlow on Linux?
Just like on Windows, you can install TensorFlow using Miniconda and pip on Linux. Or select a build from source.
Let me show you how it's done:
No. 1. Using Miniconda and pip (recommended method)
Note. Follow the same commands as in Windows. The only difference is how you install/download Miniconda on Linux.
Here is how to install Miniconda on Linux using the terminal:
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o Miniconda3-latest-Linux-x86_64.shbash Miniconda3-latest-Linux-x86_64.sh
Restart the terminal on your Linux distribution to find something like this:
You will notice (base) in front of the terminal prompt variables. This indicates that conda is currently active and installed.
Don't disable it until you've finished installing TensorFlow.
You can go to the steps mentioned above for Windows and install it. Or paste in the following to install TensorFlow:
conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/ python3 -m pip install tensorflow # Verify install: python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
On Linux, you may have problems with the GPU driver. To do this, you should refer to the NVIDIA documentation to find out more.
#2 Building From Source
As with Windows, building from source on Linux is complex and is reserved for advanced users only.
You (assuming you are a beginner) should not choose this method unless you have something specific in mind. The best way to learn more about this is to refer to the documentation.
How to install TensorFlow with Docker? (Windows and Linux)
Regardless of the platform, Docker makes it easy to install TensorFlow images.
Make sure you have Docker installed on your system, or use our Docker installation guide.
Once the configuration is complete, you need to enter the following command from Docker:
docker pull tensorflow/tensorflow
You need experience with Docker containers to run a container with the configurations you need for your work.
To learn about specific GPU support or download a different version of TensorFlow, refer to the options available in the official documentation.
This is what the command looks like when you want to run it with Docker:
docker run [-it] [--rm] [-p hostPort:containerPort] tensorflow/tensorflow[:tag] [command]
Summary
Installing TensorFlow is a one-time thing, and with our guide, it should be a hassle-free process for most.
If you already had previous configurations or settings with older versions of Python or the older Conda package manager. Be sure to apply the latest updates to install TensorFlow without any problems.