CUDA version 10 is available in the official package repository of Ubuntu 20.04 LTS. ... To confirm the installation, press Y and then press . All the ...
ThefullformofCUDAisComputeUnifiedDeviceArchitecture.CUDAisaparallelcomputingplatformandprogrammingmodeldevelopedbyNVIDIA.ItisusedtorunprogramsontheNVIDIAGraphicsProcessingUnits(GPUs)tospeedupcomputingapplicationsdramatically.
Inthisarticle,IwillshowyouhowtoinstallCUDAonUbuntu20.04LTSfromtheofficialpackagerepositoryofUbuntu.IwillalsoshowyouhowtoinstallthelatestversionofCUDAonUbuntu20.04LTSfromtheofficialNVIDIApackagerepository.Iwillshowyouhowtowrite,compile,andrunyourveryfirstCUDAprogramaswell.So,let’sgetstarted.
TableofContents
Pre-requisites
UpdatingPackageRepositoryCache
InstallingGCCandOtherBuildTools
InstallingCUDAfromtheOfficialUbuntuPackageRepository
InstallingtheLatestVersionofCUDAfromtheOfficialNVIDIAPackageRepository
WritingaHelloWorldProgramwithCUDA
Conclusion
References
Pre-requisites
Beforeyougetstarted,
i)YoumusthaveanNVIDIAGPUinstalledonyourcomputer.
ii)YoumusthavetheNVIDIAGPUdriversinstalledonyourcomputer.
ToconfirmwhethertheNVIDIAGPUdriversareworking,runthefollowingcommand:
$lsmod|grepnvidia
IftheNVIDIAdriverkernelmodulesareworking,youshouldseethefollowingoutput.
IftheNVIDIAdriversareworkingcorrectly,NVIDIAcommand-linetoolsshouldworkasexpected.
$nvidia-smi
NVIDIAgraphicaltoolsliketheNVIDIAXServerSettingsappshouldalsowork.
UpdatingPackageRepositoryCache:
Onceyouhavefulfilledalltherequirements,updatetheAPTpackagerepositorycachewiththefollowingcommand:
$sudoaptupdate
TheAPTpackagerepositorycacheshouldbeupdated.
InstallingGCCandOtherBuildTools:
ForCUDAtowork,youmusthaveGCCandotherbuildtoolsinstalledonyourcomputer.
YoucaninstallGCCandalltherequiredbuildtoolsfromtheofficialpackagerepositoryofUbuntuwiththefollowingcommand:
$sudoaptinstallbuild-essential
Toconfirmtheinstallation,pressYandthenpress.
Alltherequiredpackagesarebeingdownloaded.Itwilltakeawhiletocomplete.
Oncethepackagesaredownloaded,APTwillinstallthemonebyone.Itwilltakeawhiletocomplete.
Atthispoint,GCCandalltherequiredbuildtoolsshouldbeinstalled.
Asyoucansee,theGCCcompilerisworkingjustfine.
$gcc--version
$g++--version
InstallingCUDAfromtheOfficialUbuntuPackageRepository
CUDAversion10isavailableintheofficialpackagerepositoryofUbuntu20.04LTS.
ToinstallCUDAv10fromtheofficialpackagerepositoryofUbuntu20.04LTS,runthefollowingcommand:
$sudoaptinstallnvidia-cuda-toolkit
Toconfirmtheinstallation,pressYandthenpress.
Alltherequiredpackageswillbedownloadedfromtheinternet.Itwilltakeawhiletocomplete.
Oncetherequiredpackagesaredownloaded,theywillbeinstalledonebyone.Itwilltakeawhiletocomplete.
Atthispoint,CUDAandalltherequireddependenciesshouldbeinstalled.
ToconfirmwhetherCUDAisworking,runthefollowingcommand:
$nvcc--version
InstallingtheLatestVersionofCUDAfromtheOfficialNVIDIAPackageRepository
Atthetimeofthiswriting,CUDA11isthelatestversionofCUDA.YoucaninstallthelatestversionofCUDAfromtheofficialpackagerepositoryofNVIDIA.
First,runthefollowingcommandtomakesurethattherequiredLinuxheadersareinstalled.
$sudoaptinstalllinux-headers-$(uname-r)-y
TheLinuxkernelheadersshouldbeinstalled.Inmycase,theyarealreadyinstalled.
Now,downloadtheCUDArepositoryPinfilefromtheofficialwebsiteofNVIDIAwiththefollowingcommand:
$sudowget-O/etc/apt/preferences.d/cuda-repository-pin-600https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
ToaddtheGPGkeyoftheofficialNVIDIApackagerepository,runthefollowingcommand:
$sudoapt-keyadv--fetch-keyshttps://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
TheGPGkeyoftheofficialNVIDIApackagerepositoryshouldbeaddedtotheAPTpackagemanager.
ToaddtheofficialNVIDIACUDApackagerepository,runthefollowingcommand:
$sudoadd-apt-repository"debhttps://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64//"
TheofficialNVIDIACUDApackagerepositoryshouldbeadded.
Now,updatetheAPTpackagerepositorycachewiththefollowingcommand:
$sudoaptupdate
BeforeyouinstallthelatestversionofCUDAfromtheofficialNVIDIApackagerepository,it’sagoodideatoupdatealltheexistingpackagesofyourUbuntu20.04LTSmachine.
ToupdatealltheexistingpackagesofyourUbuntu20.04LTSmachine,runthefollowingcommand:
$sudoaptfull-upgrade
Toconfirmtheupdate,pressYandthenpress.
Alltherequiredupdatesarebeingdownloadedfromtheinternet.Itwilltakeawhiletocomplete.
Oncetheupdatesaredownloaded,theAPTpackagemanagerwillinstallthemonebyone.Itwilltakeawhiletocomplete.
Atthispoint,alltheupdatesshouldbeinstalled.
Now,youshouldbeabletoinstallthelatestversionofCUDAfromtheofficialpackagerepositoryofNVIDIAasfollows:
$sudoaptinstallcuda
Toconfirmtheinstallation,pressYandthenpress.
ThelatestversionofCUDAandalltherequireddependencypackagesshouldbedownloadedandinstalled.Itwilltakeawhiletocomplete.
Atthispoint,thelatestversionofCUDAandalltherequireddependencypackagesshouldbeinstalled.
ToaddCUDAtothepath,createanewscriptcuda.shinthe/etc/profile.d/directoryasfollows:
$sudonano/etc/profile.d/cuda.sh
Typeinthefollowinglinesinthecuda.shscript.
exportCUDA_HOME="/usr/local/cuda"
exportPATH="$PATH:$CUDA_HOME/bin"
Onceyou’redone,press+XfollowedbyYandtosavethecuda.shscript.
Forthechangestotakeeffect,rebootyourcomputerasfollows:
$sudoreboot
YoushouldbeabletoaccessCUDAtoolsasyoucanseeinthescreenshotbelow.
$nvcc--version
ForrunningsomeoftheCUDAtools,youmayneedsuperuserprivileges.Toallowsudotorunbinaryfilesfromthe/usr/local/cuda/bindirectory(wherethelatestversionofCUDAisinstalledfromtheofficialNVIDIApackagerepository)withsuperuserprivileges,youwillhavetoconfigurethe/etc/sudoersfile.
Openthe/etc/sudoersconfigurationfileforeditingwiththefollowingcommand:
$sudovisudo-f/etc/sudoers
Findthemarkedlineonthe/etc/sudoersfile.
Attheendofthesecure_path,append:/usr/local/cuda/binasmarkedinthescreenshotbelow.
Onceyou’redone,press+XfollowedbyYandtosavethe/etc/sudoersfile.
Fromnowon,ifneeded,youshouldbeabletorunCUDAtoolswithsuperuserprivilegeswithsudo.
WritingaHelloWorldProgramwithCUDA
Inthissection,IamgoingtoshowyouhowtowriteyourveryfirstCUDAprogram.TheprogramwillsimplyprintamessagefromtheGPUandamessagefromtheCPU.Ifthisprogramrunssuccessfully,youcanconfirmthatCUDAisworkingonyourcomputer.
Createanewfilehello.cuandtypeinthefollowinglinesofcodes:
NOTE:CUDAsourcefilesendwiththeextension.cu
#include
__global__voidsay_hello(){
printf("HelloworldfromtheGPU!\n");
}
intmain(){
printf("HelloworldfromtheCPU!\n");
say_hello<<>>();
cudaDeviceSynchronize();
return0;
}
Onceyou’redone,savethehello.cufileinthe~/codesdirectoryoranyotherdirectoryofyourchoice.
Navigatetothe~/codesdirectoryorthedirectorywhereyouhavesavedthehello.cufile.
$cd~/codes
Tocompilethehello.cuCUDAsourcefilewiththeCUDAcompilernvcc,runthefollowingcommand:
$nvcchello.cu-ohello
TheCUDAsourcefilehello.cushouldbecompiledwithoutanyerrorsandanewexecutable/binaryfilehelloshouldbecreatedasyoucanseeinthescreenshotbelow.
YoucanrunthecompiledCUDAprogramhelloasfollows:
$./hello
Ifyouseethefollowingoutput,thenCUDAisworkingjustfineonyourcomputer.YoushouldhavenoproblemscompilingandrunningCUDAprograms.
Conclusion
Inthisarticle,IhaveshownyouhowtoinstallCUDAonUbuntu20.04LTSfromtheofficialpackagerepositoryofUbuntu.IhavealsoshownyouhowtoinstallthelatestversionofCUDAonUbuntu20.04LTSfromtheofficialNVIDIApackagerepository.IhaveshownyouhowtowriteyourfirstCUDAprogram,compileit,andrunitaswell.
References
[1]CUDAZone|NVIDIADeveloper
[2]InstallationGuideLinux::CUDAToolkitDocumentation(nvidia.com)
[3]Tutorial01:SayHellotoCUDA–CUDATutorial(cuda-tutorial.readthedocs.io)
[4]YourFirstCUDACProgram–YouTube
[5]cudaTutorial=>Let’slaunchasingleCUDAthreadtosayhello(riptutorial.com)
Abouttheauthor
ShahriarShovon
Freelancer&LinuxSystemAdministrator.AlsolovesWebAPIdevelopmentwithNode.jsandJavaScript.IwasborninBangladesh.IamcurrentlystudyingElectronicsandCommunicationEngineeringatKhulnaUniversityofEngineering&Technology(KUET),oneofthedemandingpublicengineeringuniversitiesofBangladesh.
Viewallposts
RELATEDLINUXHINTPOSTS
HowtoInstallRustonUbuntuInstallingandWorkingwithPackagesinUbuntuPingCommandnotfoundonUbuntuLinuxHowtoInstallLibreNMSonUbuntu10BestWebcamsforUbuntuin2022HowtoInstallCUDAonUbuntu20.04LTSAccessControlListsinUbuntu