We have discussed about GPU computing as minimally needed theoretical background. Also, in an earlier guide we have shown Nvidia CUDA tool installation on MacOS X. Here is Practical Guide On How To Install PyTorch on Ubuntu 18.04 Server With Nvidia GPU. Installation demands server architecture which has Nvidia graphics card – there are such dedicated servers available for various purposes including gaming. Installing on localhost for intense and time consuming work not recommended for the sake of life of the device. The graphics card must support at least Nvidia compute 3.0 for more works than just PyTorch.
Steps on How To Install PyTorch on Ubuntu 18.04 Server
SSH to server. Update and upgrade :
1 2 | apt update -y apt upgrade -y |
We can see what graphics card hardware installed by running :
---
1 | sudo lshw -C display | grep product |
We need Nvidia driver installed. We can check whether and what graphics driver on SSH:
1 | nvidia-smi |
If no driver is installed or latest driver needed then unlike our MacOS X Nvidia CUDA guide, we have multiple options.
Ubuntu Default Recommended Driver more than good. Nouveau is the open source implementation of the Nvidia driver.
Official Nvidia Site is for official drivers but they do not upgrade automatically. Here are Ubuntu’s PPA, browse it :
1 | https://launchpad.net/~graphics-drivers/+archive/ubuntu/ppa |
nvidia-graphics-drivers-396
is newest but probably not much tested. We can add nvidia-graphics-drivers-390
PPA and install that driver :
1 2 | sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt install nvidia-driver-390 |
Now, again run the command :
1 | nvidia-smi |
You will get meaningful output. We should hold that version to stop getting upgraded :
1 | sudo apt-mark hold nvidia-driver-390 |
Install Linux headers :
1 | sudo apt install linux-headers-$(uname -r) |
We need gcc, g++ etc for the next steps :
1 | apt install gcc g++ gcc-6 g++-6 |
Now we have to install CUDA 9.x and cuDNN. Both will need manual browsing through browser.
1 | https://developer.nvidia.com/cuda-toolkit-archive |
You’ll see links for Nvidia driver and Nvidia CUDA tool kit. At the time of writing this guide, Nvidia CUDA tool kit is at version 9.2. We click-selected these options :
1 2 3 4 5 | Operating System -> Linux Architecture -> x86_64 Distribution -> Ubuntu Version -> 17.10 Installer Type -> deb (network) |
Download that deb file and upload to server via FTP. Run :
1 2 3 4 | sudo dpkg -i cuda-repo-ubuntu1710_9.2.148-1_amd64.deb sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64/7fa2af80.pub apt update -y apt install cuda |
There is more information on their doc :
1 | https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#package-manager-metas |
Again, hold that version :
1 | sudo apt-mark hold cuda |
Next, you will need to install CUDNN. For that, you need to login using Nvdia account, that is easy. You will get link to something like cuDNN v7.1.x Library for Linux
. You need to download that deb file and upload to server via FTP. The URL is :
1 | https://developer.nvidia.com/rdp/cudnn-download |
Find the directory where you have installed CUDA. It is probably usr/local/cuda-9.0/
. Move the above content into the directory where you install CUDA and run these operations (be careful about version numbered directory, below is example of format) :
1 2 3 4 | tar -zxvf cudnn-9.0-linux-x64-v7.1.tgz ls cd cudnn-9* ls |
We can install in normal way (check your version by doing ls
) :
1 2 3 | sudo dpkg -i libcuDNN7_7.1.5.15–1+cuda9.0_amd64.deb sudo dpkg -i libcuDNN7-dev_7.0.5.15–1+cuda9.0_amd64.deb sudo dpkg -i libcuDNN7-doc_7.0.5.15–1+cuda9.0_amd64.deb |
In official docs, I saw to do these :
1 2 3 | sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64/ sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include/ sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda/lib64/libcudnn* |
Install libcupti
, cuda-toolkit
(to avoid any missing package) :
1 2 | apt install libcupti-dev apt install nvidia-cuda-toolkit |
Open ~/.bashrc
and add the following line :
1 2 | export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}} export LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} |
Install conda following our previous guide to install Anconda or you can install miniconda. Then install PyTorch, TorchVision :
1 2 3 4 5 6 | pip install — ignore-installed — upgrade conda create -n pytorch python=3.6 pip numpy conda source activate pytorch ## install what is your version of cuda # conda install pytorch torchvision cuda91 -c pytorch conda install pytorch torchvision cuda92 -c pytorch |
That is it. Check Nvidia version :
1 2 3 | nvcc –version # cat /proc/driver/nvidia/version |
Reboot your server.
Unfortunately the setup is not smooth like setting up some normal server software or web software. Invariably you’ll face some error, need to fix by searching the web. I already have one fix in the guide from this solution :
1 | https://blog.csdn.net/u011668104/article/details/79560381 |
Check if thing is working :
1 2 3 4 5 6 7 8 9 | cd ~ mkdir cuda-test cp -r /usr/src/cuDNN_samples_v7/ ~/cuda-test ## or # cd /temp # mkdir cuda-test # cp -r /usr/src/cuDNN_samples_v7/ /temp/cuda-test cd ~/cuDNN_samples_v7/mnistcuDNN make clean && make |
Above should not give you error, then run :
1 | ./mnistcuDNN |
That should end successfully without any error, like this on end :
1 2 | ... Test passed! |
But it can give peculiar error. For that fix, I instructed to install both versions of gcc, g++:
1 | apt install gcc g++ gcc-6 g++-6 |
If you face error then you can symlink current with version 6 :
1 2 | sudo ln -s /usr/bin/gcc-6 /usr/local/cuda/bin/gcc sudo ln -s /usr/bin/g++-6 /usr/local/cuda/bin/g++ |
Ten perform the above. Test with some Python code for PyTorch.
Tagged With pytorch ubuntu 18 , ubuntu install pytorch , install pytorch ubuntu , install pytorch , install torch ubuntu , check if cuda installed ubuntu , news for you , pytorch ubuntu , how to check whether torch install in ubuntu , install pytorch on ubuntu