Skip to content

Instantly share code, notes, and snippets.

@Sunlighter
Last active August 28, 2023 20:35
Show Gist options
  • Save Sunlighter/87bbd2cd80971c7c0d4763ec1b5ea548 to your computer and use it in GitHub Desktop.
Save Sunlighter/87bbd2cd80971c7c0d4763ec1b5ea548 to your computer and use it in GitHub Desktop.
Installing Python 3.7 in Amazon Linux 2

Installing Python 3.7 in Amazon Linux 2

This is a way to build Python 3.7 from source and temporarily install it in Amazon Linux 2 without overwriting the system Python and without interfering with the Python in amazon-linux-extras.

At the time of this writing, Amazon Linux 2 offers Python 2.7.14 and (through the extras) Python 3.6.2, but Python 3.7.0 was just released.

  1. Start Amazon Linux 2 and sign in. (I recommend a c5.large instance.)

  2. Get the source tarball.

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
  1. Uncompress it.
tar -xJf Python-3.7.0.tar.xz
  1. Get Prerequisites.
sudo yum install gcc bzip2-devel ncurses-devel gdbm-devel xz-devel sqlite-devel openssl-devel tk-devel uuid-devel readline-devel zlib-devel libffi-devel
  1. Create a target directory.
mkdir python37
  1. Configure and Build. (This takes a while with --enable-optimizations; omit it if you're in a hurry.)
cd Python-3.7.0/
./configure --prefix=/home/ec2-user/python37 --enable-shared --enable-optimizations
make
make install
  1. Alter your .bash_profile by adding $HOME/python37/bin to your PATH and setting LD_LIBRARY_PATH to $HOME/python37/lib. Log out and log in again.

  2. Use python3 to use your new Python interpreter. Or, optionally, create virtual environments.

  3. When you are done, you can undo your changes to .bash_profile and delete the python37 and Python3.7.0 directories.

@def6rick
Copy link

One possible alternative to step six which I got to work on my Amazon Linux 2 instance (a t2.small). Basically leverages altinstall.

$ ./configure --enable-optimizations
$ make altinstall

Afterward:

$ which python3.7
/usr/local/bin/python3.7
$ which python
/usr/bin/python
$ python --version
Python 2.7.14
$ python 3.7 --version
Python 3.7.1

Note I installed 3.7.1 as opposed to 3.7.0, the latest at the time of this writing (wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment