- WSL2 allows you to use the physical Windows GPU from a real Linux system with CUDA and DirectML support.
- It is essential to combine an updated Windows, correct drivers, and a well-configured WSL2 distro.
- CUDA Toolkit, cuDNN, Docker and frameworks like PyTorch or TensorFlow are integrated without the need for full VMs.
- Proper GPU selection and monitoring with nvidia-smi ensure performance and stability in ML.
If you work with machine learning, deep learning, or data science on Windows, you've probably wondered more than once whether it's worth setting up a separate Linux environment or using the cloud. With official GPU support in WSL2, there's no need to complicate things: you can have a real Linux environment accelerated by CUDA directly on your Windows PC.
In this guide we'll see, step by step, how to enable the GPU in WSL2 for ML and CUDA , what you need at the system level, how to install the correct drivers, how to prepare Ubuntu (or another distro), how to integrate CUDA, cuDNN, Docker, TensorFlow, PyTorch and DirectML, and how to solve the most typical problems you'll encounter along the way.
Architecture: how the GPU works within WSL2
WSL2 runs a real Linux kernel inside a lightweight virtual machine managed by Windows itself. It's not a shoddy emulation: it's real Linux, with its kernel and user space, but extremely integrated with the host system.
To leverage the GPU from within that kernel, NVIDIA (and Microsoft) have developed a specific stack that exposes the Windows host's GPU to WSL2 . From the Linux perspective, this results in the classic CUDA interface via libcuda.so , allowing software to mistakenly believe it's running on a "native" Linux system.
Thanks to that layer, within WSL2 you can use nvidia-smi, nvcc, the CUDA Toolkit, cuDNN and virtually any CUDA-compatible ML framework (PyTorch, TensorFlow, etc.) without needing to set up a heavy virtual machine or always depend on the cloud.
Furthermore, this integration extends to Docker and NVIDIA Container Toolkit , so you can run GPU-enabled containers just like you would on a dedicated Linux server: for example, official NVIDIA NGC images for TensorFlow, PyTorch, or other AI frameworks.
In the graphics section, with WSLg, you can also control which GPU is used for Linux graphical user interface applications, which is very useful if your computer has an Intel iGPU or integrated AMD GPU and a dedicated NVIDIA card and you want the heavy workload to go to the NVIDIA card.
Windows requirements, GPU and required versions

For all of this to work, the first filter is the operating system: you need Windows 11 or Windows 10 version 21H2 or later . Earlier versions do not have mature (or no) GPU support in WSL2.
At the hardware level, you'll need a modern GPU with updated drivers . For pure CUDA, you'll need a compatible NVIDIA GPU (ideally a recent RTX series), but you can also use DirectML if your GPU is AMD or Intel, or even if you prefer to use frameworks adapted to DirectML on an NVIDIA card.
Additionally, WSL2 must be running a sufficiently recent kernel. For GPU features, kernel version 5.10.43.3 or higher is required . You can check this from PowerShell with:
wsl cat /proc/version
If you're setting up the environment from scratch on an updated Windows system, you'll most likely already have a compatible version, but it's worth checking in case your computer hasn't received updates in a while or if you've manually changed the WSL settings.
Install and update WSL2 with a suitable Linux distribution

Before delving into drivers and CUDA, ensure that WSL2 is installed and configured correctly . For ML development, it's recommended to use a glibc-based distribution, such as Ubuntu or Debian, due to their compatibility with NVIDIA repositories and packages.
From PowerShell with administrator privileges, you can enable WSL and the necessary component for WSL2 using the appropriate Windows commands. In modern versions of Windows 10/11, the most direct approach is to use `wsl --install` and specify the distribution, for example:
wsl –install -d Ubuntu-22.04
Once the installation is complete, you'll see the new Ubuntu app in the Start menu. The first time you open it, you'll be prompted to complete the basic setup (username, password, etc.). The next essential step is to update the package cache and system within that distribution.
sudo apt update && sudo apt -y upgrade
Additionally, it's advisable to ensure that WSL itself is using the latest kernel available for Windows. From PowerShell, run:
wsl-update
This combination of steps leaves you with an updated Ubuntu on WSL2 , perfect as a base for CUDA installations, ML frameworks, or even Docker.
Installing the NVIDIA driver with CUDA support for WSL2

