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.
-
Start Amazon Linux 2 and sign in. (I recommend a c5.large instance.)
-
Get the source tarball.
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
- Uncompress it.
tar -xJf Python-3.7.0.tar.xz
- 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
- Create a target directory.
mkdir python37
- 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
-
Alter your
.bash_profile
by adding$HOME/python37/bin
to yourPATH
and settingLD_LIBRARY_PATH
to$HOME/python37/lib
. Log out and log in again. -
Use
python3
to use your new Python interpreter. Or, optionally, create virtual environments. -
When you are done, you can undo your changes to
.bash_profile
and delete thepython37
andPython3.7.0
directories.
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)