How to Install OpenCL on WSL Ubuntu to Detect a CUDA GPU Device

Ng Tack Boon
2 min readJun 30, 2024

--

This guide will walk you through the steps to install OpenCL on WSL (Windows Subsystem for Linux) Ubuntu, enabling you to detect and use your CUDA GPU device.

Prerequisites

  1. Windows with WSL2 enabled.
  2. Ubuntu installed on WSL2.
  3. NVIDIA GPU with the latest drivers installed on your Windows system.
  4. CUDA Toolkit installed on your Windows system.

Step 1: Update Your System

First, make sure your system is up to date:

$ sudo apt update
$ sudo apt upgrade

Step 2: Verify CUDA is working correctly on WSL:

$ nvidia-smi

You should see your GPU details if everything is set up correctly.

Step 3: Install Latest LLVM and Clang

Check available versions of LLVM and Clang:

$ apt-cache madison llvm clang

Ensure the available versions meet the requirements for POCL (Portable Computing Language). If the versions are not up to date, proceed to add the repository directly.

$ CODENAME=$(lsb_release -cs)
$ echo "deb http://apt.llvm.org/${CODENAME}/ llvm-toolchain-${CODENAME} main" | sudo tee /etc/apt/sources.list.d/llvm.list
$ wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
$ sudo apt update

Install LLVM and Clang:

$ sudo apt install llvm clang

Verify the installation:

$ llvm-config --version
$ clang --version

If you encounter the warning /sbin/ldconfig.real: Can't link /usr/lib/wsl/lib/libnvoptix_loader.so.1 to libnvoptix.so.1 during installation, follow these steps:

  1. Open Command Prompt as Administrator and navigate to the directory:
$ cd C:\Windows\System32\lxss\lib

2. Run the following command to create a symbolic link:

$ mklink libnvoptix_loader.so.1 libnvoptix.so.1

Step 4: Install POCL (Portable Computing Language)

Clone the POCL repository:

$ git clone https://github.com/pocl/pocl.git

Set the LLVM version:

$ export LLVM_VERSION=<your LLVM version>

Install the necessary dependencies:

$ sudo apt install -y python3-dev libpython3-dev build-essential ocl-icd-libopencl1 \
cmake git pkg-config libclang-${LLVM_VERSION}-dev clang-${LLVM_VERSION} \
llvm-${LLVM_VERSION} make ninja-build ocl-icd-libopencl1 ocl-icd-dev \
ocl-icd-opencl-dev libhwloc-dev zlib1g zlib1g-dev clinfo dialog apt-utils \
libxml2-dev libclang-cpp${LLVM_VERSION}-dev libclang-cpp${LLVM_VERSION} \
llvm-${LLVM_VERSION}-dev

Build and install POCL:

$ cd pocl && mkdir build && cd build
$ cmake -DCMAKE_C_FLAGS=-L/usr/lib/wsl/lib -DCMAKE_CXX_FLAGS=-L/usr/lib/wsl/lib -DENABLE_HOST_CPU_DEVICES=1 -DENABLE_CUDA=ON ..
$ make
$ sudo make install
  • Note: The -DENABLE_HOST_CPU_DEVICES=1 option enable host CPU devices. You can turn it off by setting it to 0 if you do not need this feature.

Register the POCL library:

$ sudo mkdir -p /etc/OpenCL/vendors/
$ echo "/usr/local/lib/libpocl.so.<version>" | sudo tee /etc/OpenCL/vendors/pocl.icd
  • Replace version with the actualversion of the libpocl.so file installed on your system, e.g. 2.14.0 .

Verify POCL installation:

$ clinfo

You should see your CUDA-capable GPU and any other devices listed. Enjoy leveraging the power of your GPU for OpenCL applications!

--

--