当前位置: 首页 > news >正文

【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/ppasudoaptupdate

2、安装驱动
查看推荐版本

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-smi
Tue 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--version
Command'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()
http://www.cnnetsun.cn/news/1784143.html

相关文章:

  • 2026 答辩季实测:PaperXie AI PPT 生成器,把毕业论文答辩 PPT 从 “熬夜噩梦” 变成 “一键惊艳”
  • AICoverGen语音转换全攻略:从基础搭建到创意实践
  • 建筑行业企业大数据可视化大屏系统源码(Vue3+DataV架构)
  • 7个实用技巧:Ryujinx模拟器从入门到精通
  • 一图看懂 Windows 目录结构:C 盘这些文件夹到底是干什么的?
  • 3个核心功能让ASMR爱好者一键构建个人音频库:asmr-downloader技术解析
  • 互联网行业自动化平台选型,运营全流程提效指南:2026企业级智能体架构与实战全解析
  • 科技中介如何借助数字化工具提升服务的专业性与覆盖面?
  • Pretext:值得关注的文本排版引擎劝
  • 如何快速清理Windows 11系统垃圾:免费工具Win11Debloat完整使用指南
  • 4个效率倍增技巧:D3KeyHelper让暗黑3操作自动化更精准
  • 微信对接OpenClaw的常见问题和解决方案盘
  • iperf3实战指南:解决网络性能瓶颈的测试方法论
  • Sketch Measure:设计协作效率引擎的5大核心场景与实战指南
  • 企业级管理系统的架构设计与实战指南
  • Mysql的行级锁到底是怎么加的?讲
  • 深入浅出:图解OV13850 Sensor驱动中的曝光、增益与消隐时间
  • 【数字工厂合集】1300余份数字工厂、工业互联网、AI智能工厂、PLM\MES\SCADA\MOM\APS\WMS\ERP等系统方案报告合集
  • Simulink仿真避坑指南:如何设置步长、powergui和模块采样时间才能让控制周期更稳定
  • 揭秘农田传感器数据暴增下的PHP实时渲染瓶颈:3步实现毫秒级ECharts动态可视化
  • 告别踩坑:基于STM32Duino的Arduino开发环境一站式搭建与实战排错指南
  • 从接线到解析:手把手教你用SocketCAN在Linux下调试ARS408毫米波雷达(附C++代码)
  • 打造沉浸式智能AI问答助手:Vue + UniApp 全端实战(支持 Markdown/公式/多模态交互)破
  • 说实话,Pop!_OS 真的不太适合 Linux 初学者
  • 3个核心功能:重新定义英雄联盟游戏体验的智能助手
  • 四足机器人控制入门指南:从仿真到算法的完整实践
  • 127.0.0.2地址是用来干啥的?
  • 如何用wxhelper实现高效PC微信自动化开发:从原理到实战指南
  • 5个高效步骤:Win11Debloat让Windows系统臃肿问题迎刃而解
  • 猫抓浏览器插件:轻松下载网页视频资源的终极解决方案