PyTorch is an open-source machine learning library developed by Facebook’s AI Research lab. It provides a flexible and powerful platform for building deep learning models. In this tutorial, I will guide you through the step-by-step process of installing PyTorch on Ubuntu 22.04, so that you can start building and training your own deep learning models.
Step 1: Update and Upgrade System Packages
Before installing PyTorch, it is always recommended to update and upgrade the system packages to ensure that you have the latest versions of all software installed on your system. Open a terminal window and run the following commands:
sudo apt update
sudo apt upgrade
Step 2: Install Python and pip
PyTorch requires Python to be installed on your system. Ubuntu 22.04 comes with Python pre-installed, but you may need to install pip, the Python package manager. You can do this by running the following command:
sudo apt install python3-pip
Step 3: Install PyTorch
There are several ways to install PyTorch on Ubuntu, but the easiest and most recommended method is to use pip. PyTorch provides pre-built binaries that can be installed using pip. To install the latest stable version of PyTorch, run the following command:
pip install torch torchvision torchaudio
This will install the latest stable release of PyTorch along with the torchvision and torchaudio libraries, which are commonly used for computer vision and audio tasks, respectively.
Step 4: Verify the Installation
To verify that PyTorch has been installed successfully, you can open a Python shell and import the torch module. Run the following command:
python3
import torch
print(torch.__version__)
This should output the version of PyTorch that you have installed on your system. If you see the version number displayed without any errors, then PyTorch has been successfully installed.
Step 5: Test PyTorch Installation
To further verify that PyTorch is working correctly, you can run a simple test script that creates a random tensor and performs basic operations on it. Create a new Python script file called test_pytorch.py and add the following code:
import torch
# Create a random tensor
x = torch.rand(5, 3)
print(x)
# Perform basic operations
y = torch.rand(5, 3)
z = x + y
print(z)
Save the file and run it using the following command:
python3 test_pytorch.py
If you see the random tensor generated and basic operations performed without any errors, then PyTorch is working correctly on your system.
Step 6: Install CUDA (Optional)
If you have an NVIDIA GPU and want to take advantage of GPU acceleration in PyTorch, you can install CUDA, which is a parallel computing platform and application programming interface developed by NVIDIA. To install CUDA on Ubuntu, you can follow the official installation guide provided by NVIDIA.
Congratulations! You have successfully installed PyTorch on Ubuntu 22.04 and are ready to start building and training deep learning models. Remember to always refer to the official PyTorch documentation for more information on how to use the library and its various features. Happy coding!