- How do I install Python 3 on an AWS EC2 instance?
- 12 Answers 12
- How to Install Python 3.11 on Amazon Linux 2
- Prerequisites
- Step 1 – Download Python 3.11
- Step 2 – Install Python 3.11 on Amazon Linux
- Step 3 – Check Python Version
- Step 4 – Create Python Virtual Environment
- Conclusion
- How do I install and use Python 3.8 on an Amazon EC2 Linux Instance?
- 5 Answers 5
- How to Install Python 3.8 on Amazon Linux
- Step 1 – Prerequaities
- Step 2 – Download Python 3.8
- Step 3 – Install Python 3.8
- Step 4 – Check Python Version
How do I install Python 3 on an AWS EC2 instance?
I’ve googled around and I can’t find anyone else who has this problem so I’m asking here. Do I have to manually download and install it?
Possibly yes, it may not yet be available for Amazon Linux (or clarify if you’re using a different OS).
Hi, yes it’s the ‘standard’ Amazon Linux OS. Happy to manually install but there are a few things missing ( and I thought it best to check before spending time messing about 🙂
12 Answers 12
sudo yum list | grep python3
you will see that while they don’t have a «python3» package, they do have a «python34» package, or a more recent release, such as «python36». Installing it is as easy as:
sudo yum install python34 python34-pip
This seems to be the simpler answer. Did AWS update their packages between when @Jake_Howard posted an answer and you did?
@SohanShirodkar you may have been unlucky and just caught a new release — their September release moved to python35 . here’s where you can see the newest releases: aws.amazon.com/amazon-linux-ami/#Release_Notes
In 2018, sudo yum install python36 is also available. also don’t forget to install latest pip: python3 -m pip install —user —upgrade pip and then you can python3 -m pip install —user virtualenv for a virtual env. ref: packaging.python.org/guides/installing-using-pip-and-virtualenv
Note: This may be obsolete for current versions of Amazon Linux 2 since late 2018 (see comments), you can now directly install it via yum install python3 .
In Amazon Linux 2, there isn’t a python36 in the default yum repos, instead there’s the Amazon Extras Library.
sudo amazon-linux-extras install python3
If you want to set up isolated virtual environments with it; using yum install ‘d virtualenv tools don’t seem to reliably work.
virtualenv --python=python3 my_venv
Calling the venv module/tool is less finicky, and you could double check it’s what you want/expect with python3 —version beforehand.
Other things it can install (versions as of 18 Jan 18):
[ec2-user@x ~]$ amazon-linux-extras list 0 ansible2 disabled [ =2.4.2 ] 1 emacs disabled [ =25.3 ] 2 memcached1.5 disabled [ =1.5.1 ] 3 nginx1.12 disabled [ =1.12.2 ] 4 postgresql9.6 disabled [ =9.6.6 ] 5 python3=latest enabled [ =3.6.2 ] 6 redis4.0 disabled [ =4.0.5 ] 7 R3.4 disabled [ =3.4.3 ] 8 rust1 disabled [ =1.22.1 ] 9 vim disabled [ =8.0 ] 10 golang1.9 disabled [ =1.9.2 ] 11 ruby2.4 disabled [ =2.4.2 ] 12 nano disabled [ =2.9.1 ] 13 php7.2 disabled [ =7.2.0 ] 14 lamp-mariadb10.2-php7.2 disabled [ =10.2.10_7.2.0 ]
@NickT python3-3.7.2-4.amzn2.0.1.x86_64 is actually installed through ‘yum install python3’ you don’t use amazon linux extras anymore for python
Here are the steps I used to manually install python3 for anyone else who wants to do it as it’s not super straight forward. EDIT: It’s almost certainly easier to use the yum package manager (see other answers).
Note, you’ll probably want to do sudo yum groupinstall ‘Development Tools’ before doing this otherwise pip won’t install.
wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz tar zxvf Python-3.4.2.tgz cd Python-3.4.2 sudo yum install gcc ./configure --prefix=/opt/python3 make sudo yum install openssl-devel sudo make install sudo ln -s /opt/python3/bin/python3 /usr/bin/python3 python3 (should start the interpreter if it's worked (quit() to exit)
How to Install Python 3.11 on Amazon Linux 2
Amazon Linux 2 is an operating system developed by the team of Amazon Web Services (AWS). You can launch an Amazon ec2 instance using this operating system. Also, the disk images are available for major hypervisor platforms.
Python is a powerful, general-purpose programming language. It is very friendly and easy to learn. During the writing of this tutorial, Python 3.11 is the latest version available for installation. This tutorial will help you to install Python 3.11 on Amazon Linux 2 system.
Prerequisites
This tutorial provides instructions to compile Python from the source code. The compilation process required the development tools to be pre-installed, like the make command. So must have installed the required development libraries first.
Open a terminal on your system and install the required packages with the following command:
sudo yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel -y
Step 1 – Download Python 3.11
Next, download the Python 3.11 source code from the Python official website. At the time of writing, Python 3.11 is the latest version available.
Use the wget command to download the source code:
wget https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tgz
Then, extract the tarball file:
This command will create a new directory called Python-3.11.4.
Step 2 – Install Python 3.11 on Amazon Linux
Now you’re ready to compile Python from the source code. Navigate to the Python-3.11.4 directory:
Configure the build process using the ./configure script script. This script checks your system for necessary features and libraries:
sudo ./configure --enable-optimizations
The —enable-optimizations flag optimizes the Python binary by running multiple tests and making some tweaks, resulting in a slight performance improvement.
After that, you can compile and install it with the make command. Use the follwoing command to build and install using the Python:
Using `altinstall` instead of `install` avoids replacing the existing Python binary in your system, which could potentially break certain functionalities that rely on the original Python version.
This will complete the Python installation on your system. You can remove the downloaded archive file to free some space.
sudo rm -f /opt/Python-3.11.4.tgz
Step 3 – Check Python Version
The Python binary will be available under the /usr/local/bin directory. That is already included in the PATH environment variable. As we have not overwritten the current Python version, you need to run the Python 3.11 command as follows:
Step 4 – Create Python Virtual Environment
Python virtual environment provides you with an isolated environment for the applications. This can be created with the “venv” module that is already installed with the above steps.
To create the virtual environment first switch to your application directory.
Use the following command to create an environment directory:
The above command will create a directory “env” in the current directory containing all the required files for the isolated environment.
Every time you need to make changes in the environment, Use the below command to activate it.
After activating the environment, you can work with your application.
Once your work is finished, use the following command to deactivate the Python environment.
Conclusion
Congratulations, you have successfully compiled Python 3.11 from the source code on Amazon Linux 2. With your custom-built Python, you can now enjoy enhanced customization and possible performance improvements.
Remember, however, that manually maintaining a Python installation can be challenging and time-consuming. Whenever possible, use a package manager or a precompiled binary to simplify the process. Also, be cautious while replacing the system’s default Python interpreter, as many system tools rely on it and replacing it might break those tools.
How do I install and use Python 3.8 on an Amazon EC2 Linux Instance?
So I followed a tutorial to install python 3.8 and I’m able to use it by doing the command python3.8 , but for some reason the code in my main.py file is still running on python 3.7. If I enter python3.8 main.py , I get errors saying that I don’t have modules installed. I don’t know how to install my modules on python 3.8 because when I do sudo python3.8 -m pip install pymongo it says sudo: python3.8: command not found . Any help would be appreciated.
5 Answers 5
Amazon has it’s own Linux with extras, the command is:
sudo amazon-linux-extras install python3.8
taken from https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install-linux.html but after installing 3.7 from there, there is the above update command appearing during the install telling how to upgrade. Ahh the convolution of hosted virtual machines, still easier than trying to get a console command line login on any other Linux on AWS.
It does rebuild the python to 3.8, cleaning up files from 3.7, so boom a clean build of 3.8 thanks to Amazon.
All that being said, the default «python» will still be 2.7 as there is no backward compatibility, and many think getting rid of 2.7 will cause problems.
So what NOT TO DO NEXT as we are done is:
sudo rm /usr/bin/python (which is only a link to /usr/bin/python2.7) sudo ln -s /usr/bin/python3.8 /usr/bin/python
but usually peeps simply type python3.8 to specifically run the new version.
Thanks!, the sudo amazon-linux-extras install python3.8 helped me installing it in my cloud9 instance 🙂
Now you can use a package available via yum instead of getting from Amazon Extras. This way works for Amazon Linux 2:
yum install -y python38 python38-devel
@encryptedname are you sure you’re running Amazon Linux 2? JFYI, a command to check: awk -F ‘[=»]*’ ‘/^PRETTY_NAME/ < print $2 >‘ < /etc/os-release
@encryptedname strange, I installed python this way two times on amazon linux 2. Next time I’ll try to walk through it again and check what might be missing
- sudo yum -y groupinstall development sudo yum -y install zlib-devel
- sudo yum -y install openssl-devel bzip2-devel sudo yum remove python
- wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz
- tar xzvf Python-3.10.4.tgz
- cd Python-3.10.4/
- sudo ./configure —enable-optimizations
- sudo make install
The tutorial link you have posted makes you install python interpreter in a /opt folder not the /usr/bin or anywhere standard binaries are installed.So a workaround would this be :-
Just add your python interpreter Path to your Environment PATH variable , so that your shell knows where to find python interpreter.
But before that go to /opt folder nd determine your interpreter’s full path using pwd command and then execute the following.
type export PATH=»$PATH:/opt/python3.8/python and press enter ( for example)
or you can also do this specify the python interpreter path on top of your script
#!/opt/python3.8/bin/python import sys print(sys.executable)
These answers are now out of date as of Amazon Linux 2023. Amazon Linux 2023 FAQ
Q: Does AL2023 have Amazon-Linux-Extras like AL2?
A: No, AL2023 does not have extras. For higher-level software packages like language runtimes, we will use the quarterly release where we will add major/minor updates to packages as separate namespaced packages in addition to the default package provided in the repository. For example, default Python version in Amazon Linux 2023 may be 3.8, but we will add Python 3.9 (python39) as a separate namespaced package whenever it is made available. These additional packages will closely follow their upstream release cadence and support model and their support policies can be accessed by the package manager for compliance and security use cases. Default packages will continue to be supported throughout the life of AL2023.
Python is installed by default as python3 exact version is managed by Amazon. It is possible to pick a different version, but I have not found the instructions, since the currently install 3.9 works for my needs.
python3 —version
Python 3.9.16
python3x —version
always generated
-bash: python3x: command not found
regardless of choice of x
How to Install Python 3.8 on Amazon Linux
Python is a powerful programming language. It is very friendly and easy to learn. During the latest update of this article Python 3.8.12 (of Python 3.8 series) latest stable version is available to download and install. This tutorial will help you to install Python 3.8 on Amazon Linux systems.
Step 1 – Prerequaities
This Python installation required the GCC compiler on your system. Login to your server using ssh or shell access. Now, use the following command to install prerequisites for Python before installing it.
sudo yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel
Step 2 – Download Python 3.8
Download the Python from the Python official website. You can also download the latest version in place of the specified below.
cd /opt sudo wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tgz
Now extract the downloaded package.
sudo tar xzf Python-3.8.12.tgz
Step 3 – Install Python 3.8
Use the below set of commands to compile Python 3.8 from the source code and install using the altinstall command.
cd Python-3.8.12 sudo ./configure --enable-optimizations sudo make altinstall
make altinstall is used to prevent replacing the default python binary file /usr/bin/python.
Now remove the downloaded source archive file from your system
sudo rm -f /opt/Python-3.8.12.tgz
Step 4 – Check Python Version
The Python 3.8 binary is installed under /usr/local/bin directory. As we have not overwritten the current Python version, you need to run Python 3.8 command as following: