Ubuntu18.04 system builds a deep learning environment

安装概要:显卡驱动+cuda11.7+cudnn+pip换源+Anaconda3+pycharm

"xxx": represents the software name


1. System change source

注: 更新包的时候速度变快

1) Find "Software and Updates", change the source in "Download from" to the source of Alibaba Cloud, as shown in the following figure:

insert image description here
2) Check "Canonical Partner" in "Other Software", as shown in the figure below:

insert image description here
3) Change "Automatically check for updates" and "Notify me when there is a new version" in "Update" to "Never", as shown in the following figure:

insert image description here
4) After closing, open the terminal and update:

sudo apt update
sudo apt upgrade

2. Install the graphics card driver

1) Enter the following command in the terminal to install vim:

sudo apt install vim

2) To disable the nouveau driver that comes with Ubuntu, enter the following command:

sudo vim /etc/modprobe.d/blacklist-nouveau.conf 

3) After opening the blacklist-nouveau.conf file, enter "i" to enter the edit mode, and then enter the following command:

blacklist nouveau
options nouveau modset=0

注:按“esc”+“:”+“wq”退出该文件的编辑

4) Reboot reboot (this step is required)

reboot

5) After restarting, check whether nouveau is disabled successfully

lsmod | grep nouveau 

注:输入该命令后终端若无输出则表示禁用成功

6) Add nvidia-driven ppa source and install it

sudo add-apt-repository ppa:graphics-drivers/ppa

7) To view the optional driver version, enter the following command:

ubuntu-drivers devices

The display is as follows:
insert image description here
8) Enter the following commands in order to install the nvidia driver and its dependent packages:

sudo apt update
sudo apt install nvidia-driver-470 #(此处安装驱动以实际输出信息为准,一般安装recommended的,如图中nvidia-driver-470)
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3-dev

注:如果显示“E:无法定位软件包”,则可用以下方法安装驱动

Open "Software and Update", click "Additional Driver", select the appropriate version, and apply it, as shown in the figure below:
insert image description here
9) Reboot again, restart

10) Open the terminal and enter nvidia-smi, if the following similar interface appears, it means that the driver installation is complete
insert image description here


3. Install cuda and cudnn

1) Select the version suitable for the current system to download on the cuda official website , as shown in the figure below:

insert image description here
2) Download cuda according to the download link of "Installation Instructions", and then run:

sudo sh cuda_11.7.1_515.65.01_linux.run 

注:首先点击continue和接受协议(输入accept),然后要将Driver项勾选去掉,勾选上CUDA Toolkit 11.7,其他的几个选项可选可不选,点击Install,等待安装完成

3) Configure environment variables

sudo gedit ~/.bashrc
 
# 在打开文件的最下方依次输入以下内容:
export CUDA_HOME=/usr/local/cuda 
export PATH=$PATH:$CUDA_HOME/bin 
export LD_LIBRARY_PATH=/usr/local/cuda-11.7/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

4) Save and exit to make the environment variables take effect, open the terminal, and enter the following command:

nvcc -V

The following similar interface appears, indicating that the installation is successful

insert image description here
5) Install cudnn, select the appropriate version of cudnn on the official website

注:1. 需要在官网进行注册,然后下载“Local Installer for Linux x86_64 (Tar)”

insert image description here
6) After the download is complete, unzip and enter the folder, so enter the following command:

注:这里的指令和安装cuda10.0的cudnn是有区别的

# 将下载好的.h文件和lib文件放到cuda文件夹目录下
sudo cp cudnn-linux-x86_64-8.5.0.96_cuda11-archive/include/cudnn*.h /usr/local/cuda/include 
sudo cp -p cudnn-linux-x86_64-8.5.0.96_cuda11-archive/lib/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*

Then enter the following code:

cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

注:出现以下界面查看版本,安装成功

insert image description here


4. Install Anaconda3

注: Anaconda可以很好地创建虚拟环境,用于分割不同项目所使用的环境

1) From the version correspondence of anaconda in this blog and the anaconda download address, check the anaconda corresponding to different versions of python. Most of them use python3.7, and install "Anaconda3-5.3.1-Linux-x86_64.sh" here.

2) Open the terminal from the location where the downloaded file is located, and enter the following command to install:

sudo sh Anaconda3-5.3.1-Linux-x86_64.sh

注: 在安装过程中会提示是否要把Anaconda写进环境变量里,写入的话终端输入python就会进入anaconda的python环境;而不写入的话,终端输入python3就是进入系统自带的python环境

3) Enter the following command to load environment variables:

gedit ~/.bashrc
# 在文件末写入、下面这一行命令
. /home/xxx/anaconda3/etc/profile.d/conda.sh
# source一下,激活环境变量
source ~/.bashrc

4) After installing anaconda, use the following command to create a conda environment:

conda create -n xxx python=???  # xxx是虚拟环境名称     ???是python的版本
conda activate xxx  # 进入虚拟环境
conda deactivate  # 退出虚拟环境

5. pip change source

注: pip换源加快下载python包的速度

1) Open the terminal and enter the following command to change the source:

mkdir ~/.pip
gedit ~/.pip/pip.conf
# 打开pip.conf文件后,在文件中输入
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple # 换为清华源

Save and exit the pip.conf file, the file takes effect


6. Install pytorch

1) It is recommended to use the domestic image to install pytorch, and enter the following commands in the terminal in sequence:

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

insert image description here
2) View your own adaptation version on the pytorch official website

注: Pytorch Build中,stable是稳定版,preview是抢先版

insert image description here
3) Enter the virtual environment and use the command "conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch-nightly -c nvidia" to install

Use the command to view the torch version:

python
import torch
torch.__version__

Use the command to check whether it can be accelerated:

print(torch.cuda.is_available())

7. Install Pycharm

1) Download from the official website and run the following command to install:

注: 激活码可在网上查找

tar zxvf pycharm-professional-2022.2.1.tar.gz # 解压文件
cd pycharm-professional-2022.2.1
cd bin
./pycharm.sh

2) Create a pycharm shortcut:
Open "Pycharm", click "Tools" –> "create desktop entity" –> "OK". Then open show applications, find the "Pycharm" icon, right-click and select "Add to Favorites".


Summarize

So far, the deep learning environment of Ubuntu 18.04 system has been installed, I hope it can help everyone, welcome to collect!

Guess you like

Origin blog.csdn.net/Ayu147258/article/details/126864068