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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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 make sudo make install cd ~ echo ''' alias python='/opt/python2.7/bin/python' PATH=$PATH:/opt/python2.7/bin ''' >> .bash_profile source .bash_profile echo ''' /opt/python2.7/lib ''' >> opt-python2.7.conf sudo mv opt-python2.7.conf /etc/ld.so.conf.d/ sudo ldconfig python -V Python 2.7 |
Notes
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):
|
1 2 3 |
sudo ln -s /opt/python2.7/bin/python /usr/bin/python2.7 sudo python2.7 -V Python 2.7 |
Very nice! It works perfectly.
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).
Following the directions here you get a nice installable .rpm http://blog.milford.io/2012/01/building-and-installing-python-2-7-rpms-on-centos-5-7/
this worked great, but i’m struggling with updating mod_wsgi. any pointers?
As of March 28, you can “yum install python27″. https://forums.aws.amazon.com/ann.jspa?annID=1419
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
#!/usr/bin/python2.6
instead of
#!/usr/bin/python
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.
Thanks for this.
If you want to change your default python version:
first, remove the existing symbolic link to 2.6.7
rm /usr/bin/python
add new link to python 2.7:
ln -s /usr/bin/python2.7 /usr/bin/python
More information here: http://www.linuxquestions.org/questions/debian-26/change-default-python-version-605397/
Thanks a lot!! Saved my day!
Thanks a ton!! Worked like a charm. I get impressed when instructions lead to expected results in first attempt.
Thanks for the help.