Guide to install PyTorch with CUDA on Ubuntu 18.04
Tags: python, beginners, pytorch, ubuntu
Published: 2019-12-20 18:40:28

# Guide to install PyTorch with CUDA on Ubuntu 18.04

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ukv2ncfhz9jwrcmla6at.png)

Yesterday I was installing PyTorch and encountered with different difficulties during the installation process. Let me share the resulting path, that brought me to the successful installation. I hope this little instruction will save you time and show further direction. 

### Install Python3.8
PyTorch requires Python version 3.7 or above, so I decided to install the latest stable version on this moment Python 3.8. Standard Ubuntu repository has maximum 3.6, so I added repository from the “deadsnakes” team [link](https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa). 
```bash
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt install python3.8 python3.8-dev python3.8-venv
```
- `python3.8-dev` - requires for building C extensions
- `python3.8-venv` - provides support for creating lightweight “virtual environments” with their own site directories

### Install CUDA
My graphic card supports CUDA, so why not take advantage of this.
Just choose one of the type of installation [there](https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1804). I selected `deb(network)`:

```bash
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pinsudo 
mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
sudo apt-get update
sudo apt-get -y install cuda
```
### Install pip

Before pip installation, you should create a new [virtual environment](https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments) for Python. On the first step, we installed `python3.8-venv` exactly for this purpose. 

```bash
python3.8 -m venv ~/python_env/my_env
```

This command creates a new local environment in your local folder. After the directory has been created "activate" this environment by the next command. (Works only for bash/zsh, for other shells look [there](https://docs.python.org/3/library/venv.html))

```bash
source ~/python_env/my_env/bin/activate
```

Then install `pip` following by the (instruction)[https://pip.pypa.io/en/stable/installing/]

```bash
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
```

Perfect! Let's go to the finish. 🏁

### And the final step - Get, compile and install PyTorch

Firstly install `cmake` and clone the necessary version of the PyTorch. Don't be afraid, repository really big, so it takes a lot of time:
```bash
sudo apt install cmake
git clone --recursive https://github.com/pytorch/pytorch.git
git submodule sync
git submodule update --init --recursive
```

Than install all required Python packages using pip:

> Ensure that you use right Python virtual environment 

```bash
python3.8 -m pip install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing
```

Ensure that you are in the cloned directory and run the next commands:
```bash
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
python setup.py install
```

:tada: Congratulations, you are installed the PyTorch in your local virtual environment. Time to check it!

### Running

Create the file `torch_program.py` and add the next lines:
```python
from __future__ import print_function
import torch

x = torch.ones(5, 5)

print(x)

if torch.cuda.is_available():
    device = torch.device("cuda") # a CUDA device object
    y = torch.ones_like(x, device=device)  # directly create a tensor on GPU
    x = x.to(device)                       # or just use strings ``.to("cuda")``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))
```

If all installed correctly there will be the followed resuls:
```
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
tensor([[2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.]], device='cuda:0')
tensor([[2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.]], dtype=torch.float64)
```

If you get stuck following this manual feel free to write in comments about it, I'll be sure to expand this post with the new information.

Happy Machine Learning! 🤖📖