How to install PyTorch on Linux (AlmaLinux)

Reading Time: 7 minutes

This help document provides a detailed with regard to how to install PyTorch on AlmaLinuxusing Liquid Web servers, focusing on best practices and considerations for a successful setup.

By following the guidelines outlined in this article, users can seamlessly set up PyTorch on their AlmaLinux server hosted on Liquidweb, ensuring optimal performance and efficiency in developing and training machine learning models.

Key points

Here is a list of the key points you will find in this help article:

  • A better understanding of what PyTorch is in terms of the software and its purpose
  • How to install PyTorch on AlmaLinux using Anaconda or PIP
  • A high-level overview of how to use PyTorch
  • How to update PyTorch using Anaconda or PIP
  • Steps for unisntalling PyTorch using Anaconda or PIP
  • Deploying and managing PyTorch on your servers

What is PyTorch?

PyTorch is a machine learning (ML) framework from the open source community that builds deep neural networks with Python and the Torch library. It was created by Facebook's AI Research Lab (FAIR) and is renowned for its adaptability, simplicity, and capacity for dynamic computation. Due to its intuitive interface and efficient debugging capabilities, it is a crucial tool for researchers, developers, and practitioners in the machine-learning community.

PyTorch's dynamic computation graph structure makes creating and modifying neural networks simple, which is why it is frequently used in domains like computer vision, natural language processing, and reinforcement learning. It also supports GPU acceleration, enabling faster computations, and is ideal for training large-scale models. PyTorch's versatility and performance have made it a leading machine learning library, driving advancements in AI research and development.

Installing PyTorch on Linux (AlmaLinux)

To install PyTorch on AlmaLinux, set up the prerequisites and follow the below given in this post.

Prerequisites

Here is the short list of prerequisites for installing PyTorch on AlmaLinux:

  • Operating System: AlmaLinux 8 installed on your Liquid Web server.
  • SSH Access: Regardin your system, root or sudo access is needed to install PyTorch on your Liquid Web server.
  • CPU: Minimum: 2 CPU cores. Recommended: 4 CPU cores or more.
  • RAM: Minimum: 8 GB RAM. Recommended: 16 GB RAM or more.
  • Python: 3.6 or higher. In this article, we used the Python 3.11.7 version.

Step #1. Access your Liquid Web server

As a Liquid Web customer, follow these steps to connect to your Liquid Web server via SSH and install PyTorch.

1.1. Open the Terminal (Mac/Linux) or Command Prompt (Windows)

Terminal and Command Prompt can be found in your system's applications or by searching in the Start menu.

1.2. Connect to your Liquid Web Server

Run the ssh command and your server's IP address or hostname to establish a connection to your Liquid Web server. Replace your_username with the SSH username you received from Liquid Web and your_server_ip with your server's IP address or hostname:

ssh your_username@your_server_ip

1.3. Enter your SSH password

You'll be asked to input the SSH password that Liquid Web sent you.

1.4. Verify your connection

After connecting, a command prompt should show you that you are signed into your Liquid Web server over SSH. You can now proceed to install PyTorch on your Liquid Web Server.

Step #2. Update your AlmaLinux system

Before installing PyTorch, Liquid Web customers must update their server's operating system for security and performance reasons. You can issue this command to update your AlmaLinux system:

sudo dnf update

Step #3. How to install PyTorch using Anaconda or PIP

You can install PyTorch on AlmaLinux using Anaconda or PIP.

Option 3.1. Installing PyTorch via Anaconda on AlmaLinux

Anaconda is a robust package manager for Python and R, a widely used programming language for data analysis, graphical data visualization, and statistical computation.

3.1.1. Install Anaconda

To install Anaconda, please follow the these steps,

3.1.1.1. Create a directory for Anaconda and navigate to it

Run the following commands to create a directory for Anaconda and change the current directory to it:

mkdir anaconda
cd ~/anaconda
3.1.1.2. Download the Anaconda installer for AlmaLinux

Download the Anaconda installer for AlmaLinux from the official Anaconda website using wget:

sudo wget https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh

Here is the output:

[root@host anaconda]# sudo wget https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh
--2024-03-26 12:29:42--  https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh
Resolving repo.anaconda.com (repo.anaconda.com)... 104.16.131.3, 104.16.130.3, 2606:4700::6810:8303, ...
Connecting to repo.anaconda.com (repo.anaconda.com)|104.16.131.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1045673900 (997M) [application/octet-stream]
Saving to: 'Anaconda3-2024.02-1-Linux-x86_64.sh'

Anaconda3-2024.02-1-Linux-x86_64.sh                         100%[=========================================================================================================================================>] 997.23M   144MB/s    in 7.4s    

