Install CUDA Toolkit and Run Your First GPU Program
In this article
To install the CUDA Toolkit and verify it works, you must download the official installer from NVIDIA, configure your system environment variables, and compile a C++ program using the NVCC compiler. This process enables your computer to use the GPU for general-purpose computing (GPGPU), allowing you to run complex mathematical operations much faster than a standard CPU could alone.
What You'll Need
- An NVIDIA GPU that supports CUDA (most GeForce, Quadro, and Tesla cards).
- A Windows PC with an up-to-date version of Windows 10 or 11.
- Administrative privileges on the computer to install drivers and system tools.
- A C++ compiler installed on your system. For Windows, Visual Studio (Community edition is free) is the standard requirement as CUDA relies on the MSVC compiler.
- A stable internet connection to download several gigabytes of installation data.
Step-by-Step
- Verify Hardware Compatibility: Before downloading anything, ensure your GPU is CUDA-capable. You can check this by opening the NVIDIA Control Panel, clicking Help > System Information, and looking for the CUDA version listed in the details. If no version is listed, your hardware may not support CUDA.
- Install Visual Studio: CUDA requires a host compiler to handle the CPU side of your code. Download and install Visual Studio. During installation, you must select the "Desktop development with C++" workload. Without this specific workload, the CUDA installer will fail because it cannot find the necessary C++ build tools.
- Download the CUDA Toolkit: Visit the official NVIDIA CUDA Toolkit archive or download page. Select your operating system (Windows), architecture (x86_64), version of Windows, and the installer type (the "exe local" is generally more reliable than the network installer). Download the installer to your local drive.
- Run the Installer: Launch the .exe file. The installer will first check your system for compatibility. Choose the "Express" installation option for most users, as this automatically handles the driver installation and the toolkit setup. If you already have a newer driver installed than the one bundled with the toolkit, the installer may warn you; in this case, choose "Custom" and uncheck the driver component to keep your current version.
- Configure Environment Variables: The installer usually adds CUDA to your system path, but you should verify this. Open the Start menu, search for "Edit the system environment variables," and click the result. In the System Properties window, click Environment Variables. Under System Variables, look for CUDA_PATH. If it is missing, you must manually add it, pointing to the directory where the toolkit was installed (usually C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.X).
- Verify Installation via Command Line: Open a Command Prompt or PowerShell window. Type
nvcc --versionand press Enter. If the installation was successful, the system will return the version of the CUDA compiler. If you see an error saying the command is not recognized, restart your computer to refresh the environment variables. - Write Your First GPU Program: Open a text editor (like Notepad or VS Code) and create a file named
hello_cuda.cu. The.cuextension tells the compiler that this file contains CUDA C++ code. Copy the following basic code which adds two numbers on the GPU:#include <iostream> __global__ void add(int a, int b, int *c) { *c = a + b; } int main() { int c; int *dev_c; cudaMalloc((void**)&dev_c, sizeof(int)); add<<<1,1>>(2, 7, dev_c); cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost); std::cout < < "2 + 7 = " < < c < < std::endl; cudaFree(dev_c); return 0; } - Compile the Program: Open the "Developer Command Prompt for VS" (search for it in the Start menu) to ensure the C++ compiler is in the path. Navigate to the folder where you saved your file and run the command:
nvcc hello_cuda.cu -o hello_cuda.exe. - Run and Prove Success: Execute the compiled program by typing
hello_cuda.exe. If the output displays "2 + 7 = 9", you have successfully installed the toolkit, configured the compiler, and executed your first program on the GPU hardware.
Common Mistakes
One of the most frequent errors is attempting to install the CUDA Toolkit before installing Visual Studio. Because the toolkit integrates with the C++ compiler, it looks for the MSVC build tools during the installation process. If they aren't there, the installation may complete, but you will find that nvcc cannot compile any code because it cannot find the host compiler.
Another common mistake is ignoring the driver version. While the CUDA Toolkit comes with a driver, it is often an older version. If you have a very new GPU (e.g., a 40-series card) and install an old toolkit, the bundled driver might be too old to recognize your hardware. Always ensure your NVIDIA Game Ready or Studio drivers are updated via GeForce Experience or the NVIDIA website before starting.
Users often forget to use the "Developer Command Prompt" instead of the standard Windows Command Prompt. The standard prompt does not have the paths to the Visual Studio linker and compiler, leading to errors like "cl.exe not found" when running nvcc.
Tips
To keep your system clean, consider using the "Custom" installation option. This allows you to opt out of installing components you might not need immediately, such as the documentation or specific samples, which can save several gigabytes of disk space.
When writing CUDA code, always remember that the GPU has its own separate memory from the CPU. A common point of confusion for beginners is trying to access a variable on the GPU that was only declared on the CPU. You must always use cudaMalloc to allocate memory on the device and cudaMemcpy to move data between the host (CPU) and the device (GPU).
If you plan on doing deep learning, you might not need to install the full CUDA Toolkit manually. Many libraries like PyTorch or TensorFlow come with their own bundled CUDA binaries, which simplifies the process significantly. However, for custom C++ GPU programming, the full toolkit is essential.
Troubleshooting
If nvcc --version returns a "command not found" error, the first step is to check your system environment variables. Ensure that the bin folder of your CUDA installation (e.g., C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.x\bin) is listed in the Path variable. After adding it, you must close and reopen any active command prompt windows for the change to take effect.
If you encounter a "CUDA driver version is insufficient" error when running your program, this means your installed GPU driver is older than the version required by the CUDA Toolkit you installed. Update your drivers to the latest version available from NVIDIA.
If the program compiles but crashes or returns incorrect values, check your memory management. Ensure every cudaMalloc has a corresponding cudaFree. Additionally, check if you are using the correct memory transfer direction in cudaMemcpy (e.g., cudaMemcpyDeviceToHost when moving results back to the CPU).
If Visual Studio is installed but nvcc still cannot find the compiler, try reinstalling the "Desktop development with C++" workload. Ensure that the "MSVC v14x - VS 20xx C++ x64/x86 build tools" component is specifically checked in the individual components tab of the Visual Studio Installer.
Frequently Asked Questions
Do I need a specific version of CUDA for my GPU?
Yes, your GPU architecture (e.g., Pascal, Ampere, Ada Lovelace) determines the minimum CUDA version you can use. Newer GPUs require newer toolkits to unlock their full feature set and performance capabilities.
Can I install multiple versions of the CUDA Toolkit?
Yes, you can install multiple versions side-by-side. You can switch between them by changing the CUDA_PATH environment variable and updating your system Path to point to the desired version's bin folder.
Will installing CUDA slow down my gaming performance?
No, the CUDA Toolkit is a set of development tools and libraries. It does not affect the performance of your GPU during gaming; it simply provides the means for software to utilize the GPU for non-gaming calculations.
What is the difference between the CUDA Driver and the CUDA Toolkit?
The driver is the software that allows Windows to communicate with the GPU hardware. The toolkit is a set of tools, including the nvcc compiler and libraries (like cuBLAS), used by developers to write and compile GPU-accelerated software.
Do I need to reboot after installation?
While not always strictly required, rebooting is highly recommended. This ensures that all new environment variables are recognized by the system and that the new driver is fully initialized.