Last modified: Jun 01, 2025 By Alexander Williams

Install PyCUDA in Python Easily

PyCUDA lets you use NVIDIA GPUs for parallel computing in Python. It is a powerful tool for high-performance tasks.

Prerequisites

Before installing PyCUDA, ensure you have Python 3.6+ and an NVIDIA GPU. You also need the CUDA Toolkit installed.

Check if your GPU supports CUDA on NVIDIA's website. If you need help, see our guide on Install PyTables with HDF5 Support in Python.

Install CUDA Toolkit

Download the CUDA Toolkit from NVIDIA's website. Follow the installation instructions for your OS.

Verify the installation by running:


nvcc --version


nvcc: NVIDIA (R) Cuda compiler

Install PyCUDA

Use pip to install PyCUDA. Run the following command:


pip install pycuda

If you face issues, try installing with --pre for pre-release versions.

Verify PyCUDA Installation

Test PyCUDA with a simple script. This checks if the installation works.

 
import pycuda.driver as drv
drv.init()
print("PyCUDA installed successfully!")


PyCUDA installed successfully!

Common Issues

If you get errors, ensure CUDA paths are set. Check your environment variables.

For more help, see our guide on How to Install PyOpenCL in Python Easily.

Example: Vector Addition

Here’s a simple PyCUDA example. It adds two vectors using GPU.

 
import pycuda.autoinit
import pycuda.driver as drv
import numpy as np
from pycuda import gpuarray

# Define vectors
a = np.array([1, 2, 3], dtype=np.float32)
b = np.array([4, 5, 6], dtype=np.float32)

# Transfer to GPU
a_gpu = gpuarray.to_gpu(a)
b_gpu = gpuarray.to_gpu(b)

# Add vectors
result_gpu = a_gpu + b_gpu

# Print result
print(result_gpu.get())


[5. 7. 9.]

Conclusion

PyCUDA is a great tool for GPU computing in Python. Follow these steps to install and verify it.

For more Python guides, check Install MoviePy in Python Easily.