2024-03-26 12:29:50 (135 MB/s) - 'Anaconda3-2024.02-1-Linux-x86_64.sh' saved [1045673900/1045673900]
3.1.1.3. Provide the execute permission to the script

To provide the execute permission to the script, run the following command:

sudo chmod +x ./Anaconda3-2024.02-1-Linux-x86_64.sh
3.1.1.4. Run the script to install Anaconda

To run the Anaconda installer script, run the following command:

sudo ./Anaconda3-2024.02-1-Linux-x86_64.sh

To accept, scroll through the license agreement and select the "Yes" option. You can enter a different location or select the default ~/anaconda when asked where to install the application.

Here is the output:

[root@host anaconda]# sudo ./Anaconda3-2024.02-1-Linux-x86_64.sh
Welcome to Anaconda3 2024.02-1
In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> ENTER
-----
-----

Thank you for installing Anaconda3!
3.1.1.5. Verify Anaconda installation

Once Anaconda is installed, you can confirm the installation by running the following command:

conda --version

Here is the output:

[root@host anaconda]# conda --version
conda 24.1.2
3.1.1.6. Initialize Anaconda after installation

Run the following command to initialize Anaconda after installation and accept the prompt:

conda init

3.1.2. How to install PyTorch

Option 3.1.2.1. Install PyTorch with CPU support only

To install PyTorch with CPU support only, run the following command:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

Here is the output:

[root@host anaconda]# conda install pytorch torchvision torchaudio cpuonly -c pytorch
Channels:
 - pytorch
 - defaults
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /root/anaconda/ENTER

  added / updated specs:
    - cpuonly
    - pytorch
    - torchaudio
    - torchvision

----
----

Downloading and Extracting Packages:
                                                                                                                                                                                                                                              
Preparing transaction: done                                                                                                                                                                                                                   
Verifying transaction: done                                                                                                                                                                                                                   
Executing transaction: done  

Option 3.1.2.2. Install PyTorch with CUDA (GPU support)

To install PyTorch with CUDA (GPU support), run the following command:

pip3 conda install pytorch torchvision torchaudio pytorch-cuda=<version> -c pytorch -c nvidia

Replace the <version> as per your requirement. In the example below; version 11.8 is used.

Here is the output:

[root@host anaconda]# conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
Channels:
 - pytorch
 - nvidia
 - defaults
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /root/anaconda/ENTER

  added / updated specs:
    - pytorch
    - pytorch-cuda=11.8
    - torchaudio
    - torchvision
----
----
Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done

Step 3.1.3. Verify PyTorch installation

To confirm PyTorch installation, run the following commands one by one:

python
import torch
print(torch.__version__)

Here is the output:

[root@host ~]# python
Python 3.11.7 (main, Dec 15 2023, 18:12:31) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
2.2.1
>>> quit()

Option 3.2: Installing PyTorch using PIP on AlmaLinux

PIP is another popular package manager within the Python community. It helps developers quickly install and manage Python packages and dependencies. Refer to the How to Install the PIP Package Manager on AlmaLinux article to learn more about PIP.

3.2.1. Install python3-virtualenv

Installing python3-virtualenv is the first step in creating an isolated Python environment for your project. This isolated Python environment will save you from installing Python packages globally and avoid potential compatibility problems.

To install python3-virtualenv, run the following command:

sudo dnf install python3-virtualenv

3.2.2. Prepare the environment

To set up the environment, first make a new directory for your project, which will store the files and settings for the virtual environment, and then move to that directory. The directory name is pytorch_env, however you can change it if necessary:

mkdir pytorch_env
cd pytorch_env

Then, run the following commands to set up and activate the virtual environment:

python3 -m venv pytorch_env
source pytorch_env/bin/activate

You should see the name of the virtual environment appear on your command prompt when it has been activated. With this option enabled, you can install Python packages or scripts connected with this virtual environment instead of the system-wide Python, avoiding issues with projects that require different package versions.

3.2.3. How to install PyTorch

To install PyTorch with CPU support only, run the following command:

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

Here is the output:

(pytorch_env) [root@host pytorch_env]# pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
Looking in indexes: https://download.pytorch.org/whl/cpu
---
---
Installing collected packages: mpmath, typing-extensions, sympy, pillow, numpy, networkx, MarkupSafe, fsspec, filelock, jinja2, torch, torchvision, torchaudio
Successfully installed MarkupSafe-2.1.3 filelock-3.9.0 fsspec-2023.4.0 jinja2-3.1.2 mpmath-1.3.0 networkx-3.2.1 numpy-1.26.3 pillow-10.2.0 sympy-1.12 torch-2.2.2+cpu torchaudio-2.2.2+cpu torchvision-0.17.2+cpu typing-extensions-4.8.0

