Cantech Knowledge Base

Your Go-To Hosting Resource

How to Install PyTorch on Ubuntu 22.04?

Introduction

If you take deep learning, AI, or data science seriously, PyTorch is one essential tech in your stack you must have. That sounds a bit off, huh? Fear not! Though installing PyTorch on Ubuntu 22.04 might sound like a daunting task for some, it’s not rocket science for those who have been through all this. A comprehensive stepwise guide, with appropriate ease, is presented here to assist you through everything in this article.

But first things first: if you are genuinely serious about AI and machine-learning stuff, it is the perfect time for you to have some power and reliability for hosting your models. Cantech provides reliable and fast Linux VPS Hosting, Dedicated Servers, and Cloud Hosting services – all optimized for AI workloads. We will make sure you maintain uninterrupted operations for, whether you are trying to train models, running experiments, deploying AI applications, or whatever you may be doing with a hosting platform with the right infrastructure and support.

Why Use PyTorch?

Let’s take a quick look at why PyTorch has become one of the most popular deep learning frameworks used by the AI community in the world. It offers everything from building neural networks, training models using large datasets and experimentation with reinforcement learning. It certainly provides enough flexibility and power.

Dynamic Computation Graphs – PyTorch features dynamic computation graphs that let you build models dynamically. You can modify the structure on the fly without restarting the process, which is extremely useful when debugging to prototype new AI architectures efficiently.

Seamless GPU Acceleration – PyTorch integrates easily with CUDA, enabling you to offload computations with NVIDIA GPUs. Thus, it increases the processing speed by orders of magnitude compared with a CPU, allowing you to process massive datasets and complex deep-learning models faster.

Strong Open-Source Community – PyTorch is backed by Facebook AI Research and is supplemented with a large, active global community contributing to its ecosystem. So what does that mean to you? You can expect regular updates, great documentation, and a ton of third-party extensions.

Intuitive & Pythonic – PyTorch is kept only for Python developers. It just feels normal and intuitive if you are used to programming in Python, thereby reducing the learning curve.

Powerful Libraries & Ecosystem – PyTorch works well with libraries like TorchVision (for computer vision), TorchText (for NLP), and torchaudio (for speech processing), making it a great versatile choice for AI applications in different domains.

When deep learning on a cloud machine is the norm for you, having a wholesome and good solution for hosting is very important. Cantech provides AI-optimized Linux VPS Hosting that helps you in all tasks from model training to proper execution and to work swiftly on computation. Be it any training job, or a deployment of machine-learning models in action, or a real-time AI workload, the power of hosting solutions from Cantech will consistently deliver!

Prerequisites

Before you install PyTorch, you require to have the following:

  • Ubuntu 22.04 LTS
  • Python 3.6+
  • pip or Conda installed
  • NVIDIA GPU (optional but recommended for AI/ML tasks)
  • CUDA (if using a GPU)

How to install PyTorch on Ubuntu?

If you are using Ubuntu, you can set up PyTorch in two main ways, either with pip or with Anaconda. Using pip is usually the faster option and works well if you just want to get started quickly. On the other hand, many developers prefer Anaconda because it makes managing different libraries and environments a lot easier. No matter which method you go with, both support CPU and GPU installations, so you will be ready to train and run deep learning models on your Ubuntu system without much trouble.

Step 1: Update Your System

You can begin by updating and upgrading your Ubuntu system to ensure you have installed the latest packages into the system.

sudo apt update && sudo apt upgrade -y

Step 2: Install Python & Pip

Make sure Python and pip are installed:

sudo apt install python3 python3-pip -y

Check the installed versions:

python3 --version
pip3 --version

Output:

Python 3.8.10
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

If you prefer using Anaconda, download and install it from Anaconda’s official site.

Step 3: Install PyTorch on Ubuntu 22.04 (CPU Version)

If your server doesn’t have a GPU, you can still install PyTorch easily. Just install the latest PyTorch along with the required packages like torch, torchvision, and torchaudio for full CPU support.

  • Update the Pip package manager for Python.
