How to Install CUDA Toolkit on Linux Mint 21 or 20

For developers aiming to harness the power of GPU-accelerated applications, the Nvidia CUDA Toolkit is an indispensable resource. This comprehensive suite, tailored for high-performance GPU programming, is compatible with Linux Mint 21 and the older stable release of Linux Mint 20. This guide will walk you through how to install the NVIDIA Toolkit on these Linux Mint versions, ensuring you have the tools needed to create efficient GPU-driven applications.

The Nvidia CUDA Toolkit stands out for its array of features:

  • GPU-Accelerated Libraries: These specialized libraries simplify common tasks like linear algebra and Fast Fourier Transform (FFT), allowing developers to focus on higher-level code without sacrificing performance.
  • Nvidia Performance Primitives (NPP): This set of GPU-accelerated functions tailored for image and video processing offers a faster alternative to traditional methods.
  • CUDA Profiler: A tool that provides insights into how CUDA applications perform, helping developers fine-tune their code for optimal GPU usage.
  • CUDA-GDB: An enhanced version of the well-known GNU Debugger, this tool has been adapted to support CUDA, making debugging both GPU and CPU code easier.
  • Nsight Systems and Nsight Compute: These tools offer in-depth performance metrics and API debugging for CUDA applications, aiding development and optimization.

Whether you’re just starting with CUDA or a seasoned developer, the Nvidia CUDA Toolkit offers a robust environment for GPU programming. By the end of this guide, you’ll have a clear path to installing the toolkit on Linux Mint 21 or Linux Mint 20, setting the stage for efficient and powerful GPU-driven development.

Remove Pre-existing CUDA NVIDIA Installations From Linux Mint

This is only required if you have installed some form of CUDA or NVIDIA drivers. This is very situational. This section can be skipped for users on a fresh system with no NVIDIA drivers installed.

It’s pivotal to establish a fresh foundation while initiating the NVIDIA drivers’ installation process or even considering version upgrades. Essentially, it involves purging your Linux Mint system of existing NVIDIA installations to eschew possible hitches that could result from superimposed installations.

Step 1: Remove NVIDIA Packages Installed via APT on Linux Mint

In situations where you’ve utilized the APT package manager to set up NVIDIA drivers, a solitary command aids in deleting all NVIDIA-associated packages, leading to a complete wipeout of NVIDIA from your system. To accomplish this, key in the following command in your terminal:

sudo apt autoremove cuda* nvidia* --purge

This command incorporates the autoremove function of the apt command, purging packages that were autonomously installed to meet dependencies for distinct packages, which have now become superfluous. The nvidia* pattern locates all packages that commence with ‘nvidia’. The –purge option instructs apt to eliminate the packages and their associated configuration files.

Step 2: Removal of NVIDIA Drivers Set up via Runfile on Linux Mint

In scenarios where NVIDIA drivers were configured utilizing a .run file (usually not the preferred method due to the availability of superior alternatives like the NVIDIA CUDA repository), a distinctive methodology is required for its removal.

Execute the following command to delete the runfile type of installation:

sudo /usr/bin/nvidia-uninstall

This command sets in motion the nvidia-uninstall script, incorporated within the runfile installation. This script aims to remove the NVIDIA driver configured via the runfile systematically.

Step 3: Remove the CUDA Toolkit Installed via Runfile on Linux Mint

If you’ve set up the CUDA toolkit via a runfile, it is also necessary to eliminate this. The process mirrors the method used for the NVIDIA drivers’ removal. To eradicate the CUDA toolkit, carry out the following command:

sudo /usr/local/cuda-X.Y/bin/cuda-uninstall

In this command, substitute X.Y with the version number of your installed CUDA toolkit. This command activates the cuda-uninstall script, which is part of the runfile installation of the CUDA toolkit. The script is engineered to purge the CUDA toolkit from your system systematically.

Import NVIDIA APT Repository and Install CUDA on Linux Mint 21 or 20

