CUDA

This tutorial provides guidelines for setting up a CUDA environment on a Linux machine. Additionally, this tutorial attempts to use as little sudo as possible.

Setting up a CUDA environment for C++ development can be challenging for beginners. Yet, it is also rewarding as it will allow you to do lots of cool things like customize PyTorch and TensorFlow.

This tutorial provides a step-by-step guide for setting up a CUDA environment on a Linux machine and a Nvidia GPU. Note: The installation process will fail if you don't have a supported compiler. Make sure you have gcc, g++, ninja, and cmake installed before you install the CUDA driver.

Install CUDA Driver (Requires Sudo)

First, download the driver run file from CUDA Toolkit Downloadsarrow-up-right and install the driver accordingly.

You need to have sudo privileges to install the driver.

# Download the installation scripts
wget https://developer.download.nvidia.com/compute/cuda/12.6.2/local_installers/cuda_12.6.2_560.35.03_linux.run

# Install Driver
sudo sh cuda_12.6.2_560.35.03_linux.run --silent --driver

# Optional install driver and toolkit
# sudo sh cuda_12.3.2_545.23.08_linux.run --silent --driver --toolkit

sudo reboot

Note: The installation process will fail if you don't have a supported compiler. Make sure you have gcc, g++, ninja, and cmake installed before you install the CUDA driver.

Install CUDA Toolkit

Option I: CUDA Toolkit

I will use the CUDA 12.6.2 version as an example here.

Setting Up Environment Variables for CUDA Toolkit

Option II: HPC SDK

NVIDIA HPC SDKarrow-up-right includes (almost) everything you need for CUDA program development. We will use it to install the CUDA Toolkit along with NCCL and cuBLAS etc.

I will use the 24.9 version as an example, and you can choose any version you want. You need to use a different URL to download the SDK if your CPU is not from Intel or AMD. Check the NVIDIA HPC SDKarrow-up-right page for the details.

Setting Up Environment Variables for NVHPC

Assuming you want to use CUDA Toolkit Version 12.6, the following code snippet contains the environment variables you want to set. You just need to add them to your ~/.bashrc file and restart your terminal to set the variables correctly.

Some libraries (ex. Caffe2) will look for dependent CUDA libraries inside the CUDA_TOOLKIT_ROOT directory. Let's create a symbolic link for these math libraries such that they can be found correctly. Note that you do not need to run these scripts if your toolkit is installed through CUDA Toolkit script.

CUDNN (Optional)

CUDNN does not come with CUDA Toolkit or HPC SDK, and it needs to be installed separately. You can download the library from this websitearrow-up-right.

If your GPU is Ampere or Hopper architecture, follow the instructions on this website arrow-up-rightto install the latest version.

Last updated