Install Pytorch+CUDA on Ubuntu 16.04

Pytorch and TensorFlow

In recent years, the Pytorch deep learning framework has become more and more popular among deep learning developers due to its simple network structure and low entry barrier. It is different from TensorFlow in that Pytorch is a dynamic framework and does not need to be set up at the beginning. The architecture can be modified while debugging during operation, while TensorFlow is the opposite. The advantage of this is that developers do not need to clarify the structure of the network built at the beginning, and can slowly learn to find a more suitable structure, just like in the building Engineers on site visits on the construction site, workers will come over every time they build a wall and ask what to do next, and TensorFlow is like an architect drawing drawings in an office, designing the structure of the entire building before construction, and when designing No one will bother, of course, the efficiency is higher than Pytorch.

installation steps##

Environment: Ubuntu16.04

Graphics card: NVIDIA GTX970

Install graphics driver###

Since we need to use CUDA to accelerate the training process in Pytorch, the first step is to install the graphics driver to prepare for the installation of CUDA.

  1. Go to the NVIDIA official website to download the corresponding graphics card driver, the address is http://www.nvidia.com/Download/index.aspx?lang=en-usblacklist nouveau
    options nouveau modeset=0blacklist nouveau
    options nouveau modeset=0
  2. ctrl + alt + f1 #Enter the command line mode to stop using the graphics card
  3. sudo service lightdm stop #Close the graphical interface
  4. sudo apt-get remove --purge nvidia* #Remove the old driver
  5. sudo apt-get update
  6. sudo apt-get install dkms build-essential linux-headers-generic #install dependency
  7. Add the following file sudo vim /etc/modprobe.d/blacklist-nouveau.conf #disable nouveau, enter the following
  8. Exit after saving the above file
  9. sudo update-initramfs -u
  10. Restart the computer
  11. ctrl + alt + f1 #Enter the command line mode again
  12. sudo service lightdm stop #Close the graphical interface again
  13. cd to the driver download directory
  14. sudo service lightdm stop
  15. sudo ./NVIDIA-Linux-x86_64-384.98.run -no-opengl-files #install driver
  16. sudo service lightdm start #Start the graphical interface
  17. cat /proc/driver/nvidia/version #Test drive

Install CUDA

  1. Go to NVIDIA official website to download the CUDA installation package for the corresponding system: https://developer.nvidia.com/cuda-90-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1604&target_type=deblocal
  2. sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb #install deb package
  3. sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub #apt adds the public key required to install CUDA
  4. sudo apt-get update
  5. sudo apt-get install cuda #apt install CUDA
  6. export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}} #Add environment variables
  7. export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} #64-bit system needs
  8. Verify that the installation is successful: nvcc -V

Install cuDNN

cuDNN is a tool library used by NVIDIA to accelerate deep neural network training. Although it is not necessary, it is better to install it for easier use in the future.

Install Anaconda

This article uses conda to install Pytorch, readers can also choose pip, but the author failed to install twice with pip, and finally chose conda, which is also a python package management tool, but Pytorch officially recommends conda, which seems to be more dependent on installation .

  1. Go to https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ to download Anaconda.sh
  2. bash Anaconda.sh
  3. source ~/anaconda3/etc/profile.d/conda.sh
  4. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ #Add the mirror source of Tsinghua University to speed up conda download
  5. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  6. conda config --set show_channel_urls yes
  7. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ #Specially add Pytorch mirror source. Since Pytorch is launched by Facebook, it is not possible to directly access its whl file in China Yes, need to use the mirror source of Tsinghua University

Install Pytorch

  1. conda install pytorchpython
    import torchprint(torch.cuda.is_available())
  2. conda install torchvision
  3. Test Pytorch
  4. Test whether you can use cuda

Reference document##

Recommended Posts

Install Pytorch+CUDA on Ubuntu 16.04
install vscode on ubuntu18
Install Redis on Ubuntu
Install R4 on ubuntu20
Install postgresql-10 on Ubuntu 18.04
Install docker on Ubuntu
Install Docker on ubuntu18.04
Install nodejs10 on Ubuntu16
Install mysql on Ubuntu 14.04
Install Django on ubuntu
Install Python3 on Ubuntu 14.04
Install rJava on Ubuntu18
Install JDK10+ on Ubuntu
Install Python3 on Ubuntu 16.04
Install KDE on Ubuntu16.04.2
Install Docker on Ubuntu18
Install Python3.7 on Ubuntu
Install flashplayer on Ubuntu
Docker practice (1): install Docker on Ubuntu 16.04
Install ubuntu on virtual machine
Install OpenSSL 1.0.2 on Ubuntu Server 14.04
Install Python 3.7 on Ubuntu 18.04 LTS
Install Android Studio on Ubuntu
Install scrapy framework on ubuntu
Install Mysql offline on Ubuntu
Install Oracle Java8 on Ubuntu
Install Odoo 11 stack on Ubuntu 16.04
Install Go locale on Ubuntu16
Install common software on Ubuntu 16.04
Install non-portable QQ on ubuntu
Install Docker on Ubuntu 18.04 offline
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
Install Tenda u12 driver on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
Install Chef server workstation on Ubuntu 18.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
Install botan2 encryption library on ubuntu
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
Docker ubuntu: install python-PIL-image environment on 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04