Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Installing a Newer Version of Python on Amazon EC2
H. Kamran
H. Kamran

Posted on • Originally published athkamran.com

     

Installing a Newer Version of Python on Amazon EC2

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

My name is H. Kamran and I'm a developer. I program mainly in TypeScript and Python. I've been using React, Vue.js, and Flutter a lot more these days, and dabble in Swift and SwiftUI.
  • Location
    Cyberspace
  • Pronouns
    he/him
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp