Ubuntu20.04安装深度学习环境及配置个人环境小记
目标配置环境
系统为Ubuntu 20.04,
显卡为3090,结合其他服务器的环境,准备配置如下环境
1 2 3 4 5 6 7 nvidia-driver-460 CUDA 11.2 (gcc 7) cudnn 8.6.1 pytorch 1.10 torchvision 0.11.1 tensorflow 2.5.0
准备工作
刚装的系统,啥也没有,所以按照个人习惯先安装一些软件。为方便起见,后文非特殊说明,均用root用户完成安装。可以直接复制下文至sh文件中,更改权限以直接运行。(请保证软件源可正常连接)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 add-apt-repository ppa:graphics-drivers/ppa apt update -y && apt upgrade -y apt install -y git autossh python3-pip vim tmux gpustat tree ranger build-essential gcc-7 g++-7 make curl htop ipython3 zhs update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 100 update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 100 chmod 666 /etc/modprobe.d/blacklist.confecho " blacklist vga16fb blacklist nouveau blacklist rivafb blacklist rivatv blacklist nvidiafb " >> /etc/modprobe.d/blacklist.conf update-initramfs -u apt install nvidia-driver-460 -y
执行完成之后,重启服务器。使用nvidia-smi
查看gpu是否正常,或使用安装好的gpustat
cuda及cudnn安装
下载
这里需要从官网下载安装包,cuda安装包可以直接通过命令下载。cudnn需要注册英伟达开发者账号,下载四个文件
cudnn-11.2-linux-x64-v8.1.0.77
libcudnn8_8.1.0.77-1+cuda11.2_amd64.deb
libcudnn8-dev_8.1.0.77-1+cuda11.2_amd64.deb
libcudnn8-samples_8.1.0.77-1+cuda11.2_amd64.deb
1 2 3 wget https://developer.download.nvidia.com/compute/cuda/11.2.2/local_installers/cuda_11.2.2_460.32.03_linux.run && chmod 755 cuda_11.2.2_460.32.03_linux.run ./cuda_11.2.2_460.32.03_linux.run
下载完直接运行,安装过程中需要把Drivers这项取消,因为自带了显卡驱动。
环境变量
在/etc/bash.bashrc或/etc/profile前加入如下两行。如果profile不行就换bashrc。一般是编辑vim ~/.bashrc
下,但为保证所有用户都生效,所以需要在/etc
目录下编辑
1 2 export PATH=/usr/local/cuda-11.2/bin${PATH:+:${PATH} } export LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH} }
至此,cuda安装完成
1 2 3 4 cd /usr/local/cuda/samples/1_Utilities/deviceQuerymake clean && make -j4 && ./deviceQuery
cudnn配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 tar -zxvf cudnn-11.2-linux-x64-v8.1.0.77.tgz cp cuda/include/cudnn.h /usr/local/cuda/include cp cuda/lib64/libcudnn* /usr/local/cuda/lib64 chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn* dpkg -i libcudnn8_8.1.0.77-1+cuda11.2_amd64.deb dpkg -i libcudnn8-dev_8.1.0.77-1+cuda11.2_amd64.deb dpkg -i libcudnn8-samples_8.1.0.77-1+cuda11.2_amd64.deb cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2cd /usr/src/cudnn_samples_v7/conv_samplemake clean && make -j4 && ./conv_sample
通过ldconfig
可检查动态链接库是否链接正确。若有输出,可以使用ln -sf
重新链接。
Python库安装
1 2 3 pip3 install tensorflow-gpu==2.5.0 -i https://pypi.tuna.tsinghua.edu.cn/simple pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 -f https://download.pytorch.org/whl/torch_stable.html pip3 install black tqdm flask sklearn
检查是否安装成功
1 2 3 4 5 6 7 8 9 10 11 12 13 import torchprint (torch.cuda.is_available()) print (torch.backends.cudnn.is_available()) print (torch.__version__)print (torch.version.cuda)print (torch.backends.cudnn.version())import tensorflow as tfgpu_device_name = tf.test.gpu_device_name() print (gpu_device_name) tf.test.is_gpu_available()
若tf未检测到GPU,可以看输出过程中的哪些动态库文件加载失败,手动配置一下。
至此,深度学习安装环境配置完成。
个人环境
1 2 3 4 5 6 7 8 9 10 11 12 13 adduser xxx chmod u+w /etc/sudoersvim /etc/sudoers ``` root ALL=(ALL:ALL) ALL xxx ALL=(ALL:ALL) ALL ``` chmod u-w /etc/sudoers
ssh文件配置
方便起见直接把其他服务器的.ssh
文件夹下的id_rsa
,id_rsa.pub
和authorized_keys
复制过来。
1 2 3 4 chmod 755 ~/.ssh/ chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ~/.ssh/authorized_keyschmod 644 ~/.ssh/known_hosts
oh-my-zsh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom} /plugins/zsh-autosuggestions git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom} /plugins/zsh-syntax-highlighting plugins=( git zsh-autosuggestions zsh-syntax-highlighting ) source $ZSH /custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zshsource $ZSH /custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zshsource $ZSH /oh-my-zsh.sh
配置vim
编辑.vimrc
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 set numbersyntax on colorscheme darkblue syntax enable filetype plugin on let g:go_disable_autoinstall = 0let g:go_highlight_functions = 1let g:go_highlight_methods = 1let g:go_highlight_structs = 1let g:go_highlight_operators = 1let g:go_highlight_build_constraints = 1" " syntax on"" filetype plugin indent onlet g:pymode_python = 'python3' set numberset cinset sw=8set ts=8set smcolorscheme darkblue set viminfo='10,\"100,:20,%,n~/.viminfo au BufReadPost * if line("' \"") > 0|if line(" '\"") <= line("$")|exe("norm ' \"")|else|exe " norm $"|endif|endif
远程连接
1 autossh -gfCNR 远程端口:localhost:22(默认本地ssh端口) 公网用户名@公网IP
需要提前配好免密登录,远程端口在公网服务器上需要开放防火墙。
远程连接登录ssh 服务器用户@公网IP -p 远程端口