
I recently worked on a Python project that used Python 3.11 and Poetry. When I went to deploy it onAmazon's Elastic Compute Cloud, better known by its moniker EC2, I ran into a major hurdle. EC2, running Amazon Linux 2, had Python 3.7.11. The project required 3.11, so I needed to compile Python from source.
Dependencies
I started by updating packages inYUM
, the package manager for Amazon Linux 2, withsudo yum update
. Then I installed some development dependencies, namely the aptly-named "Development Tools" group and a few others with the following command.
sudoyum groupinstall"Development Tools"sudoyuminstalllibffi-devel bzip2-devel
One more dependency is needed:OpenSSL. Theopenssl-devel
package included in the repositories with Amazon Linux 2 is 1.0.7, but Python 3 currently requires 1.1.1 or newer. This version is available through theopenssl11-devel
package. To install it, uninstallopenssl-devel
, then installopenssl11-devel
.
sudoyum uninstall openssl-develsudoyuminstallopenssl11-devel
Compilation
With the development dependencies installed, the next step was to download the Python source code. ThePython Software Foundation maintains tarballs of every Python version released, and they're available throughthis user-friendly page ortheir plain directory listing. Either way, find the Python version your project needs, then copy the link to the file that ends in.tgz
, which is the gzipped tarball. With the link to the Python source in hand, download it withwget
, then extract the archive.
sudowget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tgzsudo tarxzf Python-3.11.1.tgzcdPython-3.11.1
Next, run theconfigure
script, which will check to ensure that the necessary dependencies are installed:sudo ./configure --enable-optimizations
. The--enable-optimizations
flag optimizes the binary. After that finishes, there will be aMakefile
. Before proceeding, decide whether this new version should replace the system version, or if it should be installed alongside. I recommend the latter, and that's what I used. If you decided to replace the system version, usesudo make install
. If you decided to install it alongside, usesudo make altinstall
. On myt2.micro instance, it took about 35 minutes to build.
Shell Configuration
Python installs to/usr/local/bin
, but thePATH
doesn't contain it. To add this folder to thePATH
, open the~/.bashrc
file in your favourite text editor and add the following to the end.
exportPATH=/usr/local/bin:$PATH
Reload the.bashrc
file by runningsource ~/.bashrc
, then try opening a Python shell withpython3.11
. Replace3.11
with whatever Python version was installed.
Conclusion
If you have any questions or need any help, feel free to contact me onTwitter orMastodon.
If you have any improvements to any of my articles or notes, pleasesubmit a pull request.
Thank you for reading!
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse