Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
09.10.2024

How to Install PyTorch on AlmaLinux

Installing PyTorch on AlmaLinux is a straightforward process, though it does require some familiarity with the Linux command line and Python package management. In this guide, we’ll walk through the steps to get PyTorch running on an AlmaLinux machine. By the end of this tutorial, you will have PyTorch installed and ready to use for your machine learning or deep learning projects.

Prerequisites

Before starting the installation, ensure the following:

  • You have AlmaLinux installed on your machine.
  • You have sudo privileges.
  • Python 3.8 or later is installed.
  • pip is installed for managing Python packages.

If you don’t have Python or pip installed, you can install them using the following commands:

sudo dnf install python3
sudo dnf install python3-pip

Now, let’s proceed with installing PyTorch.

Step 1: Update System Packages

First, make sure your system packages are up to date to avoid compatibility issues during installation.

sudo dnf update -y

This command will update all installed packages to their latest versions.

Step 2: Install Python Development Tools

You need Python development tools and virtualenv to create an isolated Python environment for PyTorch.

sudo dnf groupinstall “Development Tools”
sudo dnf install python3-devel
sudo pip3 install virtualenv

Step 3: Create a Virtual Environment (Optional)

Creating a virtual environment is optional but recommended. It helps keep your Python projects organized and avoids package conflicts.

mkdir pytorch_env
cd pytorch_env
python3 -m venv venv
source venv/bin/activate

Now, your terminal should show that you are working inside the virtual environment ((venv) should appear before your prompt).

Step 4: Install PyTorch Using pip

To install PyTorch, you can use the official PyTorch installation command. Visit the PyTorch website and select the appropriate options (such as PyTorch build, OS, package manager, and CUDA version). Here, we will assume you want to install the latest stable version without GPU (CPU-only version):

pip install torch torchvision torchaudio

If you have a CUDA-compatible GPU and want to leverage it for PyTorch, you need to install the version that matches your CUDA version. For example, if you have CUDA 11.7 installed:

pip install torch torchvision torchaudio –index-url https://download.pytorch.org/whl/cu117

Make sure you have installed the corresponding CUDA version on your AlmaLinux system before using this command.

Step 5: Verify the Installation

After the installation is complete, you can verify that PyTorch is correctly installed by launching Python and running a simple script.

python

Then, within the Python interactive shell, type:

import torch
print(torch.__version__)
print(torch.cuda.is_available())

The output should display the installed PyTorch version. If you installed a CUDA-compatible version and have a CUDA-capable GPU, torch.cuda.is_available() should return True.

Step 6: Deactivating the Virtual Environment (If Used)

Once you have verified the installation, you can deactivate the virtual environment:

deactivate

To reactivate the environment in the future, navigate to the project directory and use:

source venv/bin/activate

Conclusion

You have successfully installed PyTorch on AlmaLinux! Now you can start building and running your deep learning models using PyTorch. By following this guide, you have also learned how to create a Python virtual environment, which is a useful skill for managing Python projects.

Happy coding with PyTorch! If you encounter any issues, be sure to check the PyTorch documentation for more detailed instructions and troubleshooting tips.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills