How to Install Python on Amazon Linux 2023
Dear Reader, I hope you are doing amazing today. In my last few posts, I have shared with you how to install Git, Ansible, Nginx, Apache Web Server, Docker, Java etc. on Amazon Linux 2023 or AL2023 EC2 Instance.
In this article, we’ll discuss how to install Python on Amazon Linux 2023 EC2 instance.
So are you ready?
Alright, let’s start.
Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.
A bit on Python and Amazon Linux 2023(AL2023)
Python: Python is an interpreted, object-oriented and high level programming language. It is the goto choice for a beginner because it is easy to learn and use. It is most popular language for machine learning and artificial intelligence. However you can use it to-
- Web development
- Software development
- Data Science
- Scientific computing
- System Administration
- Automation etc.
Amazon Linux 2023: Amazon Linux 2023 or AL2023 is the next-generation Amazon Linux AMI provided by AWS. It is rpm based and optimized for the cloud. It is designed with the latest Linux innovations to deliver high-performance and stable Linux systems.
Prerequisite
Alright, before you install Python on your Amazon Linux 2023 or AL2023 EC2 instance, You need to have an EC2 instance up and running.
Here is how you can launch an Amazon Linux 2023 easily-
Additionally, if you don’t have a free tier account, check out how to setup a free tier AWS account in right way. Having said that, don’t forget to set up a cost budget to avoid last-minute billing shocks.
Steps to Install Python on Amazon Linux 2023 Instance
- SSH into your Amazon Linux 2023 Instance
- Run System Update
- Check If Python is Installed in your EC2
- Search for Available Python Package
- Install Python on Amazon Linux 2023
- Verify Python Installation
- Switch Between Different Python Versions (Optional)
- Creating Virtaiul Env With Specific Python Version
Step 1: SSH into your Amazon Linux 2023 Instance
By this time, I assume you already have an Amazon Linux 2023 instance up and running. Therefore you are all set to install Python on it. However If that’s not the case, feel free to launch one using one of my previous tutorials.
Once your Amazon Linux 2023 instance is up and running, select your instance and click on Connect as shown in the below screenshot.
Verify that ec2-user is showing in the user name field and click Connect again.
In a matter of seconds, you should be connected to your instance as shown below.
Important Note: If you prefer connecting from your local, feel free to use the command- ssh -i /path/my-key-pair.pem ec2-user@instance-public-ip
Step 2: Run System Update
Before we install any software in your Amazon Linux 2023 instance, it’s recommended to update your system packages to the latest.
You can run the system update using the below command-
sudo dnf update
Type sudo dnf update and hit enter.
Step 3: Check If Python is Installed in your EC2
Let’s check if Python3 is installed on Amazon Linux 2023.-
python3 --version
As you can see, Amazon Linux 2023 comes preinstalled with Python 3.9.16 at the time of writing this tutorial.
If this is all you need, you should be good to go. However, if you need to install a different version of Python such as Python 3.11 stay with me till the end.
Step 4: Search for Available Python Package on AL2023
You can search all Python related packages using-
sudo dnf search python3
However, if you run the above command, you will get tons of packages in response. To narrow down my search I added a ‘.’
sudo dnf search python3.
As you can notice in the above screenshot, Python 3.11 and related packages are available(3.10 is not available at the moment as AWS recommends using 3.11). If you need this latest version of Python in your Amazon Linux 2023 instance, let’s go ahead and install it.
Step 5: Install Python 3.11 on Amazon Linux 2023
As you saw the package name above, the one line to install Python 3.11 on your Amazon Linux 2023 would be –
sudo dnf install python3.11 -y
I removed -y from the above command as I wanted to check what’s getting installed.
Type y and hit enter. Python 3.11 should be installed in a minute in your Amazon Linux 2023.
Step 6: Verify Python Installation
As we just installed Python 3.11, let’s verify it.
python3.11 --version
Congratulation !!!
You have successfully installed Python 3.11 on your Amazon Linux 2023 EC2 Instance.
However, do you know that if you type python3 –version, it will still return you 3.9.16(the default version)
If you want to work with multiple versions, the best way is to create a Python virtual environemnt.
Step 7: Switch Between Different Python Versions (Optional)
Although the best way to manage different versions of Python is to create virtual environments and use them as per need.
I wanted to try switching to a different version of Python globally on my Amazon Linux 2023. I did it in below steps.
1.Check all the installed Python Versions
ls -ls /usr/bin/python*
For me it is Python 3.9 and 3.11 as you can see in above screenshot.
2. Add Python 3.9 and Python 3.11 to update-alternatives
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
3.Update Python 3 for point to Python 3.11
sudo update-alternatives --config python3
As two options are shown, I entered 2 as I need Python 3.11.
4.Verify Python Version Again
python3 --version
Step 8: Creating Virtaiul Env With Specific Python Version
The problem with changing default python version is, a lot of things starts breaking for example-
Traceback (most recent call last):
File “/usr/bin/dnf”, line 61, in
from dnf.cli import main
ModuleNotFoundError: No module named ‘dnf’
So we are back to square 1. Let’s change the default Python version back to 3.9 by using sudo update-alternatives –config python3. And this time entering 1.
We’ll try different way to use Python 3.11.
Let’s try to create a virtual environment by using the below command-
python3.11 -m venv demo_app //Create a Virtual Environment using Python 3.11
source demo_app/bin/activate //Activate Created Environment
python3 --version //Check Python Version
deactivate //Deactivate environemnt
python3 --version //Check Python Version Again
Did you notice that after activating the virtual environment Python version is shown as 3.11? And as soon as you deactivate it, you get back python 3.9 again.
That’s what we wanted right?
Alright !! Let’s conclude this post.
Conclusion
In this how-to guide, we talked about How to Install Python on Amazon Linux 2023 Instance.
We started with checking the Python version on Amazon Linux 2023. Then we installed a newer version using dnf. After that, we verified the Python installation.
Last but not least, we saw the best way to manage multiple Python versions on Amazon Linux 2023.
Were you able to install Python on Amazon Linux 2023 using this tutorial? Let me know in the comment section.
Enjoyed the content?
Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.
Don’t forget to motivate us-