Skip to content
Advertisement

How to install python 3.9 on Amazon Linux 2 with cloud-init and CDK

I’m trying to install Python 3.9 an EC2 instance that uses Amazon Linux 2. I tried following this guide: https://computingforgeeks.com/install-latest-python-on-centos-linux/, and I was able to install Python3.9 manually on the EC2 instance by SSH’ing in and running the commands. I’m now trying to setup the EC2 instance with a UserData script that calls some CloudFormationInit scripts to install dependencies, including Python 3.9, and my script is failing.

Here’s part of the script that I’m using to install Python 3.9:

JavaScript

I’m getting the following errors when trying to start up the EC2 instance:

JavaScript

Is there an easier way to install Python 3.9 on Amazon Linux 2 using CloudFormationInit?

Advertisement

Answer

Looks like the path to the python is /usr/local/bin which is not in $PATH so the python3.9 command is not found.

run the following commands in order.

export PATH="/usr/local/bin:$PATH" or echo "export PATH='/usr/local/bin:$PATH' >> ~/.bashrc(if you do this relaunch the ssh session) to save it to bashrc so you don’t have to run the export everytime you log in.

python3.9 --version

additionally if you keep having issues, follow this to install python3.9, which is what i used, and everything went flawlessly.

if you have python packages that need to be installed, i would recommend creating a requirements.txt and using pip3.9 install -r requirements.txt to install them.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement