How to Install PyTorch on Ubuntu Linux

install pytorch ubuntu linux

Learn how to install PyTorch on Ubuntu Linux to build your own Artificial Intelligence (AI) and Machine Learning cloud. PyTorch is an open source machine learning framework for deep learning using GPUs and CPUs. It is one of the most popular machine learning library, alongside with TensorFlow, though PyTorch more towards object oriented programming style and its implementations are longer because they seem to cover a lot of low level details. It should be noted that both PyTorch and TensorFlow are supported by ExpertVM cloud.


Installing PyTorch

  1. As good practice, ensure all packages are up-to-date by using the following command:
    sudo apt-get update -y
  2. Now, install the python3-venv package:
    apt-get install -y python3-venv
  3. Next, let us install PyTorch (with CUDA)
    pip3 install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f

    This is how you install PyTorch (without CUDA)

    pip3 install torch==1.9.0+cpu torchvision==0.10.0+cpu torchaudio==0.9.0 -f
  4. Let us test the installation now by executing:
  5. Enter the following code:
    import torch
    x = torch.rand(5, 3)
  6. You should expect to see this output.
    tensor([[0.3380, 0.3845, 0.3217],
    [0.8337, 0.9050, 0.2650],
    [0.2979, 0.7141, 0.9069],
    [0.1449, 0.1132, 0.1375],
    [0.4675, 0.3947, 0.1426]])
  7. Finally, you may check if NVIDIA driver and CUDA are both accessible:
    import torch

Install NVIDIA driver, CUDA and cuDNN

In order to ensure that you can utilise GPU for PyTorch within our GPU Compute, you will need to ensure that you have the latest and correct NVIDIA driver, CUDA & cuDNN installed within your VPS.

Install NVIDIA driver for Ubuntu

Firstly, download the latest NVIDIA driver from here using the wget command, and then run the following command to install it:

sudo sh

Install CUDA

Next, download the latest CUDA from here using the wget command, and then run the following command to install it. Remember to take note on the instructions given at the end of installation to add the additional lines to your shell script:

sudo sh

Install cuDNN

Finally, refer to the official guideline here to complete cuDNN installation.


This tutorial shows you how to install PyTorch on Ubuntu, as well as installing the right NVIDIA driver, CUDA & cuDNN for your cloud server.

Important Information

You may wish to be mindful of future updates done to Ubuntu (sudo apt-get update) may break your NVIDI driver, CUDA & cuDNN installation. When this happens, simply roll back with your Ubuntu upgrade and switch back to the kernel version that worked well.

Related Post