操作系统环境

系统信息: Linux user-Super-Server 5.15.0-97-generic #107~20.04.1-Ubuntu SMP Fri Feb 9 14:20:11 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

驱动环境

安装包

  • cuda版本: cuda_12.2.2_535.104.05_linux.run
  • NVIDIA版本: NVIDIA-Linux-x86_64-535.154.05.run

错误备注

  • cuda安装包当中包含NVIDIA的驱动,但是不能直接安装cuda,会导致模型加载出错,必须分开安装
  • cuda12.3版本不可用,禁止安装,可能导致驱动损坏问题以及模型加载过慢问题
  • 安装之前一定保证原始环境当中没有cuda与NVIDIA的任何信息,否则可能导致安装冲突失败
  • 确认卸载后驱动挂载还会存在,需要重启电脑才能保证驱动挂载被清理掉
  • 禁止使用Ubuntu官方源进行安装

安装流程

安装NVIDIA

1、查看操作系统

{.line-numbers}
1
2
3
root@user-Super-Server:~# lsb_release -d
Description: Ubuntu 20.04.6 LTS

2、查看显卡信息

{.line-numbers}
1
2
3
4
5
6
root@user-Super-Server:~# lspci | grep VGA
03:00.0 VGA compatible controller: ASPEED Technology, Inc. ASPEED Graphics Family (rev 52)
31:00.0 VGA compatible controller: NVIDIA Corporation Device 2684 (rev a1)
4b:00.0 VGA compatible controller: NVIDIA Corporation Device 2684 (rev a1)
b1:00.0 VGA compatible controller: NVIDIA Corporation Device 2684 (rev a1)
ca:00.0 VGA compatible controller: NVIDIA Corporation Device 2684 (rev a1)

pci-ids.ucw.cz输入上面的 2684 得出当前显卡信息为:AD102 [GeForce RTX 4090]

也可以使用以下命令查看显卡更多信息

1
sudo lshw -numeric  -C display

3、安装GCC、G++、make等工具

{.line-numbers}
1
2
3
4
sudo apt-get install build-essential
# gcc --version 验证gcc是否安装成功
sudo apt-get install g++
sudo apt-get install make

安装结果如下

{.line-numbers}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@user-Super-Server:~# gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
root@user-Super-Server:~# g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
root@user-Super-Server:~# make --version
GNU Make 4.2.1
为 x86_64-pc-linux-gnu 编译
Copyright (C) 1988-2016 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第 3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律允许的范围内没有其他保证。

4、卸载nvidia旧驱动

{.line-numbers}
1
2
3
4
sudo /usr/bin/nvidia-uninstall
sudo apt-get --purge remove nvidia*
sudo apt-get purge nvidia*
sudo apt-get purge libnvidia*

直到命令不输出任何内容:sudo dpkg --list | grep nvidia-* 为保证驱动正常被取消挂载,请重启电脑后再次确认

5、根据显卡型号,下载对应的显卡驱动

下载驱动网址:Official Drivers | NVIDIA

可以选择版本下载,点击 Official Advanced Driver Search | NVIDIA根据自己的显卡型号,查找驱动,我这里选择NVIDIA-Linux-x86_64-535.154.05.run安装驱动之前,需要禁用nouveaunouveau 是专门用来逆向工程 NVIDIA 闭源驱动的开源驱动项目),因为ubuntu安装时会默认安装nouveau驱动,如果不禁用,会阻碍NVIDIA官方驱动的安装。

禁用nouveau方法如下:

{.line-numbers}
1
sudo vim /etc/modprobe.d/blacklist.conf

在文件末尾添加:

{.line-numbers}
1
2
3
4
5
blacklist nouveau
blacklist lbm‐nouveau
options nouveau modeset=0
alias nouveau off
alias lbm‐nouveau off
  • 对刚才修改的文件进行更新:sudo update-initramfs -u
  • 重启计算机:sudo reboot

查看nouveau是否禁用成功:lsmod | grep nouveau,执行完这句,如没有任何输出,表示禁用成功。

6、关闭图形界面(选择性操作)

进入tty模式(具体操作依情况而定)输入:sudo service lightdm stop

7、开始安装驱动

进入驱动所在目录,给驱动文件付权限,然后安装:

{.line-numbers}
1
2
sudo chmod +x NVIDIA-Linux-x86_64-535.154.05.run
sudo sh NVIDIA-Linux-x86_64-535.154.05.run -no-opengl-files -no-x-check -no-nouveau-check

