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

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 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)[https://pip.pypa.io/en/stable/installing/]

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:

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

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 setup.py install

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


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

Posted on by:

evanilukhin profile

Ivan Ilyukhin


Passionate reader(sometimes writer). Avidly finding something interesting to create/research/read/cook.


Editor guide

What are the frameworks/tools/ libraries for CUDA (high performance) available in the market? As anyone tries using services of This company fastcompression.com/ I defently would try it. I read a really good review of it.


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


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


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