Skip to content

Install PyTorch on Amazon EC2 instances with NVIDIA GPUs running Amazon Linux 2023 (AL2023)

5 minute read
Content level: Intermediate
2

Steps to install PyTorch on AL2023 (Amazon Linux 2023) (x86_64/arm64) NVIDIA GPU instances

Overview

This article suggests how to install PyTorch using pip on NVIDIA GPU EC2 instances running AL2023 (Amazon Linux 2023)

Other Options

If you need AMIs preconfigured with NVIDIA GPU driver and PyTorch framework, consider AWS Deep Learning AMIs. Refer to Release Notes for Single Framework DLAMIs, PyTorch DLAMIs for currently supported options, and Deep Learning graphical desktop on Amazon Linux 2023 (AL2023) with AWS Deep Learning AMI (DLAMI) for graphical desktop setup guidance.

Prerequisites

Go to Service Quotas console of your desired Region to verify On-Demand Instance quota value of your desired instance type:

Service Quota

Request quota increase if the assigned value is less than vCPU count of your desired EC2 instance size. Do not proceed until your applied quota value is equal or higher than your instance type vCPU count

Prepare Amazon Linux 2023

Launch a new NVIDIA GPU instance running Amazon Linux 2023 preferably with at least 20 GB storage

Launch AL2023 EC2

Install NVIDIA GPU drivers

Refer to article Install NVIDIA GPU driver, CUDA toolkit, NVIDIA Container Toolkit on Amazon EC2 instances running Amazon Linux 2023 (AL2023) for NVIDIA GPU driver (and optionally NVIDIA Container Toolkit) installation guidance. CUDA Toolkit, cuDNN and other NVIDIA libraries are usually not required.

Verify integrity of NVIDIA GPU driver installation, and run nvidia-smi to get the supported CUDA Version.

In the output below, the installed NVIDIA driver supports up to CUDA 13.0

....
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.105.08             Driver Version: 580.105.08     CUDA Version: 13.0     |
+-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
...

Do not proceed until NVIDIA driver is installed and working properly

Connect to the instance as ec2-user

Install Python 3.1x

AL2023 system Python version is 3.9

At time of writing, PyTorch requires Python version 3.10 or higher,

Install newer Python version

Install Python 3.13 or Python 3.14

sudo dnf install -y python3.13 python3.13-pip

Create Python venv

Setup a Python 3.13 virtual environment in /home/ec2-user/pytorch to install and run PyTorch

cd ~
python3.13 -m venv pytorch

Activate venv

Activate virtual environment and verify Python version is 3.13

cd
source pytorch/bin/activate
pip3 install --upgrade pip
python3 --version
pip3 --version

Output should be similar to below

[ec2-user@ip-172-31-30-16 ~]$ source pytorch/bin/activate
(pytorch) [ec2-user@ip-172-31-16-179 ~]$ pip3 install --upgrade pip
Requirement already satisfied: pip in ./pytorch/lib64/python3.13/site-packages (24.2)
Collecting pip
  Using cached pip-25.3-py3-none-any.whl.metadata (4.7 kB)
Using cached pip-25.3-py3-none-any.whl (1.8 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 24.2
    Uninstalling pip-24.2:
      Successfully uninstalled pip-24.2
Successfully installed pip-25.3
(pytorch) [ec2-user@ip-172-31-30-16 ~]$ python3 --version                                                                                                                                            
Python 3.13.3         
(pytorch) [ec2-user@ip-172-31-30-16 ~]$ pip3 --version
pip 25.3 from /home/ec2-user/pytorch/lib64/python3.13/site-packages/pip (python 3.13)

Install PyTorch

Get PIP install command

Go to PyTorch Get Started and select the following

  • PyTorch Build: Stable
  • Your OS: Linux
  • Package: PIP
  • Language: Python
  • Compute Platform: Latest CUDA version supported by your NVIDIA driver, e.g. CUDA 13.0

PyTorch PIP install command

Copy the PIP install script in row Run this Command:

Refer to Installing previous versions of PyTorch if you wish to install a previous version

Install PyTorch

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

Output should be similar to below

(pytorch) [ec2-user@ip-172-31-30-16 ~]$ pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu130                                      
Looking in indexes: https://download.pytorch.org/whl/cu130 
...
Installing collected packages: nvidia-cusparselt-cu13, mpmath, typing-extensions, triton, sympy, setuptools, pillow, nvidia-nvtx, nvidia-nvshmem-cu13, 
nvidia-nvjitlink, nvidia-nccl-cu13, nvidia-curand, nvidia-cufile, nvidia-cuda-runtime, nvidia-cuda-nvrtc, nvidia-cuda-cupti, nvidia-cublas, numpy, networkx, 
MarkupSafe, fsspec, filelock, nvidia-cusparse, nvidia-cufft, nvidia-cudnn-cu13, jinja2, nvidia-cusolver, torch, torchvision
Successfully installed MarkupSafe-3.0.2 filelock-3.19.1 fsspec-2025.9.0 jinja2-3.1.6 mpmath-1.3.0 networkx-3.5 numpy-2.3.3 nvidia-cublas-13.0.0.19 
nvidia-cuda-cupti-13.0.48 nvidia-cuda-nvrtc-13.0.48 nvidia-cuda-runtime-13.0.48 nvidia-cudnn-cu13-9.13.0.50 nvidia-cufft-12.0.0.15 nvidia-cufile-1.15.0.42 
nvidia-curand-10.4.0.35 nvidia-cusolver-12.0.3.29 nvidia-cusparse-12.6.2.49 nvidia-cusparselt-cu13-0.8.0 nvidia-nccl-cu13-2.27.7 nvidia-nvjitlink-13.0.39 
nvidia-nvshmem-cu13-3.3.24 nvidia-nvtx-13.0.39 pillow-11.3.0 setuptools-70.2.0 sympy-1.14.0 torch-2.9.1+cu130 torchvision-0.24.1+cu130 triton-3.5.1 
typing-extensions-4.15.0

Notice that nvidia-* packages including nvidia-cuda-runtime are installed.

Verify installation

Start Python shell

In your Python virtual environment, start a Python shell

python3
(pytorch) [ec2-user@ip-172-31-30-16 ~]$ python3
Python 3.13.3 (main, Oct 17 2025, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Verify CUDA

Run the following commands

import torch
torch.cuda.is_available()
torch.version.cuda
torch.cuda.device_count()

PyTorch

Output should be similar to below

>>> import torch
>>> torch.cuda.is_available()
True
>>> torch.version.cuda
'13.0'
>>> torch.cuda.device_count()
1

Refer to PyTorch documenation for more information

Install script

To automate install, you can use the below as install script

#!/bin/bash
sudo dnf install -y python3.13 python3.13-pip

cd ~
python3.13 -m venv pytorch
source pytorch/bin/activate
pip3 install --upgrade pip

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

Updating PyTorch

To update PyTorch

cd ~
source pytorch/bin/activate
pip3 install --upgrade torch torchvision --index-url https://download.pytorch.org/whl/cu130
deactivate
AWS
EXPERT

published a year ago1.1K views