The critical factor for using a CUDA GPU in WSL2 is the NVIDIA driver installed on Windows , not within Linux. Many people mistakenly believe they also need to install the driver within the Linux distribution, but this isn't necessary for WSL2.
You must download the CUDA and WSL-enabled driver from NVIDIA's official WSL page (or from the general driver search engine, selecting your GPU and system). It's important not to use an outdated driver: WSL2 and GPU support has received substantial improvements.
Once the driver is installed on Windows, you can verify that the system correctly recognizes the GPU and driver using PowerShell. Run a command that displays adapter information, or simply open the NVIDIA Control Panel and check the version. From WSL, when everything is working correctly, `nvidia-smi` will return a list of available CUDA devices.
In server scenarios (for example, Windows Server with WSL2), the workflow is similar: first, you install WSL and the Linux distribution, and then the NVIDIA driver for Windows Server . Next, on the Linux side, you install only the CUDA Toolkit and the necessary libraries, without any additional GPU drivers within the WSL kernel.
Installing CUDA Toolkit, cuDNN, and base tools within WSL2
With WSL and the NVIDIA driver ready, it's time to configure the development environment within Linux. The goal is to have the CUDA Toolkit, cuDNN, and basic Python tools installed so you can compile, run examples, and work comfortably with ML frameworks.
There are two approaches: using the official NVIDIA repositories for WSL/Ubuntu or using the nvidia-cuda-toolkit package from your distribution. For modern workflows, it's best to add the NVIDIA repositories specific to your distribution, as they are usually more up-to-date than generic packages.
From your Ubuntu terminal in WSL2, you can add the repositories and then install CUDA. An alternative, simpler approach in some environments is:
sudo apt -y install python3-pip nvidia-cuda-toolkit
If you use this package, make sure the CUDA version it installs is compatible with the PyTorch or TensorFlow versions you plan to use. Once installed, it's a good idea to add the path to the CUDA binaries to your PATH environment variable , for example, by editing your ~/.bashrc file and adding a line like this:
export PATH=/home/user/.local/bin${PATH:+:${PATH}}
After saving and closing, simply open a new terminal session or run `source ~/.bashrc` for the changes to take effect. From there, you can use tools like nvcc or run example scripts that use CUDA.
Using Docker and NVIDIA Container Toolkit in WSL2

