How to install tensorflow with CUDA on Windows (conda virtual env+pip)

Why yet another tutorial?

I've been trying to learn how to properly configure a tensorflow environment on a windows machine from my home computer, in preperation to install it on the lab computer. There are many tutorials that you can follow on how to install GPU enabled Tensorflow on Windows machines, many of them have different approaches. The official document installs the cuda libraries (cudatoolkit and cudnn) using conda before installing tensorflow with pip, while this TowardsDataScience tutorial installs the cudatoolkit/cudnn by hand (downloading from nvidia.com) in the system then install tensorflow in a virtual conda environment. Both method works but is not very straight forward.

Additionally, there seems to be little information on how to use Jupyter Notebook with Tensorflow library in a virtual environment (conda or venv). Natually I expected some level of complication, from the cross-virtual-layer communications between Browser and Jupyter notebook and Python Kernal and packages in virtual environment.

But most importantly, I'm rather unhappy about how tutorials (especially coding tutorials) are written in digital age. The problem is they are too long. Tutorials likes to explain why do you need to do each step in detail. Yes, those contents are important and can be very useful for many, but for those in a rush for a quick fix to carry on their work ASAP, they are redundant information. To access the crucial information they ahve to scroll pass the redundant information where effort was wasted and attention diverted. This is a habit that came from paperback age, where the content of each page is fixed and references are very hard to get to.

I'd like to try on a different approach to tutorials. I aim to fit everything that is needed to be done in as little words as possible. Interactive web pages provides excellent tools for it. The most useful features are collapsible tabs and hyperlinks, that provides options for access to the useful but unecessary information while maintaining a clean and concentrated necessary contents.

Aim

This tutorial allows you to use a GPU-enabled tensorflow 2.9.1 in Jupyter notebook, without messing with your system environments (except adding VS2019 and miniconda, but chances are you'll need them someday)

Very Simple Steps

Follow these instructions step by step, the whole thing should take you about an hour depending on your internet speed. Goodluck

  1. Check Prerequisites that should be trivile but sometimes gives you a nasty surprise

  2. Install VS2019 Community (Doesn't count if you have 2022!) from here (the link may ask you to sign in, after signing in, close the tab and ckick on this link again)

  3. Install miniconda from here

  4. Open miniconda prompt (search in windows start menu 'miniconda', bottom left of your screen)

  5. Initialise virtual environment conda create --name tf-gpu python=3.9

  6. Activate conda environment conda activate tf-gpu

  7. Install cuda support conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0

  8. Update pip pip install update pip

  9. Install tensorflow pip install tensorflow==2.9.1

  10. Check if tensorflow is working with your GPU python -c "import tensorflow as tf;print(tf.test.gpu_device_name())" (You should see your GPU name somewhere like this)

  11. Install jupyter notebook pip install jupyter

  12. Check if jupyter works (if you don't know how Jupyter notebook works here is a tutorial) by creating a new ipython notebook, run a cell with import tensorflow as tf;print(tf.test.gpu_device_name()) and you should see something like this.

  13. When you finish working on it, just type conda deactivate in the prompt to leave the environment

How to use

This build allows you to use CUDA Tensorflow within Jupyter notebook. But if you know what you're doing with conda environments, you can run anything with tensorflow in the conda environment tf-gpu, just amke sure everything happens in the environment.

  1. Open the conda prompt by searching miniconda in windows start search bar (bottom left of your screen)

  2. Activate the environment tf-gpu activate tf-gpu

  3. Open Jupyter notebook jupyter notebook

  4. Use tensorflow

The end product should have the following package installed:

  • In the system environment (Default at C:\Program Files (x86)\ or )C:\Program Files\)

    • VS2019

    • miniconda

  • In the conda virtual environment tf-gpu (Default at C:\Users\User_Name\miniconda3\envs\tf-gpu, or C:\ProgramData\Miniconda3 if you installed miniconda for all users)

    • python 3.9 (conda)

    • codatoolkit=11.2/cudnn=8.1.0 (conda-forge)

    • tensorflow 2.9.1 (PiPy)

    • Jupyter notebook (PyPi)


How to detele environment

  1. If you ever comes to needing to detele this environment, the easiest way is through conda env remove -n tf_gpu. This will remove the folder where the virtual environment is installed alongside all the packages in the environment.

  2. However we have also installed two things in the system environment, miniconda and VS2019. These can be simply removed from control panal if you really don't want them.

These two steps will leave your computer as if nothing had been installed, but I'd leave the miniconda and take this opportunity to learn it. It's annoying and conda packages are quite shit but sometimes they come in handy. I'd highly recommend looking into managing containers (Docker) instead of managing virtual environments (conda) since containers are a lot more transferable if you ever want to deploy somewhere else (e.g. in a remote HPC). I might do a tutorial in the future on how to install Tensorflow on windows with container (Win10+Docker+WSL2)

Known problems

  1. This installation is about 3 time slower than when I installed in system environment for some reason