Author(s): compensation
Originally published on Towards AI.
If you’re writing deep learning code on a machine with a GPU, TensorFlow will default to running on the CPU. This happens because TensorFlow does not automatically select the best hardware. To use the GPU, you must specify it manually.
To run TensorFlow code on a GPU, you don’t need any additional setup other than installing the GPU-enabled version of TensorFlow. However, if you are using Windows, you will need to install the Windows Subsystem for Linux (WSL) because, starting with TensorFlow 2.10, the GPU version is no longer natively supported on Windows. To install WSL, open a command prompt as administrator and run the following commands:
wsl --install
After installing WSL, open it from the search bar. The interface should look like this:

After opening WSL, run the following commands:
The following command checks if GPU drivers are installed on your system:
nvidia-smi
Download and install Miniconda using the following command, as we need to create a virtual environment:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
Run the following installer script:
bash miniconda.sh
Activate Conda using the following command:
source ~/.bashrc
Create a conda environment using the following command:
conda create - name tf-gpu python=3.9 -y
Activate the TensorFlow GPU environment using the following command:
conda activate tf-gpu
Install TensorFlow GPU using the following command:
pip install tensorflow(and-cuda)
Before we proceed, I want to point out that in order to interact with the GPU, we need to install CUDA, which is a software toolkit from NVIDIA that provides high-level abstractions that enable our code to run on the GPU. If you want to run your system on a GPU, you need to install CUDA. While CUDA can be installed manually for system-wide use, in this case, we only need it for TensorFlow. Fortunately, TensorFlow automatically installs the relevant CUDA dependencies.
Next, install Jupyter Notebook using the following command:
pip install notebook
Open Jupyter Notebook using the following command:
Jupyter notebook
After running Jupyter Notebook, copy the link shown in the WSL terminal and paste it into your browser.

After pasting the link in the browser, you will see this page.

Now you can create a new notebook and test your code.
import tensorflow as tfgpus = tf.config.list_physical_devices('GPU')
print(gpus)
details = tf.config.experimental.get_device_details(gpus(0))
print(f"Device: {gpus(0).name}, Name: {details.get('device_name', 'Unknown')}")

You have now successfully set up TensorFlow with GPU. When you run your code, it will automatically use the GPU. In TensorFlow, you do not need to manually specify the GPU in your code, as shown in the sample output below.

conclusion
Installing TensorFlow with a GPU can significantly speed up deep learning tasks. In this guide, we looked at the process of setting up WSL, Miniconda, and TensorFlow GPUs, ensuring that your code runs efficiently on available GPUs. Unlike some frameworks, TensorFlow automatically uses the GPU without requiring explicit configuration in your code. With this setup, you are now ready to train and test deep learning models with improved performance.
Published via Towards AI