Skip to content

PyTorch Installation

PyTorch is a framework for the Python programming language, designed for machine learning. It includes a set of tools for working with models, commonly used in natural language processing, computer vision, and other similar fields. You can install it on your server manually using this guide.

PyTorch Installation on Linux

This instruction is suitable for the following operating systems: Ubuntu 22.04, and verified for Python versions: Python 3.10.

Note

If you plan to use GPU acceleration, please install NVIDIA drivers and CUDA according to this instruction.

  1. Install Python:

    sudo apt install python3.10
    

    In Ubuntu 22.04, this version is installed by default, so we do not recommend installing a newer version.

  2. Create a virtual environment for Python:

    python3 -m venv venv
    
  3. Activate the virtual environment:

    source venv/bin/activate
    

    After successful activation, the prompt will include the name of the virtual environment in parentheses:

    (venv) user@49069:~$
    

    Note

    You can create as many virtual environments as you like and install different libraries (including simultaneously, but sometimes this may cause conflicts).

  4. Install PyTorch libraries in the virtual environment:

    pip3 install torch torchvision torchaudio
    
  5. Verify PyTorch installation:

    To do this, run a Python console command python and then run the following program:

    import torch
    x = torch.rand(5, 3)
    print(x)
    

    If the installation is successful, you will receive the following output:

    (venv) user@49069:~$ python
    Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
     >>> import torch
     >>> x = torch.rand(5, 3)
     >>> print(x)
    tensor([[0.80, 0.04, 0.6],
         [0.32, 0.59, 0.7],
         [0.8, 0.70, 0.25],
         [0.40, 0.9, 0.9],
         [0.8, 0.15, 0.5]])
    
  6. Verify if PyTorch libraries use CUDA:

    To do this, run the following program in a Python console:

    import torch
    torch.cuda.is_available()
    

    If PyTorch can work in GPU mode, the output will be:

     >>> import torch
     >>> torch.cuda.is_available()
    True
    

To exit the virtual environment, use the command:

deactivate