The instructions in this section are tailored for Linux Mint 21 based on Ubuntu 22.04 and Linux Mint 20 based on Ubuntu 20.04 LTS versions. Please note that this procedure doesn’t apply to short-term releases.

Installing CUDA directly from the NVIDIA CUDA official APT repository is recommended. This is because it offers an efficient path to obtain immediate updates, such as new enhancements, bug fixes, security patches, or features.

Step 1: Preparing the Linux Mint System

Before starting the installation, ensuring your system is aptly prepared is essential. This step involves installing several prerequisite packages. Though some of these packages might already be in your system, it’s prudent to verify. Execute the following command in your terminal:

sudo apt install build-essential gcc dirmngr ca-certificates software-properties-common apt-transport-https dkms curl -y

This command sets up several necessary packages that will be instrumental for the subsequent steps. Among them are dirmngr (a key manager), ca-certificates (manages SSL certificates), software-properties-common (for controlling software repositories), apt-transport-https (facilitates secure package downloads), dkms (helps manage kernel modules), and curl (enables downloading files from the internet).

Step 2: Authenticating Installation with the NVIDIA Repository GPG Key

Software installation demands paramount security. To guarantee the genuineness and integrity of the packages to be installed, it’s important to import the GPG key for your specific Linux Mint version. This key serves to sign the packages in the repository, and by importing it, we essentially instruct our system to trust packages signed with this key.

Execute the following command for Linux Mint 21 based on Ubuntu 22.04 Jammy Jellyfish release:

curl -fSsL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub | sudo gpg --dearmor | sudo tee /usr/share/keyrings/nvidia-drivers.gpg > /dev/null 2>&1

For Linux Mint 20 based on Ubuntu 20.04 Focal Fossa release, use this command:

curl -fSsL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub | sudo gpg --dearmor | sudo tee /usr/share/keyrings/nvidia-drivers.gpg > /dev/null 2>&1

Step 3: Incorporating the NVIDIA Repository into Linux Mint

With the GPG key activated, we can now integrate the NVIDIA repository into our system. This repository houses the packages needed for our CUDA installation.

For Linux Mint 21 based on Ubuntu 22.04 Jammy Jellyfish release, use the following command:

echo 'deb [signed-by=/usr/share/keyrings/nvidia-drivers.gpg] https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /' | sudo tee /etc/apt/sources.list.d/nvidia-drivers.list

For Linux Mint 20 based on Ubuntu 20.04 Focal Fossa release, use this command:

echo 'deb [signed-by=/usr/share/keyrings/nvidia-drivers.gpg] https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /' | sudo tee /etc/apt/sources.list.d/nvidia-drivers.list

Step 4: Updating the Package List

Updating our system’s package list with the NVIDIA repository added is essential. This step is crucial to ensure our system acknowledges the new packages present in the NVIDIA repository. Execute the following command to do so:

sudo apt update

This command fetches the most recent package information from all configured repositories, including the freshly added NVIDIA repository.

Step 5: Install CUDA and Nvidia Drivers on Linux Mint 21 or 20

We can set up CUDA and the latest NVIDIA drivers with all preconditions met. However, it’s advisable to verify the available driver versions before installation. This can be achieved by using the APT search command:

apt search cuda-drivers | grep 'CUDA Driver'

The following is an example of what your output might look like:

Screenshot demonstrating the search process for NVIDIA CUDA drivers on Linux Mint.
A visual guide on searching and locating NVIDIA CUDA drivers for Linux Mint.

This command displays all available CUDA versions. Choose the version that aligns best with your requirements. This guide will demonstrate how to install the currently available latest version.

Remember to replace 545 with 535, 530, 525, 520, 515, etc., depending on your preference.

sudo apt install nvidia-driver cuda-drivers-545 cuda

Upon successful installation, it’s important to reboot your system:

sudo reboot

This ensures that all changes are correctly implemented, paving the way for a seamless CUDA experience on your Linux Mint system.

