how to install cuda on ubuntu
- User
how to install cuda on ubuntu
- Assistant
how to install cuda on ubuntu
To install CUDA (Compute Unified Device Architecture) on Ubuntu, you'll need to perform several steps, including verifying your GPU compatibility, installing the NVIDIA drivers, installing the CUDA Toolkit, and setting up the environment variables. Here's a comprehensive guide to help you through the process:
Before proceeding, ensure that your system has an NVIDIA GPU that supports CUDA.
Check for an NVIDIA GPU:
Open a terminal and run:
lspci | grep -i nvidia
If you see output related to NVIDIA Corporation, your system has an NVIDIA GPU.
Confirm CUDA Support:
Visit the NVIDIA CUDA-Enabled GPUs page to confirm that your GPU model supports CUDA.
It's essential to ensure your system is up-to-date before installing new software.
sudo apt update
sudo apt upgrade -y
Installing the correct NVIDIA driver is crucial for CUDA to function properly.
Identify Available Drivers:
ubuntu-drivers devices
This command lists the recommended drivers for your GPU.
Install the Recommended Driver:
Replace nvidia-driver-xxx with the recommended driver version (e.g., nvidia-driver-470):
sudo apt install nvidia-driver-xxx
Add the Graphics Drivers PPA:
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
Install the Latest Driver:
sudo apt install nvidia-driver-xxx
Replace nvidia-driver-xxx with the latest driver version suitable for your GPU.
Blacklisting Nouveau (Optional):
If you encounter issues with the open-source Nouveau driver interfering, you may need to blacklist it.
echo -e "blacklist nouveau\noptions nouveau modeset=0" | sudo tee /etc/modprobe.d/blacklist-nouveau.conf
sudo update-initramfs -u
After installing the NVIDIA driver, reboot your system to load the new driver.
sudo reboot
After rebooting, confirm that the NVIDIA driver is installed correctly.
nvidia-smi
You should see a table displaying your GPU details, driver version, and CUDA version.
Add the CUDA Repository Key:
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu$(lsb_release -sr | sed -e 's/\.//')/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
Install CUDA Toolkit:
sudo apt-get install -y cuda
Visit the CUDA Toolkit Download Page:
Go to the NVIDIA CUDA Toolkit Download page.
Select Your Configuration:
deb (local) or deb (network)Follow the Instructions Provided:
NVIDIA provides specific commands based on your selections. For example:
Download the CUDA Repository Package:
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda-repo-ubuntu2204-11-8-local_11.8.0-520.61.05-1_amd64.deb
Install the CUDA Repository Package:
sudo dpkg -i cuda-repo-ubuntu2204-11-8-local_11.8.0-520.61.05-1_amd64.deb
Add the Repository Key:
sudo cp /var/cuda-repo-ubuntu2204-11-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
Install CUDA Toolkit:
sudo apt-get update
sudo apt-get install -y cuda
Note: Replace version numbers with the ones provided on the NVIDIA website relevant to your installation.
You need to add CUDA's bin and lib64 directories to your system's PATH and LD_LIBRARY_PATH.
Open Your Shell Configuration File:
If you're using Bash:
nano ~/.bashrc
For Zsh:
nano ~/.zshrc
Add the Following Lines at the End of the File:
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
Save and Exit:
Ctrl + O to save, then Enter, and Ctrl + X to exit.Source the Configuration File to Apply Changes:
For Bash:
source ~/.bashrc
For Zsh:
source ~/.zshrc
nvcc --version
You should see the CUDA compiler version information.
Copy CUDA Samples to Your Home Directory:
cuda-install-samples-$(uname -i).sh ~
Compile and Run the Device Query Sample:
cd ~/NVIDIA_CUDA-*/Samples/1_Utilities/deviceQuery
make
./deviceQuery
Check the Output:
The sample should display detailed information about your GPU. Look for the line Result = PASS to confirm a successful installation.
While not always necessary, rebooting ensures all changes take effect.
sudo reboot
nvcc Not Found:
Ensure your PATH variable includes the CUDA bin directory and that you've sourced your shell configuration file.
Driver Compatibility Issues:
Ensure that the NVIDIA driver version is compatible with the CUDA Toolkit version. Refer to the CUDA Compatibility Guide for details.
Conflicts with Nouveau Driver:
If you encounter issues due to the Nouveau driver, ensure it's blacklisted (see Step 3).
By following these steps, you should have successfully installed CUDA on your Ubuntu system. You can now develop and run CUDA applications, leveraging the parallel computing power of NVIDIA GPUs.
Not yet included in a published release.