Log In

How to Install PyTorch on Ubuntu 22.04

How to Install PyTorch on Ubuntu 22.04
23.04.2024
Reading time: 6 min
Hostman Team
Technical writer

PyTorch is a popular open-source machine learning library widely used for developing deep learning models. It is renowned for its dynamic computational graph construction, which distinguishes it in the field. Offering a seamless transition from research prototyping to production deployment, PyTorch has earned its place as the preferred choice for developers and researchers alike in the realm of deep learning and AI.

Installing PyTorch on Ubuntu 22.04 is essential for developers and data scientists looking to leverage its powerful capabilities. This tutorial provides step-by-step instructions for installing PyTorch using both Conda and pip methods, verifying the installation, and checking the PyTorch version.

Ecosystem and Key Features

  • Tensor and Neural Network Operations: PyTorch provides a versatile tensor computation library, facilitating efficient mathematical operations crucial for building neural networks and conducting advanced research in deep learning.
  • Dynamic Computational Graphs: Unlike static computational graphs employed by other frameworks, PyTorch's dynamic computational graph construction enables flexible model building, facilitating dynamic adjustments and experimentation during the development process.
  • Automatic Differentiation: PyTorch simplifies gradient computation through its automatic differentiation capabilities, streamlining the process of optimizing neural network parameters and enhancing model performance.
  • Rich Tooling and Libraries: PyTorch offers a rich ecosystem of tools and libraries, including torchvision for computer vision tasks, torchaudio for audio processing, and torchtext for natural language processing, empowering developers to tackle diverse AI challenges efficiently.
  • Pythonic Interface: With its intuitive and Pythonic interface, PyTorch prioritizes developer productivity, enabling rapid prototyping and experimentation without compromising on performance or flexibility.

Why PyTorch?

  • Ease of Use: PyTorch's user-friendly interface and dynamic graph computation make it accessible to developers of all skill levels, empowering them to quickly grasp and implement complex deep learning models.
  • Research Flexibility: PyTorch's dynamic computational graph construction and automatic differentiation capabilities provide researchers with unparalleled flexibility, allowing for rapid experimentation and iterative model refinement.
  • Community Support: Backed by a vibrant community of developers, researchers, and contributors, PyTorch boasts extensive documentation, tutorials, and community forums, fostering collaboration and knowledge sharing within the AI community.
  • Production Readiness: While PyTorch excels in research and prototyping, it also offers robust deployment options, with seamless integration with popular deployment frameworks like TorchServe and ONNX, ensuring smooth transition from development to production environments.
  • Industry Adoption: PyTorch's versatility, performance, and extensive ecosystem have propelled its adoption across industries, with leading organizations leveraging PyTorch to drive innovation in areas such as computer vision, natural language processing, and reinforcement learning.

Comparison of Conda and pip

Both Conda and pip are popular package managers for Python, each with its advantages and potential disadvantages.

Conda

Advantages:

  • Manages both Python packages and non-Python dependencies.

  • Simplifies package management by creating isolated environments.

  • Ensures consistent package versions across different environments.

Disadvantages:

  • Larger package sizes due to including dependencies.

  • Limited availability of some packages compared to pip.

Pip

Advantages:

  • Lightweight and straightforward package manager.

  • Provides access to a vast ecosystem of Python packages.

Disadvantages:

  • Does not handle non-Python dependencies.

  • May lead to dependency conflicts in complex environments.

Installing PyTorch via Conda

Conda is a popular package management system that simplifies the installation and management of software packages. Users can follow these steps to install PyTorch using Conda.

Step 1. Opening Terminal

The user should open the terminal on their Ubuntu 22.04 system. In Ubuntu, you can open the terminal using one of the following methods:

1. Using Keyboard Shortcut

  • Press Ctrl + Alt + T to quickly open a new terminal window.

2. Using Dash/Search

  • Press the Super/Windows key to open the Dash.
  • Type Terminal in the search bar.
  • Click on the Terminal icon that appears in the search results to open it.

3. Using Applications Menu

  • Click on the Applications menu located at the top left corner of the screen.
  • Navigate to Accessories or Utilities and find the Terminal application.
  • Click on Terminal to open it.

Step 2. Creating a New Conda Environment (Optional)

Creating a new environment is recommended to isolate PyTorch from other packages. The user can create a new environment with the following command:

conda create -n pytorch_env python=3.9

Step 3. Activating the Environment

Users should activate the newly created environment using the following command:

conda activate pytorch_env

Step 4. Installing PyTorch

Users should run the following command to install PyTorch with CUDA support:

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch -c conda-forge

Installing PyTorch via pip

Pip is a package manager for Python packages. Although Conda is recommended for PyTorch installation, pip can also be used. Users can follow these steps.

Step 1. Ensuring Dependencies are Installed

Users should ensure that they have the necessary dependencies installed by running the following command:

sudo apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6

Step 2. Installing PyTorch

PyTorch can be installed with CUDA support using pip:

pip install torch torchvision torchaudio

Verifying PyTorch Installation

After installation, it's crucial to verify that PyTorch is correctly installed. Follow these steps.

Step 1. Launching Python Interpreter

Users should open the terminal and launch the Python interpreter by typing the following command:

python3

Step 2. Importing PyTorch

Inside the Python interpreter, users can import PyTorch by using the following piece of code:

import torch

Step 3. Checking PyTorch Version

Users should check the PyTorch version to ensure the installation was successful by using the following code:

print(torch.__version__)

Image1

Conclusion

Readers can congratulate themselves for successfully installing PyTorch on Ubuntu 22.04 using either Conda or pip. Now they're ready to dive into the world of deep learning and build powerful machine learning models with PyTorch.


Share