参数说明:

  • -no-x-check:安装驱动时关闭 X 服务
  • -no-nouveau-check:安装驱动时禁用 nouveau
  • -no-opengl-files:只安装驱动文件,不安装 OpenGL 文件

操作项说明:

{.line-numbers}
1
2
3
4
5
6
Would you like to register the kernel module souces with DKMS? This will allow DKMS to automatically build a new module, if you install a different kernel later?  
选择 No 继续。
Nvidia's 32-bit compatibility libraries?
选择 No 继续。
Would you like to run the nvidia-xconfigutility to automatically update your x configuration so that the NVIDIA x driver will be used when you restart x? Any pre-existing x confile will be backed up.
选择 Yes 继续

8、挂载 Nvidia 驱动

{.line-numbers}
1
sudo modprobe nvidia

9、查看驱动是否安装成功

{.line-numbers}
1
nvidia-smi

输出结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
root@user-Super-Server:~# nvidia-smi
Thu Feb 29 15:25:16 2024
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.104.05 Driver Version: 535.104.05 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| 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 RTX 4090 Off | 00000000:31:00.0 Off | Off |
| 55% 47C P2 90W / 450W | 20175MiB / 24564MiB | 100% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
| 1 NVIDIA GeForce RTX 4090 Off | 00000000:4B:00.0 Off | Off |
| 46% 48C P2 112W / 450W | 20431MiB / 24564MiB | 100% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
| 2 NVIDIA GeForce RTX 4090 Off | 00000000:B1:00.0 Off | Off |
| 52% 48C P2 106W / 450W | 17871MiB / 24564MiB | 100% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
| 3 NVIDIA GeForce RTX 4090 Off | 00000000:CA:00.0 Off | Off |
| 69% 49C P2 115W / 450W | 22477MiB / 24564MiB | 100% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| 0 N/A N/A 1828292 C ...onda3/envs/llama_factory/bin/python 20142MiB |
| 1 N/A N/A 1828293 C ...onda3/envs/llama_factory/bin/python 20398MiB |
| 2 N/A N/A 1828294 C ...onda3/envs/llama_factory/bin/python 17838MiB |
| 3 N/A N/A 1828295 C ...onda3/envs/llama_factory/bin/python 22444MiB |
+---------------------------------------------------------------------------------------+

安装CUDA

1、查看自己电脑的驱动程序版本

nvidia-smi命令结果输出右上角可以看到最高版本的cuda不能大于12.2,具体驱动程序对应的cuda版本可以通过官网查看

2、下载cuda

官网地址:CUDA Toolkit Archive | NVIDIA Developer

这里依据个人情况自定义去下载,当前文档使用版本为:cuda_12.2.2_535.104.05_linux.run

3、卸载旧版本

开始卸载旧版本CUDA(确保一定没有cuda环境):

{.line-numbers}
1
2
sudo apt-get --purge remove "*cuda*" "*cublas*" "*cufft*" "*cufile*" "*curand*"  "*cusolver*" "*cusparse*" "*gds-tools*" "*npp*" "*nvjpeg*" "nsight*" "*nvvm*"
sudo apt-get autoremove

{.line-numbers}
1
sudo /usr/local/cuda-12.1/bin/cuda-uninstaller

用空格选择所有cuda相关文件,确认,需要一会儿提示成功卸载。

最后确认删除磁盘文件,手动删除命令:sudo rm -rf /usr/local/cuda-12...

4、开始准备安装

修改安装包:

{.line-numbers}
1
vi cuda_12.2.2_535.104.05_linux.run

编辑修改/tmp为/data/tmp ==(就在开头几行不用往下找)==

执行安装cuda:

{.line-numbers}
1
sudo sh cuda_12.2.2_535.104.05_linux.run

配置环境变量:

{.line-numbers}
1
sudo  vim /etc/profile

在文件末尾添加:

{.line-numbers}
1
2
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-12.2/lib64
export PATH=/usr/local/cuda-12.2/bin:$PATH

重新加载环境变量:

{.line-numbers}
1
source /etc/profile

5、看看cuda安装是否正常:

{.line-numbers}
1
2
3
4
5
6
root@user-Super-Server:~# nvcc  -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Aug_15_22:02:13_PDT_2023
Cuda compilation tools, release 12.2, V12.2.140
Build cuda_12.2.r12.2/compiler.33191640_0

python 环境

==为方便环境安装部署迁移等操作,python环境统一为miniconda==

安装与卸载miniconda

