How to Install PyTorch on Ubuntu 24.04 Using pip

Estimated reading: 3 minutes 125 views

Introduction

PyTorch is one of the most widely used open-source machine learning frameworks. It powers cutting-edge research and real-world applications in computer vision, natural language processing, and more.

If you’re running a VPS with Ubuntu 24.04, such as GreenCloud VPS, you can install PyTorch and start building AI projects without needing a GPU. While GPU acceleration speeds up training, many tasks — especially smaller models and inference — run perfectly fine on CPU.

This tutorial will guide you step by step through installing PyTorch on Ubuntu 24.04 using pip inside a virtual environment.

Prerequisites

  • A VPS running Ubuntu 24.04

  • sudo privileges

  • Basic knowledge of Linux commands

Step 1: Update the System

Before installing any software, it’s a good practice to update your system to ensure all packages are up to date. Run the following command:

sudo apt update

Step 2: Install Python and pip

PyTorch requires Python and PIP (Python’s package installer). Install them using the following commands:

sudo apt install python3 

sudo apt install python3-pip

Step 3: Install Virtualenv

To avoid conflicts between dependencies, it’s recommended to use a virtual environment. Install virtualenv using PIP:
sudo apt install virtualenv

Step 4: Create a Virtual Environment

Create a virtual environment for your PyTorch project:

virtualenv my_project_env

This command creates a directory named my_project_env containing the virtual environment.

Step 5: Activate the Virtual Environment

source my_project_env/bin/activate

Step 6: Install PyTorch (CPU Version)

With the virtual environment activated, install PyTorch, along with torchvision and torchaudio, using PIP:

This command installs the latest stable version of PyTorch and its associated libraries.

pip3 install torch torchvision torchaudio

 

Step 7: Verify the Installation

To verify that PyTorch is installed correctly, open the Python interpreter:

python

Run the following commands:

import torch
print(torch.__version__)

If PyTorch is installed correctly, this will print the version of PyTorch installed. For example:

2.0.1

Exit the Python interpreter by typing:

exit()

Step 8: Deactivate the Virtual Environment

When you’re done working with PyTorch, you can deactivate the virtual environment by running:

deactivate

Conclusion

Congratulations! 🎉 You’ve successfully installed PyTorch on Ubuntu 24.04 in a virtual environment on your VPS.

Even without a GPU, you can still use PyTorch to:

  • Run inference tasks (deploy trained models)

  • Train small to medium-sized models

  • Experiment with deep learning frameworks

  • Use PyTorch for data science and machine learning projects

With a GreenCloud VPS, you benefit from high-performance SSD storage, stable uptime, and scalable resources, making it an excellent environment for hosting your AI experiments and applications.

Share this Doc

How to Install PyTorch on Ubuntu 24.04 Using pip

Or copy link

CONTENTS