1.安装驱动
1.执行lspci | grep -i nvidia
命令查看当前显卡型号
[root@centos79-temp install]# lspci | grep -i nvidia
00:0a.0 VGA compatible controller: NVIDIA Corporation Device 2204 (rev a1)
- 进入PCI devices (ucw.cz)网站,输入上一步的显示的数字2204,点击搜索
image.png
可以看到对应的显卡是GeForce RTX 3090
image.png
- 进入nvidia官网
https://www.nvidia.cn/Download/index.aspx?lang=cn -
选择对应的显卡驱动,并下载
选择显卡驱动 - 禁用nouveau
nouveau是一个第三方开源的Nvidia驱动,一般Linux安装的时候默认会安装这个驱动。 这个驱动会与Nvidia官方的驱动冲突,在安装Nvidia驱动和和CUDA之前应先禁用nouveau
lsmod | grep nouveau
查看系统是否正在使用nouveau,如果有输出,则按以下命令禁用:
#新建一个配置文件
vi /etc/modprobe.d/blacklist-nouveau.conf
#写入以下内容
blacklist nouveau
options nouveau modeset=0
#备份当前的镜像
mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak
#建立新的镜像
dracut /boot/initramfs-$(uname -r).img $(uname -r)
#重启
reboot
#最后输入上面的命令验证
lsmod | grep nouveau
- 执行安装
# 赋执行权限
chmod +x NVIDIA-Linux-x86_64-525.89.02.run
# 执行安装命令
./NVIDIA-Linux-x86_64-525.89.02.run -no-x-check -no-nouveau-check -no-opengl-files
-no-x-check
:安装驱动时关闭X服务
-no-nouveau-check
:安装驱动时禁用nouveau
-no-opengl-files
:只安装驱动文件,不安装OpenGL文件
- 测试是否安装成功
执行nvidia-smi
命令,如果执行输出以下信息,则表示安装成功了
[root@centos79-temp install]# nvidia-smi
Tue Mar 7 15:10:54 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.89.02 Driver Version: 525.89.02 CUDA Version: 12.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. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:00:0A.0 Off | N/A |
| 35% 30C P0 N/A / 350W | 0MiB / 24576MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
到此,宿主机的NVIDIA驱动就安装完毕了,如果你不使用docker来运行程序,现在已经可以正常使用了。
可能遇到的错误:
- 宿主机未安装
gcc
ERROR: Unable to find the development tool `cc` in your path; please make sure that you have the package 'gcc' installed. If gcc is installed on your system, then please check that `cc` is in your PATH.
执行 yum install gcc gcc-c++ -y
安装即可
- 缺少
kernel-devel
ERROR: Unable to find the kernel source tree for the currently running kernel. Please make sure you have installed the kernel source files for your kernel and that they are properly configured; on
Red Hat Linux systems, for example, be sure you have the 'kernel-source' or 'kernel-devel' RPM installed. If you know the correct kernel source files are installed, you may specify the
kernel source path with the '--kernel-source-path' command line option.
执行 yum update && yum install kernel-devel -y
安装即可,有时候安装完还是报错,那就要看内核是否一致。
yum info kernel-devel kernel-headers
和 uname -r
对比一下内核版本,内核更新之后记得重启一下服务器。
kernel-devel下载: https://pkgs.org/download/kernel-devel
kernel-headers下载: https://pkgs.org/download/kernel-headers
[root@localhost home]# ls
kernel-devel-3.10.0-1160.el7.x86_64.rpm kernel-headers-3.10.0-1160.el7.x86_64.rpm NVIDIA-Linux-x86_64-535.146.02.run
# 安装包
[root@localhost home]# rpm -ivh *.rpm --nodeps --force
准备中... ################################# [100%]
正在升级/安装...
1:kernel-headers-3.10.0-1160.el7 ################################# [ 50%]
2:kernel-devel-3.10.0-1160.el7 ################################# [100%]
[root@i-hekarfs5 packages]# yum info kernel-devel kernel-headers
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.aliyun.com
已安装的软件包
名称 :kernel-devel
架构 :x86_64
版本 :3.10.0
发布 :1160.el7
大小 :38 M
源 :installed
来自源:updates
简介 : Development package for building kernel modules to match the kernel
网址 :http://www.kernel.org/
协议 : GPLv2
描述 : This package provides kernel headers and makefiles sufficient to build modules
: against the kernel package.
名称 :kernel-headers
架构 :x86_64
版本 :3.10.0
发布 :1160.el7
大小 :3.8 M
源 :installed
来自源:updates
简介 : Header files for the Linux kernel for use by glibc
网址 :http://www.kernel.org/
协议 : GPLv2
描述 : Kernel-headers includes the C header files that specify the interface
: between the Linux kernel and userspace libraries and programs. The
: header files define structures and constants that are needed for
: building most standard programs and are also needed for rebuilding the
: glibc package.
[root@i-hekarfs5 packages]# uname -r
3.10.0-1160.el7.x86_64
2.安装 nvidia-docker-runtime
1.根据官方文档,执行安装命令
Migration Notice | nvidia-container-runtime
centos7 的添加方式为:
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.repo | \
sudo tee /etc/yum.repos.d/nvidia-container-runtime.repo
ubuntu的添加方式为:
curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | \
sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list | \
sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
- 执行安装
yum install nvidia-container-runtime
- 运行docker容器测试
docker run -it --rm --gpus all centos nvidia-smi
果然,不出意外的情况下,就要出意外了:
[root@centos79-temp install]# docker run -it --rm --gpus all centos nvidia-smi
docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]].
原因是没有重启docker,执行systemctl restart docker
命令,再次测试就成功了
[root@centos79-temp install]# docker run -it --rm --gpus all centos nvidia-smi
Tue Mar 7 07:19:23 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.89.02 Driver Version: 525.89.02 CUDA Version: 12.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. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:00:0A.0 Off | N/A |
| 35% 30C P0 N/A / 350W | 0MiB / 24576MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+