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.
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.
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.
On older Amazon Linux 2 images, yum may still be the right tool.
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.
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.
Then install the application packages inside the environment.
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.
Inside the virtual environment, the path should change:
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
- How do I log in to AWS Console with an IAM user account?
- How do I look up a cognito user by their sub/UUID?
- How do I make cloud-init startup scripts run every time my EC2 instance boots?
- How do I pass arguments to AWS Lambda functions using GET requests?
- How do I install Python OpenCV through Conda?
- How do I install tensorflow_text?
- How do I perform the k8s/helm setup of YB?
- How do I put object to amazon s3 using presigned url?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.