Updating an Amazon Linux AMI to Python 2.7

I’ve been doing some work with Linux lately, a new thing for me, and feel a bit like I’ve been thrown into the deep end and told to swim. Today I updated Python to version 2.7 on an EC2 instance.

Amazon Linux (Basic 64-bit Amazon Linux AMI 2011.09 AMI Id: ami-1b814f72) currently comes with Python 2.6.7 on it. At first, I thought I’d just be able to update to 2.7 with yum, but as far as I can tell, there isn’t a package for that already.

Joshua Holmes wrote about updating Python, which was a great help. My experienced varied a little, so I thought I’d share in case it is useful to someone else. For example, I didn’t want sqllite, but I did want ssl support. Also, there were some cases where I needed to use sudo when logged in as ec2-user.

sudo yum install make gcc gcc-c++
sudo yum install openssl-devel.x86_64
cd ~
wget http://python.org/ftp/python/2.7/Python-2.7.tgz
tar xfz Python-2.7.tgz
cd Python-2.7
./configure --prefix=/opt/python2.7 --with-threads --with-ssl --enable-shared
sudo make install

cd ~ 
echo '''
 alias python='/opt/python2.7/bin/python'
 ''' >> .bash_profile
source .bash_profile
echo '''
 ''' >> opt-python2.7.conf
sudo mv opt-python2.7.conf  /etc/ld.so.conf.d/
sudo ldconfig
python -V
Python 2.7


Line 26 is output from Python — not something you input (if that wasn’t obvious).

The configure script (line 8 ) specifies which optional libraries will be included in Python. I’m not sure what the typical list includes (for example, which are included in the official Python install for Windows?), but when you run make (line 9), it will report all the missing libraries for Python modules (“Python build finished, but the necessary bits to build these modules were not found”). If you use a module with a missing library in a Python program, you’ll get a runtime error.

The echo lines (13-16) are editing your user profile file for bash so that when you type ‘python’ you’ll get the 2.7 version instead of the default 2.6.7 version. I had a situation where I needed to run python under sudo, but root doesn’t have the alias, so it kept running 2.6.7. After some unfruitful attempts to add the alias to /etc/profile (sudo doesn’t execute that), I just added a symlink and invoked ‘python2.7’ explicitly (thanks to Chris Brinker):

sudo ln -s /opt/python2.7/bin/python /usr/bin/python2.7
sudo python2.7 -V
Python 2.7

16 thoughts on “Updating an Amazon Linux AMI to Python 2.7

  1. The details were extemely helpful, I’ve bookmarked this page and I hope it never goes away :).

    The great thing is that the script is good not only for AMIs, but also for CentOS installations that have Python <2.7 (and, since CentOS is so slow in adopting new versions of Python due to yum source code, this script will always be helpful).

    • Installing the 2.7 package is not enough. You also need to point change the symbolic link to point to the new version:

      sudo yum install python27
      sudo rm /usr/bin/python
      sudo ln -s /usr/bin/python2.7 /usr/bin/python

      • Unfortunately, doing this breaks yum because the yum (and probably some other things with similar issues) because the yum python package is installed in /usr/lib/python2.6/site-packages/ .

        One way to get around this issue with yum after doing your update is to modify the /usr/bin/yum shell script to start with
        instead of

        • Also, looking at some of the other packages in the 2.6 site-packages directory like cloudinit and cfnbootstrap which are probably helpful for interaction with aws, it is less of a hassle to call 2.7 explicitly when needed and leave the default symlinks alone.

  2. I’ve tried this but with the Ubuntu EC2 instance, I tried this and changed used apt instead of yum, and it worked fine until I tried to install packages such as lxml which started complaining that they couldn’t find files such as pyconfig.h. Is there something I’m missing ?

Leave a Reply

Your email address will not be published. Required fields are marked *