How to Install NVIDIA CUDA Toolkit on Ubuntu 22.04?
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
Is separate installation of cuDNN required?
Yes! If you are using, for example, TensorFlow or PyTorch then it is very highly recommended that you install cuDNN. It will improve the speed of computations completed using the deep-learning framework.
Does CUDA work with Secure Boot enabled?
Normally not! You will have to either disable Secure Boot or manually sign kernel modules before it works.
Can I have multiple versions of CUDA installed side-by-side?
Yes!, though to manage this properly you will need to be very careful with paths. Environment modules or Docker are useful for easily managing CUDA versions.
What is the easiest way to uninstall CUDA?
If you used .run installation, simply run the uninstall script. If you installed using apt, run:
sudo apt-get --purge remove "cuda*"