DEV Community

Cover image for Guide to install PyTorch with CUDA on Ubuntu 18.04
Ivan Iliukhin
Ivan Iliukhin

Posted on

Guide to install PyTorch with CUDA on Ubuntu 18.04

Guide to install PyTorch with CUDA on Ubuntu 18.04

Alt Text

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.

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt install python3.8 python3.8-dev python3.8-venv
Enter fullscreen mode Exit fullscreen mode
  • 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. I selected deb(network):

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
Enter fullscreen mode Exit fullscreen mode

Install pip

Before pip installation, you should create a new virtual environment for Python. On the first step, we installed python3.8-venv exactly for this purpose.

python3.8 -m venv ~/python_env/my_env
Enter fullscreen mode Exit fullscreen mode

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)

source ~/python_env/my_env/bin/activate
Enter fullscreen mode Exit fullscreen mode

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

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
Enter fullscreen mode Exit fullscreen mode

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:

sudo apt install cmake
git clone --recursive https://github.com/pytorch/pytorch.git
git submodule sync
git submodule update --init --recursive
Enter fullscreen mode Exit fullscreen mode

Than install all required Python packages using pip:

Ensure that you use right Python virtual environment

python3.8 -m pip install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing
Enter fullscreen mode Exit fullscreen mode

Ensure that you are in the cloned directory and run the next commands:

export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
python setup.py install
Enter fullscreen mode Exit fullscreen mode

🎉 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:

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))
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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! 🤖📖

Top comments (4)

Collapse
 
jota87r profile image
Jonatán Ramírez Aldana

I'm getting the following error while trying to install the cuda toolkit:

The following packages have unmet dependencies:
cuda : Depends: cuda-11-2 (>= 11.2.0) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

Does anyone knows how to fix this?

Collapse
 
jodyngo profile image
jodyngo

Please advise me to solve this issue:
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
dirname: missing operand

Collapse
 
evanilukhin profile image
Ivan Iliukhin • Edited

It looks like the command $(which conda) returned nothing. Also, I recommend to try without exporting that env variable

Collapse
 
evanilukhin profile image
Ivan Iliukhin

Interesting question! I know only about the PyTorch and haven't searched other.