pip install --upgrade pip
  • For systems without GPU, a CPU-only version can be installed of PyTorch using pip as follows:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

Install Via Conda:

For CPU-only systems, you can install PyTorch with Conda by adding the cpuonly package from the PyTorch channel. Skip the NVIDIA channel since no GPU is used.

  • First, activate the Conda environment where you want to install PyTorch. For example, run conda activate myenv.
conda activate myenv
  • Install the most recent release of PyTorch directly from the official PyTorch channel.
conda install pytorch torchvision torchaudio cpuonly -c pytorch

This command sets up PyTorch in your Conda environment along with cpuonly, torchvision, and torchaudio packages.

Step 4: Install PyTorch on Ubuntu 22.04 (GPU Version)

If you have an NVIDIA GPU, install CUDA and cuDNN first.

  • Check if your system has a supported GPU:
nvidia-smi
  • Now, install PyTorch with GPU support:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

The command installs the latest PyTorch release based on CUDA 11.8. Along with it, torchvision and torchaudio are added to provide image and audio processing features.

Using Conda

On a GPU server, install PyTorch with Anaconda by setting up Anaconda first and then following the steps below.

  • First, activate the Conda environment where you want to install PyTorch. For example, run conda activate myenv.
conda activate myenv
  • Install the most recent release of PyTorch directly from the official PyTorch and the NVIDIA channel.
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

This command sets up the newest PyTorch release built on CUDA 11.8. Make sure you check the latest available version and install it in your environment.

Step 5: Verify Installation

To see if the installed version of PyTorch works, open Python and run:

1. Open the Python Shell

python3

2. Add the torch package

import torch

3. Check the tensor value

print(torch.__version__)
print(torch.cuda.is_available()) # Should return True if CUDA is enabled

If you see the version of PyTorch and a message about the GPU being available, you’ve installed PyTorch on Ubuntu 22.04!

Step 6: Running a Test Script

To ensure everything works fine, run a simple PyTorch script:

1. Open the Python Shell

python3

2. Add the torch package

import torch

3. Check the tensor value

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

If this runs without errors, PyTorch is ready to use!

Conclusion

To install PyTorch on Ubuntu 22.04, follow the necessary steps and you will find it easy. You can install either with pip or Conda, along with the necessary requirements. If you’re working with a PyTorch GPU setup, make sure to install the matching CUDA version for the best performance. And if you are looking for a future proof hosting solution that would take care of all your deep learning models, Cantech hosts AI and ML infrastructure, from VPS Hosting to Dedicated Servers.

FAQs

What are the uses of PyTorch?

PyTorch is one of the main deep learning frameworks used in many AI, machine learning, and data science domains. The library is used for developing neural networks, computer vision models, NLP’s and many other applications.

Can I install PyTorch with/without a GPU?

Yes! PyTorch works perfectly fine on the CPU. However, if you are working with large datasets and complicated models, the training time would be reduced significantly with a GPU optimized server like the ones from Cantech.

How do I check if PyTorch is using my GPU?

Following the successful installation of PyTorch, if you open a Python terminal, you can type in these lines

import torch
print(torch.cuda.is_available())

If the result is True, then it implies that PyTorch is utilizing your GPU. That means PyTorch is using your GPU.

What’s the difference between using pip and Conda to install PyTorch?

pip is the best option to operate in a virtual environment with a lightweight structure. Conda is the best option to manage dependencies, particularly if you are working on multiple projects.

Do I require CUDA for PyTorch?

CUDA is required only if you are going to use a GPU. If you are using PyTorch on a CPU, you will not need to install CUDA.

Can I install PyTorch inside a Docker container?

You can install PyTorch in a Docker container! Just remember, you must pull the right PyTorch Docker Image from the PyTorch official Docker Hub. Cantech’s Dedicated Servers for high-performance computing are great options for dedicated AI-optimized servers.

Related:
How to Install Anaconda on Ubuntu 22.04
How to Install NVIDIA CUDA Toolkit Ubuntu 22.04

 

October 3, 2025