How to install Python 3.x on Amazon EC2 instance


Discover which python3 packages are available for yum install:

sudo yum list | grep python3

Install the package you’ve decided on:

sudo yum install python36 -y

Longer explanation:

So you’ve probably just fired up a fresh t2.micro instance using the latest Amazon Linux AMI. Perhaps you’ve even already yum updated.

You take a look at the python version and:

[ec2-user@ip-172-16-0-0 ~]$ python --version
Python 2.7.13
[ec2-user@ip-172-16-0-0 ~]$

Arggggh! Why no Python 3?

This is easily remedied. First take a look at which python3 packages are installed:

[ec2-user@ip-172-16-0-0 ~]$ sudo yum list | grep python3
mod24_wsgi-python34.x86_64            3.5-1.24.amzn1                amzn-main   
mod24_wsgi-python35.x86_64            3.5-1.24.amzn1                amzn-main   
mod24_wsgi-python36.x86_64            3.5-1.24.amzn1                amzn-main   
python34.x86_64                       3.4.7-1.38.amzn1              amzn-updates
python34-devel.x86_64                 3.4.7-1.38.amzn1              amzn-updates
python34-docs.noarch                  3.4.3-1.23.amzn1              amzn-main   
python34-libs.i686                    3.4.7-1.38.amzn1              amzn-updates
python34-libs.x86_64                  3.4.7-1.38.amzn1              amzn-updates
python34-pip.noarch                   9.0.1-1.24.amzn1              amzn-main   
python34-setuptools.noarch            36.2.7-1.33.amzn1             amzn-main   
python34-test.x86_64                  3.4.7-1.38.amzn1              amzn-updates
python34-tools.x86_64                 3.4.7-1.38.amzn1              amzn-updates
python34-virtualenv.noarch            15.1.0-1.14.amzn1             amzn-main   
python35.x86_64                       3.5.4-13.11.amzn1             amzn-updates
python35-devel.x86_64                 3.5.4-13.11.amzn1             amzn-updates
python35-libs.i686                    3.5.4-13.11.amzn1             amzn-updates
python35-libs.x86_64                  3.5.4-13.11.amzn1             amzn-updates
python35-pip.noarch                   9.0.1-1.24.amzn1              amzn-main   
python35-setuptools.noarch            36.2.7-1.33.amzn1             amzn-main   
python35-test.x86_64                  3.5.4-13.11.amzn1             amzn-updates
python35-tools.x86_64                 3.5.4-13.11.amzn1             amzn-updates
python35-virtualenv.noarch            15.1.0-1.14.amzn1             amzn-main   
python36.i686                         3.6.2-5.8.amzn1               amzn-updates
python36.x86_64                       3.6.2-5.8.amzn1               amzn-updates
python36-debug.i686                   3.6.2-5.8.amzn1               amzn-updates
python36-debug.x86_64                 3.6.2-5.8.amzn1               amzn-updates
python36-devel.i686                   3.6.2-5.8.amzn1               amzn-updates
python36-devel.x86_64                 3.6.2-5.8.amzn1               amzn-updates
python36-libs.i686                    3.6.2-5.8.amzn1               amzn-updates
python36-libs.x86_64                  3.6.2-5.8.amzn1               amzn-updates
python36-pip.noarch                   9.0.1-1.24.amzn1              amzn-main   
python36-setuptools.noarch            36.2.7-1.33.amzn1             amzn-main   
python36-test.i686                    3.6.2-5.8.amzn1               amzn-updates
python36-test.x86_64                  3.6.2-5.8.amzn1               amzn-updates
python36-tools.i686                   3.6.2-5.8.amzn1               amzn-updates
python36-tools.x86_64                 3.6.2-5.8.amzn1               amzn-updates
python36-virtualenv.noarch            15.1.0-1.14.amzn1             amzn-main   
[ec2-user@ip-172-16-0-0 ~]$

Looks like our package options are python34, python35, and python36 as of the time of this tutorial.

So let’s go ahead and install python36:

[ec2-user@ip-172-16-0-0 ~]$ sudo yum install python36 -y
Loaded plugins: priorities, update-motd, upgrade-helper
Resolving Dependencies
--> Running transaction check
---> Package python36.x86_64 0:3.6.2-5.8.amzn1 will be installed
--> Processing Dependency: python36-libs(x86-64) = 3.6.2-5.8.amzn1 for package: python36-3.6.2-5.8.amzn1.x86_64
--> Processing Dependency: python36-setuptools for package: python36-3.6.2-5.8.amzn1.x86_64
--> Processing Dependency: python36-pip for package: python36-3.6.2-5.8.amzn1.x86_64
--> Processing Dependency: for package: python36-3.6.2-5.8.amzn1.x86_64
--> Running transaction check
---> Package python36-libs.x86_64 0:3.6.2-5.8.amzn1 will be installed
---> Package python36-pip.noarch 0:9.0.1-1.24.amzn1 will be installed
---> Package python36-setuptools.noarch 0:36.2.7-1.33.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

 Package                                Arch                      Version                               Repository                       Size
 python36                               x86_64                    3.6.2-5.8.amzn1                       amzn-updates                     63 k