Create a Test Application with CUDA with Linux Mint 21 or 20

The CUDA world opens up a vast range of possibilities regarding Linux Mint. To aid you in interacting with this powerful tool, this section offers helpful guidance and valuable tips to ensure you efficiently utilize CUDA.

Understanding the GPU Capabilities

Before initiating CUDA programming, it is essential to acquaint yourself with your GPU’s potential. Different GPUs support various CUDA versions and come with a distinct number of cores, memory sizes, and other attributes. You can glean comprehensive details about your GPU by using the nvidia-smi command:

nvidia-smi

This command gives you an insight into several GPU attributes, such as its name, total memory, CUDA version, etc. Awareness of your GPU’s capabilities is instrumental in crafting efficient CUDA programs.

Crafting Your First CUDA Application

To set the ball rolling, we will create a basic CUDA application, compile it, and run it to confirm everything is working. First, create a new file with the .cu extension, the default for CUDA programs. Use a text editor of your choice for this purpose:

nano helloworld.cu

Now, add the following code to the file. This simple program outputs a “Hello, World!” message from the GPU:

#include <stdio.h>

__global__ void helloFromGPU (void) {
    printf("Hello World from GPU!\n");
}

int main(void) {
    printf("Hello World from CPU!\n");
    helloFromGPU <<<1, 10>>>();
    cudaDeviceReset();
    return 0;
}

To compile this program, we use the nvcc command (NVIDIA CUDA Compiler):

nvcc helloworld.cu -o helloworld

Finally, run the compiled program:

./helloworld

Your output should read:

Hello World from CPU!
Hello World from GPU!

Example of Matrix Multiplication with CUDA

As an advanced example, let’s execute a program that performs matrix multiplication using CUDA. This example demonstrates how CUDA can be employed to execute highly parallel tasks.

nano matrixmul.cu

Now, add the following code to the matrixmul.cu file. This program performs matrix multiplication on the GPU:

__global__ void gpu_matrix_mult(int *a,int *b, int *c, int m, int n, int k)
{ 
    // calculate the row & column index of the element
    int row = blockIdx.y * blockDim.y + threadIdx.y; 
    int col = blockIdx.x * blockDim.x + threadIdx.x;

    int sum = 0;
    if( row < m && col < k ) 
    {
        for(int j = 0; j < n; j++) 
        {
            sum += a[row * n + j] * b[j * k + col];
        }
        // each thread computes one element of the block sub-matrix
        c[row * k + col] = sum;
    }
} 

Compile this program using the nvcc command:

nvcc matrixmul.cu -o matrixmul

Then, execute the compiled program:

./matrixmul

Your output should display the result of the matrix multiplication operation. This is an example of how CUDA can perform highly parallel tasks like matrix multiplication, which significantly improves performance compared to sequential CPU execution.

Conclusion

We’ve journeyed through the essential steps required to install CUDA on Linux Mint 21 or 20. From understanding GPU capabilities using the nvidia-smi command to creating and compiling simple CUDA applications, this guide provides a comprehensive roadmap to leverage your GPU’s power for general-purpose computing fully. As CUDA exposes a wide range of possibilities, it’s vital to remember that the examples we’ve gone through serve as a starting point for your explorations.

Armed with this knowledge, you’re well-equipped to take on more complex tasks, further optimizing your programs to extract maximum performance from your GPU. As a final recommendation, continue building on these basics, ensuring your applications tap into the full potential of the GPU. And always remember, it’s a journey of constant learning and improvement.

3 thoughts on “How to Install CUDA Toolkit on Linux Mint 21 or 20”

  1. I had to change:
    sudo apt install nvidia-driver cuda-drivers-550 cuda
    to
    sudo apt install nvidia-driver-550 cuda-drivers-550 cuda

    Looks like they changed the ‘nvidia-driver’ package to ‘nvidia-driver-550’

    Reply

Leave a Comment