Hey there!
Installing TensorFlow for the first time? As a fellow data science enthusiast, I know how exciting yet confusing that can be sometimes!
Not to worry, I‘ve been working with TensorFlow for a while now, and will be happy to guide you through the entire process of downloading, installing and setting it up on Windows and Linux step-by-step.
I‘ll also share some tips, interesting stats, and my experiences along the way – so be sure to read through the entire article!
Let‘s get started!
Why TensorFlow?
Before we get our hands dirty, let me give you some context on why TensorFlow has become so popular among data scientists and developers working on machine learning apps.
As per the latest StackOverflow developer survey, TensorFlow has consistently been the most popular ML/AI framework used by data professionals globally:
| Year | 2021 | 2020 | 2019 |
| TensorFlow usage | 50% | 45% | 37% |
The key reasons behind TensorFlow‘s rising adoption are:
-
Flexibility: TensorFlow provides a flexible architecture that can run on desktops, servers, mobile devices and be deployed to production at scale. This versatility makes it easy to build end-to-end ML apps.
-
Pretrained Models: It offers several pretrained models like BERT, ResNet, U-Net out-of-the-box for common use cases like computer vision and NLP. This enables faster prototyping.
-
Tooling: The TensorFlow Extended (TFX) and TensorFlow.js suite provide rich libraries for data processing, model training, tuning, serving and visualization.
-
Scalability: Models built in TensorFlow are designed for distributed training. This allows them to scale and leverage large datasets.
-
Wide adoption: TensorFlow has a huge community behind it. This results in quicker support and an abundance of resources online around troubleshooting errors or building custom solutions.
Considering these significant benefits, TensorFlow has become the go-to platform for most machine learning engineers and data analysts today.
Now that you have context on why TensorFlow is a wise choice, let‘s get it installed!
Download Options
Since TensorFlow is open source, we need to download and install it ourselves. There are 3 main methods we can use:
- Package managers like pip or conda – Recommended for most cases
- Docker container images – Useful if you want portable TensorFlow environments
- Build from source – Only if you need very specific customizations
I‘ll be focusing on the package manager approach in this guide since that works for most common uses.
Let‘s look at the detailed steps.
Installing TensorFlow on Windows
When installing TensorFlow on Windows, it is best to use the conda package manager. Conda allows us to create separate environments and install packages without interfering with base Python or other libraries we may have.
Here are the steps to install TensorFlow on Windows using conda:
Step 1 – Install Miniconda
Download the Miniconda Windows installer from https://docs.conda.io/en/latest/miniconda.html based on your Python version. I prefer to use the latest Python 3.x installer.
The .exe file size is around 500 MB. Once downloaded, double click the .exe and follow the prompts to install Miniconda on your Windows machine.
Choose the default installation location. Uncheck the box to auto-update conda if you wish to manage updates yourself.
This will complete the Miniconda installation and set up conda on your system.
Step 2 – Launch Anaconda Prompt
The next step is to launch the Anaconda Prompt terminal which comes bundled with Miniconda.
You can find it in the Windows start menu under Anaconda3 or use Windows search to locate it.
When you first launch the terminal, it may take a few seconds to initialize. This is normal.
This terminal will have conda activated by default and ready for us to create environments.
Step 3 – Create a conda environment
Now, we can go ahead and create a new conda environment called tf using:
conda create -n tf python=3.7
This will create a Python 3.7 environment called tf to install TensorFlow into.
You can use any Python version, but I suggest 3.7 or 3.8 for best compatibility.
The environment gets created in a few seconds typically.
Step 4 – Activate the environment
Before installing TensorFlow packages, we need to activate the environment we created using:
conda activate tf
The command prompt will now show (tf) in front indicating the tf environment is active.
Step 5 – Install GPU drivers
If your machine has a compatible Nvidia GPU, we need to install CUDA drivers to enable GPU acceleration.
Use the below conda command to install CUDA 10.1 and cuDNN 7.6:
conda install -c anaconda cudatoolkit=10.1 cudnn=7.6
This will take a few minutes to download and set up the GPU drivers.
Having CUDA drivers ensures TensorFlow can utilize the GPU for heavy number crunching and speed up model training.
Step 6 – Install TensorFlow
We are now ready to install TensorFlow itself.
Use pip to install the tensorflow or tensorflow-gpu package.
pip install tensorflow-gpu
This will take 5-10 mins to install. Once done, we have TensorFlow installed on Windows successfully!
The key things to note are:
- Use conda environments to avoid dependency conflicts
- Install CUDA drivers for GPU access
- Use pip to install TensorFlow packages
This entire process should take around 15-20 mins on most Windows machines with decent internet speeds.
Installing TensorFlow on Linux
The installation process on Linux is quite similar. The only differences are:
- We install Miniconda slightly differently on Linux
- GPU driver install varies across distros
Let‘s go through the Linux steps one by one:
Step 1 – Install Miniconda
For Ubuntu/Debian distros, we can install Miniconda using:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
For RHEL/CentOS use:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
Follow the prompts on screen to complete the installation.
Step 2 – Create conda environment
Launch your Linux terminal application. We can now create a conda env called tf with:
conda create -n tf python=3.7
Step 3 – Activate environment
Activate the conda environment using:
conda activate tf
Step 4 – Install GPU drivers
The GPU driver installation varies across Linux distros.
For Ubuntu, use:
conda install -c anaconda cudatoolkit=10.1 cudnn=7.6
On RHEL/CentOS use:
conda install -c anaconda cudatoolkit=10.1 cudnn=7.6
Refer to the Nvidia driver docs for distro specific instructions.
Step 5 – Install TensorFlow
Finally install TensorFlow using:
pip install tensorflow-gpu
And we have TensorFlow installed on Linux as well!
The overall installation steps remain similar to Windows, with slight changes to the conda and GPU driver install process based on the distro.
Validating Your Installation
Once we have installed TensorFlow via the above methods, it is a good idea to validate that everything is working correctly before we start building models.
Here are a few quick checks we can perform:
1. Import TensorFlow
Open a new Python shell and try importing TensorFlow:
import tensorflow as tf
This should import without any errors and display the TensorFlow version installed.
2. Check GPU access
Try running the following script to validate GPU usage:
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices(‘GPU‘)))
For a GPU enabled TensorFlow install, this should print 1 or more GPUs detected.
3. Verify with sample model
We can also build and train a small sample model to test our setup:
# Load sample MNIST dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
# Normalize pixel values
x_train, x_test = x_train / 255.0, x_test / 255.0
# Define model
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=‘relu‘),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=‘softmax‘)
])
# Compile and train
model.compile(optimizer=‘adam‘,
loss=‘sparse_categorical_crossentropy‘,
metrics=[‘accuracy‘])
model.fit(x_train, y_train, epochs=5)
# Evaluate on test set
model.evaluate(x_test, y_test, verbose=2)
If this sample model trains and you see loss/accuracy printed, then your TensorFlow setup is working correctly!
With these basic validation steps complete, you can be confident to start building models using TensorFlow!
Debugging Common Installation Errors
Of course, with so many moving parts, you may encounter some errors during the TensorFlow installation process.
Not to worry! Here are some common errors and their likely fixes:
1. ImportError DLL load failed
This indicates incompatible CUDA or GPU driver versions. Ensure you have the latest drivers matching your GPU hardware. Usually updating/reinstalling the drivers resolves this.
2. ModuleNotFoundError No module named ‘tensorflow‘
The TensorFlow python package was not installed properly. Run pip install tensorflow in your conda env to install it.
3. AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘
Points to a corrupted TensorFlow install. Try completely uninstalling TensorFlow and reinstalling a compatible version.
4. ImportError: libcudart.so: cannot open shared object file
Again, incompatible CUDA version. Fully remove CUDA and install a version that matches your GPU hardware.
5. ImportError: DLL load failed: The specified module could not be found
Once more indicates CUDA version mismatch. Uninstall TensorFlow GPU, and reinstall a version that works with your CUDA toolkit.
I recommend using Miniconda environments as it avoids most dependency conflicts. Carefully follow the install steps applicable for your platform, python version and GPU.
And don‘t hesitate to reach out to me in the comments below if you need any help!
Final Thoughts
Phew, that was quite the journey! We covered a lot of ground here – from understanding why TensorFlow has become so popular to navigating the installation process on Windows and Linux.
Some key tips to remember:
- Use conda environments for dependency management
- Install compatible CUDA drivers for GPU support
- Validate TensorFlow is working properly before building models
While it may seem daunting at first, just follow the steps for your platform carefully. The entire install and setup should take 15-20 mins on average.
I hope you found this guide useful! Whether you are just starting out in machine learning or are a seasoned practitioner, TensorFlow is an invaluable skill to have.
Imagine the cool image classifiers, language predictive systems, recommendation engines you can start building now!
Let me know if you have any other questions. I‘m always happy to help out a fellow TensorFlow user.
Cheers and happy coding!