【ubuntu20.04安装nvidia显卡驱动及pytorch】
ubuntu20.04安装nvidia显卡驱动及pytorch
硬件接线
显卡为5060Ti-16G。
先电脑断电,显卡插到主板卡槽上,插好了的话卡扣会自动扣上,拧好机箱上固定显卡的螺丝,插电源线就可以开机了。
注意:
1、电源线一定要是电池自带的,不能混用。
2、如果电脑开机黑屏,把显示器的线接到显卡上。
安装驱动,自动安装
1、确认型号:
lspci|grep-invidia输出
01:00.0 VGA compatible controller: NVIDIA Corporation Device 2d04(rev a1)01:00.1 Audio device: NVIDIA Corporation Device 22eb(rev a1)如果自动安装驱动:sudo ubuntu-drivers autoinstall
会显示:No drivers found for installation.
需要更新源:
sudoadd-apt-repository ppa:graphics-drivers/ppasudoaptupdate2、安装驱动
查看推荐版本
ubuntu-drivers devices==/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0==modalias:pci:v000010DEd00002D04sv00001771sd0000205Ebc03sc00i00 vendor:NVIDIA Corporation driver:nvidia-driver-570 - third-party non-free driver:nvidia-driver-570-open - third-party non-free driver:nvidia-driver-580-open - third-party non-free recommended driver:nvidia-driver-580 - third-party non-free driver:xserver-xorg-video-nouveau - distrofreebuiltin安装:
sudoaptinstallnvidia-driver-580-open等待一会,完成后重启。
如果重启发现字特别小,则修改显示设置。
在桌面,右键修改显示设置:打开终端,查看显卡:
nvidia-smiTue Apr710:06:572026+-----------------------------------------------------------------------------------------+|NVIDIA-SMI580.126.09 Driver Version:580.126.09 CUDA Version:13.0|+-----------------------------------------+------------------------+----------------------+|GPU Name Persistence-M|Bus-Id Disp.A|Volatile Uncorr. ECC||Fan Temp Perf Pwr:Usage/Cap|Memory-Usage|GPU-Util Compute M.||||MIG M.||=========================================+========================+======================||0NVIDIA GeForce RTX5060Ti Off|00000000:01:00.0 On|N/A||0% 40C P5 8W / 180W|953MiB / 16311MiB|31% Default||||N/A|+-----------------------------------------+------------------------+----------------------+ +-----------------------------------------------------------------------------------------+|Processes:||GPU GI CI PID Type Process name GPU Memory||ID ID Usage||=========================================================================================||0N/A N/A1615G /usr/lib/xorg/Xorg 325MiB||0N/A N/A1932G /usr/bin/gnome-shell 156MiB||0N/A N/A12911G...l/sunlogin/bin/sunloginclient 9MiB||0N/A N/A49731G /usr/lib/firefox/firefox 380MiB|+-----------------------------------------------------------------------------------------+可以看到cuda的版本13.0。
安装CUDA Toolkit
如果未安装会显示:
nvcc--versionCommand'nvcc'not found, but can be installed with:sudoaptinstallnvidia-cuda-toolkit安装:
wgethttps://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pinsudocpcuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600wgethttps://developer.download.nvidia.com/compute/cuda/12.8.0/local_installers/cuda-repo-ubuntu2004-12-8-local_12.8.0-570.86.10-1_amd64.debsudodpkg-icuda-repo-ubuntu2004-12-8-local_12.8.0-570.86.10-1_amd64.debsudocp/var/cuda-repo-ubuntu2004-12-8-local/cuda-600F024F-keyring.gpg /usr/share/keyrings/sudoapt-getupdatesudoapt-get-yinstallcuda-toolkit-12-8临时添加path:
exportPATH=/usr/local/cuda-12.8/bin:$PATHnvcc--version输出:
nvcc: NVIDIA(R)Cuda compiler driver Copyright(c)2005-2025 NVIDIA Corporation Built on Wed_Jan_15_19:20:09_PST_2025 Cuda compilation tools, release12.8, V12.8.61 Build cuda_12.8.r12.8/compiler.35404655_0安装pytorch
查看网页,找适配的安装版本和语句。
激活python虚拟环境:
source/home/ubuntu/virtualenv/df_env/bin/activate安装:
pip3installtorch torchvision --index-url https://download.pytorch.org/whl/cu128测试
#!/usr/bin/env python3# -*- coding: utf-8 -*-importtorchimportsysdefmain():print("="*50)print("GPU 环境验证")print("="*50)# Python 版本print(f"Python 版本:{sys.version}")# PyTorch 版本print(f"PyTorch 版本:{torch.__version__}")# CUDA 是否可用cuda_available=torch.cuda.is_available()print(f"CUDA 可用:{cuda_available}")ifcuda_available:# GPU 数量gpu_count=torch.cuda.device_count()print(f"GPU 数量:{gpu_count}")# 当前 GPU 名称current_gpu=torch.cuda.current_device()gpu_name=torch.cuda.get_device_name(current_gpu)print(f"当前 GPU:{gpu_name}")# CUDA 计算能力cap=torch.cuda.get_device_capability(current_gpu)print(f"CUDA 计算能力:{cap[0]}.{cap[1]}")# 显存信息total_mem=torch.cuda.get_device_properties(current_gpu).total_memory/1e9print(f"总显存:{total_mem:.2f}GB")# 简单张量运算测试print("\n执行 GPU 张量运算测试...")try:x=torch.randn(1000,1000).cuda()y=torch.randn(1000,1000).cuda()z=x @ y# 矩阵乘法print(f"运算成功,结果形状:{z.shape}")print(f"结果设备:{z.device}")print("✅ GPU 工作正常")exceptExceptionase:print(f"❌ GPU 运算失败:{e}")else:print("⚠️ CUDA 不可用,PyTorch 将使用 CPU 运行")print("可能的原因:")print("1. 未安装 NVIDIA 驱动")print("2. PyTorch 版本未包含 CUDA 支持(请安装 cu118/cu124/cu126 等版本)")print("3. 驱动与 CUDA 版本不兼容")# 可选:测试 CPU 与 GPU 速度对比ifcuda_available:print("\n性能对比 (CPU vs GPU):")importtime size=5000a=torch.randn(size,size)b=torch.randn(size,size)# CPUstart=time.time()c_cpu=a @ b cpu_time=time.time()-start# GPUa_gpu=a.cuda()b_gpu=b.cuda()torch.cuda.synchronize()start=time.time()c_gpu=a_gpu @ b_gpu torch.cuda.synchronize()gpu_time=time.time()-startprint(f"CPU 时间:{cpu_time:.4f}秒")print(f"GPU 时间:{gpu_time:.4f}秒")print(f"加速比:{cpu_time/gpu_time:.2f}x")print("\n"+"="*50)print("验证完成")print("="*50)if__name__=="__main__":main()