To install PyTorch with CUDA (GPU support), run the following command:

pip3 install torch torchvision torchaudio

3.2.4. Verify PyTorch installation

To confirm PyTorch installation, run the following commands one by one:

python
import torch
print(torch.__version__)

Here is the output:

[root@host pytorch_env]# python
Python 3.11.7 (main, Dec 15 2023, 18:12:31) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
2.2.2+cpu
>>> quit()

To learn how to install PyTorch on Ubuntu, please refer to the article Installing PyTorch on Ubuntu: A Guide.

Step #4. How to use PyTorch

PyTorch is useful for tensor operations because it provides a powerful library for creating and manipulating tensors efficiently. You can use the following steps to perform tensor operations.

4.1. Import PyTorch

To import the PyTorch library, run the following command:

import torch

4.2. Create Tensors

Create two tensors and perform addition:

# Create tensors
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
y = torch.tensor([[7, 8, 9], [10, 11, 12]])

# Perform tensor addition
z = x + y

4.3. Print the result

The print(z) statement prints the result of the addition, the tensor z:

print(z)

The output shows the values of the tensor z, which is the element-wise sum of x and y.

Here is the output:

tensor([[ 8, 10, 12],
        [14, 16, 18]])

Updating PyTorch

Depending on how you installed it, you can use Anaconda or PIP to update PyTorch,

Updating PyTorch with Andconda

If you installed PyTorch using Anaconda, you can update it by running the following command:

conda update pytorch torchvision torchaudio

Updating PyTorch with PIP

If you installed PyTorch using PIP, you can update it by running the following command:

pip3 install --upgrade torch torchvision torchaudio

Uninstalling PyTorch

You can use Anaconda or PIP to remove PyTorch, depending on how you installed it.

Uninstall PyTorch with Anaconda

To uninstall PyTorch using Anaconda, follow these steps in the next sections.

1. Activate the Anaconda environment

To activate the Anaconda environment, run the following command:

conda activate pytorch_env

2. Uninstall PyTorch

If you install PyTorch with CPU support only, run the following command to uninstall PyTorch:

conda uninstall pytorch torchvision torchaudio cpuonly

If you install PyTorch with CUDA (GPU support), run the following command to uninstall PyTorch:

conda uninstall pytorch torchvision torchaudio pytorch-cuda=11.8

3. Verify the uninstallation

To verify the uninstallation, you can try to import PyTorch in a Python shell by running the following command:

python -c "import torch"

This command should produce an error indicating that the module cannot be found.

4. Deactivate the environment

To deactivate the environment, run the following command:

conda deactivate

Uninstall PyTorch with PIP

To uninstall PyTorch using PIP, follow these steps.

1. Activate the v environment (virtual environment)

You can activate your virtual environment by running this command:

source pytorch_env/bin/activate

2. Uninstall PyTorch

To uninstall PyTorch, run the following command:

python -c "import torch"

3. Verify the uninstallation

To verify the uninstallation, you can try to import PyTorch in a Python shell by running the following command:

conda deactivate

Deploying PyTorch with confidence on your Liquid Web servers

Liquid Web is a perfect choice for hosting PyTorch on AlmaLinux servers, offering a seamless installation process and robust support. The detailed walkthrough on how to install PyTorch ensures that users, regardless of expertise, can navigate the setup effortlessly. Liquid Web's commitment to performance and reliability shines through its hosting plans, promising a stable environment for PyTorch projects of any scale.

By ordering a hosting plan from Liquid Web, users gain access to top tier infrastructure and our web hosting professionals ready to guide you at every step. This combination of reliable hosting and comprehensive support makes Liquid Web the ideal partner for deploying PyTorch applications. Don't hesitate to elevate your PyTorch experience — move with Liquid Web today and unlock the full potential of your projects.

Avatar for Mohammed Noufal

About the Author: Mohammed Noufal

Mohammed Noufal is a B.Tech graduate with a decade of experience in server administration and web hosting. He is a father to two daughters and finds fulfillment in their growth. In his free time, he enjoys blogging, sharing experiences, and listening to music. With a strong technical background, family commitment, and creative outlets, he represents a well-rounded life journey.

Latest Articles

How to install PyTorch on Linux (AlmaLinux)

Read Article

Innovative armor — exploring ThreatDown’s impact on cyber defense

Read Article

Controlling PHP settings with a custom php.ini file

Read Article

Linux dos2unix command syntax — removing hidden Windows characters from files

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article