How to Install Pytorch On Ubuntu 22.04? (Step-by-Step Pytorch Ubuntu Installation Guide)

PyTorch is a widely-used open-source machine learning framework that stands out for its ease of use, dynamic computation graph, and efficiency, making it ideal for deep learning tasks.

Developed by Meta AI and now part of the Linux Foundation, PyTorch is used for applications like image and speech recognition, natural language processing, and reinforcement learning. Its powerful, GPU-optimized architecture allows data scientists to build and deploy neural networks with ease, making it an essential tool for AI research and development. 

This guide will help you how to install PyTorch on Ubuntu using Pip or Anaconda to get started.

Prerequisites

Make sure your system meets these requirements:

  • Python 3.6 or latest version 
  • For GPU support, ensure CUDA drivers and toolkit should be installed.

Installing PyTorch on Ubuntu (Install Pytorch Ubuntu)

To install PyTorch on a Ubuntu system, either with CPU or GPU support, you can use the Pip or Anaconda. We'll focus on how to install Pytorch using Pip and setting up a virtual environment to keep your Python packages organized.

Method 01: Install PyTorch on Ubuntu Using Pip3 (Install Pytorch Ubuntu installation)

You can easily install PyTorch on Ubuntu using Pip (Python package Manager) by executing the following steps:

Step 1: Update System Packages

First, make sure your system packages are up to date. Open your terminal and run the following command:

$ sudo apt update

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

This will update your package lists to ensure you have the latest versions.

Step 2: Install Python3-venv

Next, you'll need to install python3-venv, which allows you to create isolated Python environments. This is important to prevent conflicts between projects requiring different package versions.

$ sudo apt install python3-venv -y

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

Step 3: Set Up a Python Virtual Environment

To set up a Python virtual environment, create a directory for your Python project and use the cd command to navigate into it. This directory will hold the virtual environment and related files. For example, we have created a directory named “pytorch_env“. 

$ mkdir pytorch_env
$ cd pytorch_env

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

Now, create and activate the virtual environment:

python3 -m venv pytorch_env
source pytorch_env/bin/activate

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

Once the virtual environment is activated, your terminal prompt will change to show that you're now working within the pytorch_env environment. Now, you can install Python packages or run scripts specifically within this environment. This helps to avoid conflicts with other projects by keeping dependencies separate from the system-wide Python installation.

Step 4: Install PyTorch Using Pip (Install Pytorch Ubuntu using Pip)

With the virtual environment active, you can install PyTorch. If you don’t have GPU or don't need GPU acceleration. You can install the Pytorch library for only CPU support. To install Pytorch to run on the CPU, use this command:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

If you have a compatible GPU and want to leverage CUDA for acceleration, install PyTorch with GPU support:

pip3 install torch torchvision torchaudio

Step 5: Verify the Installation

To confirm that PyTorch has been successfully installed, open the Python interpreter and check the version of PyTorch:

python
import torch

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

value = torch.randn(1, 6)

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

print(torch.__version__)

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

This will display the version of PyTorch that you installed, confirming the setup was successful.

Method 02: How to Install PyTorch on Ubuntu using Anaconda

You can install PyTorch on Ubuntu using Anaconda, a popular open-source platform. This method allows you to easily manage your Python packages and environments. To install Pytorch using Conda follow these steps:

Step 1: Update System Packages

Start by updating your system packages to ensure everything is up to date. Open your terminal and run:

$ sudo apt update

Step 2: Install Anaconda or Conda

Next, you'll need to install Anaconda. You can install Conda packages using the Curl command. If curl is not already installed on your Ubuntu system, install it using the following command:

$ sudo apt install curl -y

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

Navigate to a Temporary or temp Directory: It’s a good practice to perform installations in an isolated directory. You can use the /tmp directory:

cd /tmp

Download the Anaconda Installer Script by using Curl to download the installer:

curl --output anaconda.sh https://repo.anaconda.com/archive/Anaconda3-2023.07-2-Linux-x86_64.sh

how to install pytorch on ubuntu 22.04? (step-by-step pytorch ubuntu installation guide)

This command saves the Anaconda installer as “anaconda.sh”. You can check the official Anaconda website for the latest version if needed.

Verify the Downloaded File. It's important to check the integrity of the downloaded file to ensure it hasn’t been corrupted. Verify it with:

sha256sum anaconda.sh

Make sure the checksum matches the value provided on the official Anaconda website.

Once verified, proceed with the installation by running the installation script:

bash anaconda.sh

After installation, update your shell session to make Anaconda commands available in your terminal:

source ~/.bashrc

You should see the terminal prompt change to “base,” indicating that the default Anaconda environment is active. You can later activate Anaconda using the conda activate command.

Confirm that Anaconda is installed by running:

conda --version

Step 3: Install PyTorch Using Anaconda

With Anaconda installed and active, you can now install PyTorch.

If your system doesn't have a GPU or you don’t need GPU acceleration, install PyTorch with CPU support:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

If you have a GPU and want to use CUDA for acceleration, install PyTorch with GPU support:

conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

Step 4: Verify the Installation

Finally, to verify that PyTorch was installed correctly, start a Python session and check the PyTorch version:

python
import torch
print(torch.__version__)

This will print the version of PyTorch that is currently installed, confirming that everything is set up properly.

How to Uninstall Pytorch on Ubuntu?

To uninstall PyTorch from your Ubuntu system, follow these steps depending on how you originally installed it.

If you installed PyTorch using Pip, you can remove it by running the following command:

$ pip3 uninstall torch -y

This command will uninstall PyTorch from your Python environment.

If you used Anaconda to install PyTorch, you can uninstall it with this command:

$ conda remove pytorch -y

This will remove PyTorch from your Anaconda environment.

Conclusion 

This guide demonstrated how to install PyTorch on Ubuntu using both Pip and Anaconda. It also covered the different installation options available depending on your system's computational resources. With PyTorch now set up, you're ready to dive into its features and take advantage of its powerful capabilities.

VPS web hosting is a powerful and flexible option that can significantly impact your website's success. With blueVPS, you can rent servers worldwide, including cPanel for easy management. Ready to elevate your website's performance? Consider VPS hosting today.


Blog