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:
const installPythonString = ` #!/bin/bash sudo amazon-linux-extras install -y epel sudo yum -y update sudo yum groupinstall "Development Tools" -y sudo yum install openssl-devel libffi-devel bzip2-devel -y gcc --version sudo yum install wget -y sudo mkdir -p /opt/python3.9/ sudo chown -R $USER:$USER /opt/python3.9/ wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz -P /opt/python3.9 cd /opt/python3.9/ tar xvf Python-3.9.9.tgz whoami sudo chown -R $USER:$USER Python-3.9.9 cd Python-3.9.9/ ls -al pwd ./configure --enable-optimizations sudo make altinstall python3.9 --version pip3.9 --version `; init.addConfig('install_python39', new ec2.InitConfig([ ec2.InitFile.fromString('/opt/install_python39.sh', installPythonString, { mode: '000755', owner: 'root', group: 'root', }), ec2.InitCommand.shellCommand('sudo sh install_python39.sh', { cwd: '/opt', key: 'install_python39', }), ]))
I’m getting the following errors when trying to start up the EC2 instance:
Python build finished successfully! ... WARNING: The script pip3.9 is installed in '/usr/local/bin' which is not on PATH. install_python39.sh: line 21: python3.9: command not found install_python39.sh: line 22: pip3.9: command not found
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.