DEV Community

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

Guide to install PyTorch with CUDA on Ubuntu 18.04

evanilukhin profile image Ivan Ilyukhin ・3 min read

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
  • 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):

mv /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys
sudo add-apt-repository "deb /"
sudo apt-get update
sudo apt-get -y install cuda

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

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

Then install pip following by the (instruction)[]

curl -o

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
git submodule sync
git submodule update --init --recursive

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

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

export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
python install

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


Create the file and add the next lines:

from __future__ import print_function
import torch

x = torch.ones(5, 5)


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 =                       # or just use strings ``.to("cuda")``
    z = x + y
    print("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! 🤖📖

Discussion (5)

Editor guide
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?

jodyngo profile image

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

evanilukhin profile image
Ivan Ilyukhin Author • Edited

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

evanilukhin profile image
Ivan Ilyukhin Author

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