How to Install NVIDIA CUDA Toolkit on Ubuntu 22.04?
You can install the NVIDIA CUDA Toolkit on Ubuntu 22.04 by installing the correct NVIDIA driver first, then installing CUDA via the .run file or apt, and finally setting the environment variables. Here’s the full process:
-
- Verify your GPU is CUDA-capable (
lspci | grep -i nvidia) - Remove old NVIDIA drivers
- Install the recommended NVIDIA driver and reboot
- Install CUDA via
.runfile orapt - Set
PATHandLD_LIBRARY_PATHenvironment variables - Verify with
nvcc --version
- Verify your GPU is CUDA-capable (
Introduction
Are you willing to kickstart your journey into AI, or machine learning? The demand and scope of advanced technologies like artificial intelligence and machine learning has increased to a great extent along with the usage of Graphics Unit (GPU). Such a remarkable growth is observed because of the progress of CUDA (Compute Unified Device Architecture).
However, the whole process of installing and getting CUDA up and running on Ubuntu 22.04 can be cumbersome if it is your first time! There are tons of different installers you might be dealing with, conflicts amongst drivers, incorporating paths for the CUDA installation, or you may just not be able to boot the Ubuntu OS altogether. Don’t worry, we’re here to guide you through all the steps. Let’s get your system ready to go for CUDA installation on Ubuntu 22.04!
What is CUDA?
CUDA (Compute Unified Device Architecture) is a parallel computing platform and programming model developed by NVIDIA. It facilitates software developers to seek advantage of NVIDIA GPUs and carry out general purpose processing efficiently.
Although they were originally meant for graphics, GPUs are great at computing tasks that can be broken up into smaller pieces like matrix multiplication, image analysis, and training a neural network. CUDA provides access to that computing capability from general purpose programming.
Benefits of Using CUDA
Training deep learning models with TensorFlow and PyTorch
Speeding up simulations in physics, biology, and engineering
Real-time image and video processing
Crypto-mining
Conducting parallel computations in scientific studies
Now, you are aware of what CUDA is and why it’s so powerful, the next step is to get it installed and running on Ubuntu 22.04.
Prerequisites
Before starting the installation process, you will need:
- A system with a CUDA-capable NVIDIA GPU
- A system with Ubuntu 22.04 LTS installed
- Internet access
- Basic knowledge of terminals
- Disable Secure Boot in BIOS
Steps to Install NVIDIA CUDA Toolkit on Ubuntu 22.04
Step 1: Verify Your GPU and OS
Now let’s check if your system is compatible.
Check GPU:
lspci | grep -i nvidia
Check OS version:
lsb_release -a
Verify the model of your GPU is on the list of CUDA-enabled GPUs provided by NVIDIA.
Step 2: Remove Old NVIDIA Drivers
Run the following command:
sudo apt-get --purge remove "*nvidia*" sudo apt autoremove
Step 3: Install NVIDIA driver
1. Adding the graphics PPA.
sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt update
2. Install the recommended driver.
sudo ubuntu-drivers autoinstall
3. Reboot it.
sudo reboot
4. Check if it worked.
nvidia-smi
You should see your GPU details and driver version.
Step 4: Install CUDA Toolkit
You have two paths you can take. First, if you would like to go with the latest version and have more control of the install process, go for installation using the .run file. If you simply want to get it done quickly, or prefer a simpler method, install using apt.
Option 1: Install using .run File
Go to CUDA Downloads
Then, select the following:
Operating System: Linux
Architecture: x86_64
Distribution: Ubuntu
Version: 22.04
Download a .run file, and execute the following command:
chmod +x cuda__linux.run sudo ./cuda__linux.run
Option 2: Install via apt
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600 sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /" sudo apt update sudo apt install cuda
Step 5: Configure Environment Variables
Open your .bashrc or .zshrc file:
nano ~/.bashrc
At the bottom of the file, add these lines:
export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
Now apply the changes:
source ~/.bashrc
Step 6: Check CUDA Installation
Confirm that CUDA was installed properly.
nvcc --version
You should see the CUDA version printed out.
Run a sample test:
cuda-install-samples-<version>.sh ~ cd ~/NVIDIA_CUDA-<version>/samples make -j$(nproc) cd bin/x86_64/linux/release ./deviceQuery
If you see the result as a pass, then you’re good to go with it!
Final Thoughts
And there you go, your Ubuntu 22.04 system is now CUDA-enabled! Whether you are training deep learning models or running GPU-accelerated simulations, you now have the CUDA installed for your Ubuntu system.
Frequently Asked Questions
Do I need to install cuDNN separately after installing CUDA?
Yes. If you’re using TensorFlow or PyTorch, installing cuDNN separately is strongly recommended – it significantly speeds up deep learning computations on top of CUDA.
Can I install CUDA without disabling Secure Boot?
Not by default. You’ll need to either disable Secure Boot in BIOS or manually sign the NVIDIA kernel modules for CUDA to work with Secure Boot enabled.
Can I install multiple CUDA versions on the same Ubuntu system?
Yes, multiple CUDA versions can coexist, but you need to manage PATH and LD_LIBRARY_PATH carefully. Using environment modules or Docker containers makes version-switching much easier.
How do I completely uninstall CUDA from Ubuntu?
If installed via the .run file, run the built-in uninstall script. If installed via apt, run:
sudo apt-get --purge remove "cuda*"