In many machine learning workflows, especially in large enterprises or projects, it's common practice to encapsulate everything in containers. To leverage the GPU from Docker within WSL2, you need the Docker engine to have access to the GPU through the NVIDIA Container Toolkit.
There are two main options: install Docker Desktop on Windows and use its integration with WSL2, or directly install the Docker engine on the Linux distribution within WSL. In the second case, you can do so with:
curl https://get.docker.com | sh
sudo service docker start
Once Docker is installed, it's time to configure the stable NVIDIA Container Toolkit repository. For Ubuntu within WSL, you can follow the typical sequence: define the distribution variable starting from /etc/os-release, add the GPG key, and the list of NVIDIA repositories for nvidia-docker.
Then you install the necessary package with:
sudo apt-get update
sudo apt-get install -y nvidia-docker2
With this set up, you can launch containers that see the GPU using the `--gpus` option . For example, for an official TensorFlow container on NGC:
docker run –gpus all -it –shm-size=1g –ulimit memlock=-1 –ulimit stack=67108864 nvcr.io/nvidia/tensorflow:20.03-tf2-py3
Within that container, many examples are already prepared. For example, you can go to NVIDIA's CNN examples and run a pre-trained ResNet:
cd nvidia-examples/cnn/python
resnet.py –batch_size=64
In server environments with multiple GPUs, you can also try sample containers like the classic CUDA nbody to verify that all cards are responding correctly via Docker:
docker run –rm -it –gpus=all nvcr.io/nvidia/k8s/cuda-sample:nbody nbody -gpu -benchmark -numdevices=5
TensorFlow, PyTorch and DirectML in WSL2
WSL2 is not limited to pure CUDA: it also allows you to take advantage of TensorFlow-DirectML and PyTorch-DirectML , which is especially interesting if your GPU is AMD or Intel, or if you want to take advantage of DirectML even with an NVIDIA card.
The starting point is the same: an updated GPU driver from the vendor installed on Windows, whether it's AMD, Intel, or NVIDIA. From there, you create a virtual Python environment within your Linux distribution. A common option is to use Miniconda.
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
conda create –name directml python=3.7 -y
conda activate directml
With the environment activated, install the framework you want. For TensorFlow-DirectML :
pip install tensorflow-directml
And for PyTorch-DirectML , you first cover some basic BLAS and LAPACK dependencies, and then install the corresponding package:
sudo apt install liblas3 libomp5 liblapack3
pip install torch-directml
To verify that everything is working, you can open a Python session and try a simple operation (for example, a tensor sum) and check that no errors occur and that DirectML acceleration is detected in the framework's output.
Installing PyTorch with CUDA support in WSL2
If your goal is to use PyTorch with traditional CUDA on an NVIDIA GPU, the easiest way is to install the version specifically designed for a particular CUDA version, for example, CUDA 11.8 . PyTorch provides specific indexes for compatible wheels.
Once you have Python and pip or a Conda environment ready, you can run something like:
pip3 install torch torchvision torchaudio –index-url https://download.pytorch.org/whl/cu118
To verify that PyTorch correctly sees the GPU, enter the interactive Python console within WSL2 and run:
import torch
torch.cuda.device_count()
torch.cuda.is_available()
The first command imports PyTorch, the second tells you how many CUDA-compatible devices the system detects, and the third returns True if the GPU is available to the framework. If something goes wrong, the usual steps are to check the drivers, CUDA version, and whether WSL is up to date.
In multi-GPU environments, PyTorch also allows you to restrict usage to certain cards using environment variables such as CUDA_VISIBLE_DEVICES or specific device selection APIs, which is useful when sharing the machine with multiple processes or users.
TensorFlow with CUDA on WSL2 and full ML flow
TensorFlow can also leverage the GPU in WSL2 via CUDA, especially when combined with NVIDIA Docker images or carefully curated installations from the CUDA and cuDNN repositories. The typical flow includes:
1) Install the NVIDIA driver with CUDA support on WindowsAs we have already mentioned, this ensures that the version is compatible with the TensorFlow release you are going to use.
2) Configure in WSL2 the CUDA Toolkit and cuDNN following the official NVIDIA CUDA guide in WSL.
3) Optionally, use an NGC image of TensorFlow with GPU support, which already comes adjusted with the correct CUDA/cuDNN versions, avoiding library compatibility issues.
Once your environment recognizes the GPU (you can verify this with `nvidia-smi` from WSL2), you can train intensive models directly from Linux on Windows, without full virtual machines or needing to upload everything to the cloud. Typically, this is combined with VS Code Remote – WSL and Jupyter for comfortable work on laptops.
For beginners, a good way to get comfortable is to try simple examples of neural networks, monitor consumption in nvidia-smi and experiment with batch size, number of layers or precision (FP16/FP32) to see how the GPU load varies.
GPU access verification and monitoring
Verifying that everything is configured correctly is key before starting to train large models. Since WSL2, the go-to command is `nvidia-smi` , which should display the list of GPUs, total and used memory, GPU usage, temperature, etc.
If the command returns a message like “No devices were found”, it usually indicates that the Windows driver is not installed correctly or WSL is not up to date . In many cases, this can be resolved by:
wsl –shutdown
and reopening the distribution to reload the environment, or updating drivers and restarting the system. There are also additional tools like gpustat that provide a more user-friendly summary of GPU load.
During long training sessions, it's advisable to use a separate console with nvidia-smi or monitoring scripts to keep an eye on temperature, memory usage, and system capacity . This helps detect bottlenecks (such as running out of VRAM) or performance issues due to poor configuration.
GPU selection in systems with multiple graphics cards (NVIDIA, AMD, iGPU)
Modern laptops and desktops often have multiple GPUs: for example, an integrated Intel or AMD iGPU and a dedicated NVIDIA GPU . Since WSL2, and especially with WSLg for graphics applications, it's important to be able to choose which one to use.
Microsoft documents mechanisms for selecting the GPU in WSLg, and users who have encountered situations where their application was using the iGPU have resolved the issue by following the official WSLg GPU selection guide available in the microsoft/wslg GitHub repository.
Additionally, for environments where DirectML is supported on multiple GPUs, a specific device can be selected using the environment variable:
export MESA_D3D12_DEFAULT_ADAPTER_NAME=» »
This variable performs a string match with adapter names, so if you enter something like "NVIDIA" , it will select the first GPU whose name begins with NVIDIA. This is a useful trick when the system defaults to the wrong GPU (for example, prioritizing an integrated AMD GPU over a dedicated NVIDIA GPU for certain machine learning workloads).
In particularly troublesome cases (for example, when using FAISS or other components within frameworks like LangChain or Ollama), you might see it connect to the wrong GPU even when NVIDIA is visible in WSL. In these situations, you usually need to combine WSLg's configuration, the aforementioned environment variable, and specific library settings to force the correct device.
Best practices and solving common problems
Once everything is up and running, there are some best practices that make a big difference in day-to-day comfort and performance. The first is to always work with virtual Python or conda environments and activate them before launching any training:
source ~/.venvs/ml/bin/activate (or the equivalent command in your configuration)
Keeping an up-to-date requirements.txt file with key dependencies (for example, using pip freeze > requirements.txt ) makes it easy to rebuild the environment on another machine or after a format without headaches.
Another important recommendation is to avoid working directly in /mnt/c/ for training data or large projects. Disk performance is usually much better if you use your Linux home directory in WSL (for example, ~/proyectos_ml ), especially when working with many files or large volumes of data.
For interactive development, VS Code with the Remote – WSL extension and Jupyter support offers a very convenient experience: you edit on Windows, run on Linux with a GPU, and everything is quite integrated. Furthermore, you can use tools like tmux or screen to keep training sessions running in the background even after you close the terminal window.
If you share the machine or have heavy processes running in parallel, you may want to adjust resource limits in the Windows .wslconfig file (for example, RAM and the number of cores WSL2 can use), and manually control when you run heavy training sessions to avoid crashing the system.
Regarding troubleshooting, the most common issues include nvidia-smi not detecting the GPU (due to an incorrect driver or outdated WSL), incompatibilities between CUDA versions and frameworks (PyTorch/TensorFlow), and CUDA installations directly from the web without following the official WSL repositories and guides, which often results in library conflicts. Generally, it's better to rely on the WSL-specific repositories and documentation than on generic installations.
With all these elements properly configured—a modern Windows 11 or 10, updated WSL2, correct GPU drivers, CUDA/cuDNN in the distribution, Docker and/or DirectML as needed—you can have a high-performance Linux environment for AI and ML on your own laptop or desktop without sacrificing the convenience of Windows. It's a very powerful hybrid solution that, when properly tuned, allows you to train complex models, prototype workflows, and debug code quickly without relying heavily on cumbersome virtual machines or the cloud.
Table of Contents
- 1 Architecture: how the GPU works within WSL2
- 2 Windows requirements, GPU and required versions
- 3 Install and update WSL2 with a suitable Linux distribution
- 4 Installing the NVIDIA driver with CUDA support for WSL2
- 5 Installing CUDA Toolkit, cuDNN, and base tools within WSL2
- 6 Using Docker and NVIDIA Container Toolkit in WSL2
- 7 TensorFlow, PyTorch and DirectML in WSL2
- 8 Installing PyTorch with CUDA support in WSL2
- 9 TensorFlow with CUDA on WSL2 and full ML flow
- 10 GPU access verification and monitoring
- 11 GPU selection in systems with multiple graphics cards (NVIDIA, AMD, iGPU)
- 12 Best practices and solving common problems