安装可以从官网进行下载安装,也可以使用清华国内源,尽可能使用最高版本,最高版本可以创建更多其他版本的虚拟环境。

当前主机下载的版本为: Miniconda3-py310_23.11.0-2-Linux-x86_64.sh,可以直接运行安装,所有选项均可为默认选项。

Linux自带python环境,因此需要注意的是,安装好miniconda之后miniconda会覆盖掉原本的python环境,因此如果你有需求使用原始的python环境,需要注意环境变量覆盖的问题。

安装成功演示:

1
2
3
4
(base) root@user-Super-Server:~# python
Python 3.10.13 (main, Sep 11 2023, 13:44:35) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

(base) 关键词是成功激活conda环境的关键标识,不同的虚拟环境标识会显示对应虚拟环境的名字,之后的文档都将会出现这样标识以指定python环境信息。

如何卸载miniconda如下:

查看miniconda信息:

{.line-numbers}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(base) root@user-Super-Server:~# conda info

active environment : base
active env location : /root/miniconda3
shell level : 1
user config file : /root/.condarc
populated config files :
conda version : 24.1.2
conda-build version : not installed
python version : 3.10.13.final.0
solver : libmamba (default)
virtual packages : __archspec=1=icelake
__conda=24.1.2=0
__cuda=12.2=0
__glibc=2.31=0
__linux=5.15.0=0
__unix=0=0
base environment : /root/miniconda3 (writable)
conda av data dir : /root/miniconda3/etc/conda
conda av metadata url : None
channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/linux-64
https://repo.anaconda.com/pkgs/r/noarch
package cache : /root/miniconda3/pkgs
/root/.conda/pkgs
envs directories : /root/miniconda3/envs
/root/.conda/envs
platform : linux-64
user-agent : conda/24.1.2 requests/2.31.0 CPython/3.10.13 Linux/5.15.0-97-generic ubuntu/20.04.6 glibc/2.31 solver/libmamba conda-libmamba-solver/23.12.0 libmambapy/1.5.3
UID:GID : 0:0
netrc file : None
offline mode : False

进入安装目录后执行卸载命令:

{.line-numbers}
1
bash /path/to/miniconda3/uninstall-miniconda.sh -u

随后手动清除对应的环境变量文件(~/.bashrc或/etc/profile)当中的内容之后重新加载环境变量文件
来更新配置:

{.line-numbers}
1
2
3
source ~/.bashrc

source /etc/profile

创建虚拟环境

  • 创建虚拟环境: conda create -n 环境名 python=3.9
  • 进入虚拟环境: conda activate 环境名
  • 退出虚拟环境: conda deactivate
  • 查看虚拟环境列表: conda env list
  • 删除虚拟环境: conda remove -n 环境名 --allconda env remove --name 环境名
  • 复制虚拟环境: conda create -n 新环境名 --clone 旧环境名

安装python库等操作

安装可以使用pip安装或conda命令安装:

{.line-numbers}
1
2
pip install xxx
conda install xxx

conda不是python,conda是一个环境管理工具,conda命令安装的库稳定性更好更安全也更不容易在安装过程当中出错,并且conda当中包含很多属于自己的工具,但是pip作为python指定安装工具,包含更多更新的工具,也包含==未验证的工具==,两者在大多数时候可以搭配使用。

安装速度过慢可以采用临时源解决:

{.line-numbers}
1
pip install 指定安装的库 -i https://pypi.tuna.tsinghua.edu.cn/simple 

提供以下国内几种源:

{.line-numbers}
1
2
3
4
5
6
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/

必须安装的库

项目当中往往需要使用到环境迁移配置好的环境想要进行有效迁移往往很麻烦,conda提供一个优秀的环境打包工具:conda-pack,该工具可以在同类型操作系统之间快速迁移python的工作环境,并且不会对原始环境有任何影响。

安装:

{.line-numbers}
1
conda install conda-pack

使用(激活conda环境后):

{.line-numbers}
1
2
3
4
5
conda pack -n 环境名称
# -o 参数指定打包路径和名称,把虚拟环境 my_env 打包为 out_name.tar.gz
conda pack -n 环境名称 -o out_name.tar.gz
# 把某个特定路径的虚拟环境打包为 my_env.tar.gz
conda pack -p /explicit/path/to/环境名称

迁移到其他机器上:

  • 进入对应的同操作系统类型机器
  • 创建一个环境放置的目录
  • 将condapack打包后的文件放置在该目录下后解压

激活环境:

1
2
3
4
# linux
source .../bin/activate
# windows
.../bin/activate/activate.bat