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.
Both Conda and pip
are popular package managers for Python, each with its advantages and potential disadvantages.
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
.
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.
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.
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
Ctrl + Alt + T
to quickly open a new terminal window.2. Using Dash/Search
Super/Windows
key to open the Dash.Terminal
in the search bar.3. Using Applications Menu
Applications
menu located at the top left corner of the screen.Accessories
or Utilities
and find the Terminal
application.Terminal
to open it.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
Users should activate the newly created environment using the following command:
conda activate pytorch_env
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
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.
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
PyTorch can be installed with CUDA support using pip
:
pip install torch torchvision torchaudio
After installation, it's crucial to verify that PyTorch is correctly installed. Follow these steps.
Users should open the terminal and launch the Python interpreter by typing the following command:
python3
Inside the Python interpreter, users can import PyTorch by using the following piece of code:
import torch
Users should check the PyTorch version to ensure the installation was successful by using the following code:
print(torch.__version__)
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.