Installing for dependencies:
 python36-libs                          x86_64                    3.6.2-5.8.amzn1                       amzn-updates                     11 M
 python36-pip                           noarch                    9.0.1-1.24.amzn1                      amzn-main                       2.5 M
 python36-setuptools                    noarch                    36.2.7-1.33.amzn1                     amzn-main                       831 k

Transaction Summary
Install  1 Package (+3 Dependent packages)

Total download size: 15 M
Installed size: 47 M
Downloading packages:
(1/4): python36-3.6.2-5.8.amzn1.x86_64.rpm                                                                             |  63 kB  00:00:00     
(2/4): python36-setuptools-36.2.7-1.33.amzn1.noarch.rpm                                                                | 831 kB  00:00:00     
(3/4): python36-pip-9.0.1-1.24.amzn1.noarch.rpm                                                                        | 2.5 MB  00:00:00     
(4/4): python36-libs-3.6.2-5.8.amzn1.x86_64.rpm                                                                        |  11 MB  00:00:00     
Total                                                                                                          19 MB/s |  15 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python36-setuptools-36.2.7-1.33.amzn1.noarch                                                                               1/4 
  Installing : python36-libs-3.6.2-5.8.amzn1.x86_64                                                                                       2/4 
  Installing : python36-3.6.2-5.8.amzn1.x86_64                                                                                            3/4 
  Installing : python36-pip-9.0.1-1.24.amzn1.noarch                                                                                       4/4 
  Verifying  : python36-pip-9.0.1-1.24.amzn1.noarch                                                                                       1/4 
  Verifying  : python36-3.6.2-5.8.amzn1.x86_64                                                                                            2/4 
  Verifying  : python36-setuptools-36.2.7-1.33.amzn1.noarch                                                                               3/4 
  Verifying  : python36-libs-3.6.2-5.8.amzn1.x86_64                                                                                       4/4 

  python36.x86_64 0:3.6.2-5.8.amzn1                                                                                                           

Dependency Installed:
  python36-libs.x86_64 0:3.6.2-5.8.amzn1      python36-pip.noarch 0:9.0.1-1.24.amzn1      python36-setuptools.noarch 0:36.2.7-1.33.amzn1     

[ec2-user@ip-172-16-0-0 ~]$

Excellent – now let’s reap the fruits of our labor:

[ec2-user@ip-172-16-0-0 ~]$ python --version
Python 2.7.13
[ec2-user@ip-172-16-0-0 ~]$

What!!!!? I thought we installed python3!
Never fear… python 3 is now installed as… wait for it… python3:

[ec2-user@ip-172-16-0-0 ~]$ python3 --version Python 3.6.2 
[ec2-user@ip-172-16-0-0 ~]$

Symlinking python to python3 (Optional)

Now if you want to make the python point to python3, thats a different exercise. Also realize that some functions of the Amazon Linux AMI might rely on python pointing to version 2.7.x, so you’d probably be better off exploring virtualenv instead, but I digress.
However, if you’ve decided the risk is worth it, the process is simple enough:

[ec2-user@ip-172-16-0-0 ~]$ which python python3
[ec2-user@ip-172-16-0-0 ~]$

Now let’s look at python files/symlinks in /usr/bin:

[ec2-user@ip-172-16-0-0 ~]$ ls -al /usr/bin/python* | grep '^[-l]'
lrwxrwxrwx 1 root root   24 Feb 24 09:08 /usr/bin/python -> /etc/alternatives/python
-rwxr-xr-x 1 root root 5120 Jan 31 00:18 /usr/bin/python27
-rwxr-xr-x 1 root root 5120 Jan 31 00:18 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1846 Jan 31 00:17 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root   25 Feb 24 09:08 /usr/bin/python3 -> /etc/alternatives/python3
-rwxr-xr-x 3 root root 6872 Feb 19 21:58 /usr/bin/python36
-rwxr-xr-x 3 root root 6872 Feb 19 21:58 /usr/bin/python3.6
-rwxr-xr-x 3 root root 6872 Feb 19 21:58 /usr/bin/python3.6m
lrwxrwxrwx 1 root root   31 Feb 24 09:08 /usr/bin/python-config -> /etc/alternatives/python-config
[ec2-user@ip-172-16-0-0 ~]$

Now it’s a simple matter of changing symbolic link for python to point toward /etc/alternatives/python3:

[ec2-user@ip-172-16-0-0 bin]$ sudo rm python
[ec2-user@ip-172-16-0-0 bin]$ sudo ln -s /etc/alternatives/python3 /usr/bin/python

Then let’s make a symbolic link back to python2 in case we ever need to use it or want hints on how to undo our python3 symlink:

[ec2-user@ip-172-16-0-0 bin]$ sudo ln -s /etc/alternatives/python /usr/bin/python2

Now we verify and we’re good to go:

[ec2-user@ip-172-16-0-0 bin]$ python --version
Python 3.6.2
[ec2-user@ip-172-16-0-0 bin]$
Share this:

Leave a Reply