AWS
EC2
Python 3
Installation Guide
Cloud Computing

How do I install Python 3 on an AWS EC2 instance?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Installing Python 3 on EC2 is usually straightforward, but the exact commands depend on the Linux distribution behind the AMI. The most reliable workflow is to identify the distro first, install Python from the system package manager, verify the interpreter path, and then use a virtual environment for application dependencies.

Check the Instance Operating System First

Do not guess whether the instance is Amazon Linux, Ubuntu, or something else. Connect by SSH and inspect the OS details.

bash
ssh -i your-key.pem ec2-user@your-ec2-public-dns
cat /etc/os-release

That one step prevents running apt commands on an RPM-based image or dnf commands on Ubuntu.

Install on Amazon Linux

For Amazon Linux 2023, dnf is the usual package manager.

bash
1sudo dnf update -y
2sudo dnf install -y python3 python3-pip
3python3 --version
4pip3 --version

On older Amazon Linux 2 images, yum may still be the right tool.

bash
sudo yum update -y
sudo yum install -y python3
python3 --version

The exact package names are similar, but the package manager should match the AMI generation.

Install on Ubuntu

On Ubuntu-based EC2 instances, use apt.

bash
1sudo apt update
2sudo apt install -y python3 python3-pip python3-venv
3python3 --version
4pip3 --version

Installing python3-venv at the same time is useful because it gives you the standard virtual-environment tooling immediately.

Create a Virtual Environment for Applications

Even after Python is installed, do not treat the system interpreter as your application environment. Isolate project dependencies with venv.

bash
1mkdir -p ~/apps/myservice
2cd ~/apps/myservice
3python3 -m venv .venv
4source .venv/bin/activate
5python -m pip install --upgrade pip

Then install the application packages inside the environment.

bash
pip install fastapi uvicorn

This keeps the instance's base Python separate from the app's dependency set, which reduces conflicts and makes deployments more predictable.

Verify the Interpreter Path Explicitly

When people say "Python is installed but the service still uses the wrong version," the real issue is often path resolution. Check which interpreter your shell or service is actually using.

bash
which python3
python3 -c "import sys; print(sys.executable)"

Inside the virtual environment, the path should change:

bash
source .venv/bin/activate
python -c "import sys; print(sys.executable)"

This is especially important for systemd units, cron jobs, and deployment scripts where the environment may differ from your interactive shell.

Consider Version Pinning Carefully

If the package-manager version is not sufficient for your application, tools such as pyenv can install a more specific Python release in user space. That is useful, but it also increases operational complexity.

For many EC2 workloads, the simpler and safer baseline is: use the distro-packaged Python for the machine and isolate app dependencies with a virtual environment.

Only reach for custom runtimes when the application genuinely requires them.

Common Pitfalls

Running commands for the wrong Linux distribution is the fastest way to waste time on EC2 setup.

Installing app dependencies into the system interpreter instead of a virtual environment makes upgrades and debugging harder.

Assuming python, python3, and service-unit interpreter paths all point to the same binary often leads to deployment surprises.

Summary

  • Identify the EC2 instance OS before choosing installation commands.
  • Use the distro package manager to install Python 3 and pip.
  • Create a virtual environment for application dependencies.
  • Verify the actual interpreter path in both interactive and service contexts.
  • Keep the setup simple unless you truly need a custom Python version manager.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.