CentOS 6.2 and 6.3 ships with Python 2.6.6. You can manually install Python 2.7 and Python 3.3 but you must be careful to leave the system version alone. Several critical utilities, for example yum, depend on Python 2.6.6 and if you replace it bad things will happen.
Below are the steps necessary to install Python 2.7.3 and Python 3.3.0 without touching the system version of Python. The procedure is exactly the same for both versions except for the filenames. People have reported that this also works for CentOS 5.8 but I haven’t tested that. Execute all the commands below as root either by logging in as root or by using sudo.
Install development tools
In order to compile Python you must first install the development tools and a few extra libs. The extra libs are not strictly needed to compile Python but without them your new Python interpreter will be quite useless.
# yum groupinstall "Development tools" # yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
Download and install Python
It is critical that you use make altinstall below. If you use make install you will end up with two different versions of Python in the filesystem both named python. This can lead to problems that are very hard to diagnose.
Download and install Python 2.7.3
# wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
# tar xf Python-2.7.3.tar.bz2
# cd Python-2.7.3
# ./configure --prefix=/usr/local
# make && make altinstall
Download and install Python 3.3.0
# wget http://python.org/ftp/python/3.3.0/Python-3.3.0.tar.bz2
# tar xf Python-3.3.0.tar.bz2
# cd Python-3.3.0
# ./configure --prefix=/usr/local
# make && make altinstall
After running the commands above your newly installed Python interpreter will be available as /usr/local/bin/python2.7 or /usr/local/bin/python3.3. The system version of Python 2.6.6 will continue to be available as /usr/bin/python and /usr/bin/python2.6.
Download and install Distribute
Distribute provides a framework for installing packages from the Python Package Index. Each Python interpreter on your system needs its own install of Distribute.
You can find out what the latest version of Distribute is here. At the time of this edit the current version is 0.6.35. Replace the version number below if there is a newer version available.
Download and install Distribute for Python 2.7
# wget http://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz # tar xf distribute-0.6.35.tar.gz # cd distribute-0.6.35 # python2.7 setup.py install
This generates the script /usr/local/bin/easy_install-2.7 that you use to install packages for Python 2.7. It puts your packages in /usr/local/lib/python2.7/site-packages/.
Download and install Distribute for Python 3.3
# wget http://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz # tar xf distribute-0.6.35.tar.gz # cd distribute-0.6.35 # python3.3 setup.py install
This generates the script /usr/local/bin/easy_install-3.3 that you use to install packages for Python 3.3. It puts your packages in /usr/local/lib/python3.3/site-packages/.
What’s next?
Working with multiple versions of Python is difficult and error-prone. I strongly recommend that you install virtualenv and learn how to use it. Virtualenv is a Virtual Python Environment builder that makes it possible to run Python in a sandbox-like environment. Each sandbox can have its own Python version and packages. This is very useful when you work on multiple projects, each with its own dependencies.
Install and use virtualenv for Python 2.7
# easy_install-2.7 virtualenv # virtualenv-2.7 --distribute someproject New python executable in someproject/bin/python2.7 Also creating executable in someproject/bin/python Installing distribute...................done. Installing pip................done. # source someproject/bin/activate (someproject)# python --version Python 2.7.3 (someproject)#
Install and use virtualenv for Python 3.3
# easy_install-3.3 virtualenv # virtualenv-3.3 --distribute otherproject New python executable in otherproject/bin/python3.3 Also creating executable in otherproject/bin/python Installing distribute...................done. Installing pip................done. # source otherproject/bin/activate (otherproject)# python --version Python 3.3.0 (otherproject)#
Thanks a lot, it worked like a charm!
Glad I could help.
Thanks Daniel. I second Onur.
I have two serpents now
Excellent post Daniel, thanks so much.
Excellent Post! Solved my big headache of vim compilation error..
hello,
after all this steps
i got this error when i use yum
No module named yum
how to fix it
thank you
Are you sure you installed your new python version using the command “make altinstall”. If you just used “make install” then you have replaced the system version (/usr/bin/python).
I think you can fix your problem simply by copying or linking /usr/bin/python2.6 to /usr/bin/python
how do add something to the pythonpath of python2.7
beautiful. worked perfect. Thanks for publishing. got me out of zlib error hell for a parallel python 2.7 install.
Pingback: Backing up to Amazon Glacier | Jacob Allred
Very nice work! thanks
How to fix no module name _sqlite3 ?
You need to have the sqlite-devel package installed before you configure and make python. Install it with “yum install sqlite-devel”.
Very nice, but how about throwing in a # yum install readline-devel? Makes the python interpreter easier to work with later (not to mention ipython).
Good suggestion! I’ll add it to the updated guide that I will post shortly.
Thanks Erik, after installing readline-devel, config detects the readline libs automatically, and compile in the support by default… the command line is way better!
I see failure when installing Tkinter using “easy_install-2.7 Tkinter”. How can I make it work?
error: Could not find suitable distribution for Requirement.parse(‘Tkinter’)
I needed another module ‘pexpect’ and “easy_install-2.7 pexpect” worked fine.
Issue fixed: After ‘yum install tk-devel’ and then reinstall python “make && make altinstall” I was able to import Tkinter properly. Didnt have to install Tkinter explicitly using “easy_install-2.7 Tkinter”
Thank you for the information! I will include this in the updated guide that I will post shortly.
after following all above steps, it istalled python 2.7(i checked directory where it install, its ok) but when i check python -V. it show python 2.6.6 instead of python 2.7. please help me how to change default python 2.6 to 2.7.
You do NOT want to change default python version because that breaks some system services (like yum for example). You have two options:
1. Always use “python2.7″ instead of “python” when you start python scripts.
2. Set up a virtualenv with python 2.7 and run everything from inside that virtualenv.
This is fantastic. Thank you!
Thank you for doing this! I am having one problem…When I run:
python2.7 setup.py install
I get:
bash: python2.7: command not found
Do you have an executable named /usr/local/bin/python2.7? Is /usr/local/bin on your PATH?
Thanks a lot it worked ,fine !!!!
Works great! Thanks a lot
Many thanks! Work great and fine.
Very great! Works.
Thank you.
Pingback: Broken python installation on CentOS 5.8 | UmeedainTimes.com
I get this error after I attempt to run Python for the first time:
/usr/local/python2.7/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
What options did you pass to configure? It doesn’t look like you used my suggestions.
Your suggestions didnt include –enable-shared … that’s why he got the error he did.
My update of the instructions with LD_LIBRARY_PATH and –enable-shared is here – http://www.livingcosmos.org/2013/04/how-to-install-python-2-7-and-3-3-on-centos-6-too-much-data/
admittedly a very garbage looking post, but it gets the job done
It works like a charm on my VPS
Thank you very much
Pingback: Хостинг WSGI з nic.ua | Roman Yepishev
You made this process so easy, thanks for the detailed steps! CentOS is too “stable” to be upgraded…. Only got python24 on my server.
Works on Centos 5.8 as well.
Thanks for the tutorial. How come there aren’t any easily obtainable RPMs for this kind of thing? It’s baffling to me that EPEL has PyPy 1.9 but no CPython 2.7.
hey thanks for the tutorial. How do we point django’s default python to this python ?
The best way is to run your Django app from inside a virtualenv. Look at the last steps in the article above for info on how to properly install virtualenv.
Thanks very much. It all worked until I saw this:
————————————————————————————–
Python build finished, but the necessary bits to build these modules were not found:
bsddb185 dl gdbm
imageop sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module’s name.
running build_scripts
creating build/scripts-2.7
copying and adjusting /disk2/software/Python-2.7.3/Tools/scripts/pydoc -> build/scripts-2.7
copying and adjusting /disk2/software/Python-2.7.3/Tools/scripts/idle -> build/scripts-2.7
copying and adjusting /disk2/software/Python-2.7.3/Tools/scripts/2to3 -> build/scripts-2.7
copying and adjusting /disk2/software/Python-2.7.3/Lib/smtpd.py -> build/scripts-2.7
changing mode of build/scripts-2.7/pydoc from 644 to 755
changing mode of build/scripts-2.7/idle from 644 to 755
changing mode of build/scripts-2.7/2to3 from 644 to 755
changing mode of build/scripts-2.7/smtpd.py from 644 to 755
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
make: stat: Modules/config.c.in: Permission denied
make: *** No rule to make target `Modules/config.c.in’, needed by `Makefile’. Stop.
—————————————————————————————
There is nothing at all in my /usr/local/bin.
Any suggestions?
I’m far from an expert, but the Python readme contains this:
Unix platforms: If your vendor still ships (and you still use) Berkeley DB
1.85 you will need to edit Modules/Setup to build the bsddb185
module and add a line to sitecustomize.py which makes it the
default. In Modules/Setup a line like
bsddb185 bsddbmodule.c
should work. (You may need to add -I, -L or -l flags to direct the
compiler and linker to your include files and libraries.)
XXX I think this next bit is out of date:
64-bit platforms: The modules audioop, and imageop don’t work.
The setup.py script disables them on 64-bit installations.
Don’t try to enable them in the Modules/Setup file. They
contain code that is quite wordsize sensitive. (If you have a
fix, let us know!)
That all suggests to me that you can edit the configuration, but there may be more information lower down in the readme. For example, I had to dig to find –enable-shared, which I needed.
Thanks for replying. I think I’ve got it now. It turned out to be a file protection problem. I’ve been trying to install using “sudo make && make altinstall” but apparently sudo doesn’t let me cd to directories that I don’t have rights too. So at first I started changing all the directories I needed in the source tree to 755. I still ran into a problem installing files into /usr/local/bin – I didn’t want to change the protection there. Fortunately, I could just do “sudo su” and do the install as root.
Good to hear you solved it!
Thanks, Dave. That did the trick for me. And thank you, Daniel, for such a useful guide.
Thank you! Most helpful.
Pingback: Run in a CentOS server
Thanks a lot Daniel.
The build was very clean and works like a charm.
I managed to install python 2.7.3 without breaking yum.
How to make a specific user to use python 2.7.3 as virtual env ?
Each virtualenv has its own Python version that is set when the virtualenv is first created. It is up to the user to decide what Python version he/she wants in each virtualenv.
Smooth as silk, thanks very much for taking the time to put this together
Pingback: Installing Python 2.7 on CentOS 5 | My Blog
Thanks a bunch. Was having a hard time getting python to install with many other resource I found on google. 10*
Thanks for the tutorial. I did everything you said, so if I understand correctly, now I have to install
1) python 2.7 once again
2) Will this virtualenv become default or how do I control it ?
I dont know much about python and vertualenv, hence the silly questions.
If you followed the tutorial you already have everything you need. When you create a new virtualenv you will automatically get a copy of the proper version of python inside that folder structure.
A virtualenv is never “default”, you have to manually activate it. Once activated your default python interpreter will change from the system default to the one in that particular virtualenv.
You can have as many virtualenv as you like on a machine, and each virtualenv can have a different default python interpreter, and different installed packages.
This is really useful when you develop multiple applications, each with different requirements. I do Django development myself and it is really useful to have one virtualenv for each project because one website might run Django 1.4, another might run 1.2 and yet another one might run 1.5 beta. I do most of my development in Eclipse with the pydev plugin, and that plugin knows about virtualenv so I just configure one interpreter for each project and point it at the python executable inside that particular virtualenv.
Maybe I should clarify that activating a virtualenv only affects your current session/shell.
I Want to run twissted protocol inside python 2.7 as it is working with python 2.6.6 . Please suggest
thanks a lot
it works!
Thank you. Worked perfect.
Regards
Santosh
Thanks buddy , easiest but best routine. I’m going to check your other posts.
Very useful post, you can update some version numbers that also worked for me: Centos 6.3 and distribute 0.6.35
Thank you, update done!
I have now updated the tutorial with additional information provided here in the comments, plus updated versions, plus more information about virtualenv. Enjoy!
i have intsall python using altinstall command, but still it show version 2.4
how to resolve it
The system default Python interpreter does not change, that is the whole point of this tutorial. To start your new Python interpreter you must issue the command /usr/local/bin/python2.7 (or simply python2.7 if /usr/local/bin is in your PATH which it usually is).
Hi there!
I have problem while running “make && make altinstall”. It overlap and non-stop, i mean the installation never finished. It showing:
By default, distutils will build C++ extension modules with “g++”.
If this is not intended, then set CXX on the configure command line.
And then restart the installation process.
Unfortunately I don’t know what could be causing that problem. Maybe you have installed something that messes around with your C/C++ toolchain. My only advice is to google the error message and see how others have solved it.
Works! Thanks!
Dear Colleagues,
I tried to install Python 2.7.3 and Python 3.3.0 following the method published in this blog.
My yum is now broken and after I requested the centos forum, I was advised to uninstall these 2 versions of python. How do I uninstall them, is there a command to do this?
With best regards,
Philip
Are you sure you followed the instructions? Especially the part where you use ALTINSTALL instead of INSTALL? I doubt you did, because if you used ALTINSTALL then the system version of Python would still be 2.6.6.
What is the output of the following commands:
# python –version
# /usr/bin/python –version
The Python makefile does not provide an uninstall target so you will have to remove the files manually if you want that.
Dear Daniel,
Thank you for replying back. Please find the required information below.
$ python -V
Python 2.7
$ yum
-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory
Can we change the defauly version of python to the original 2.6.6?
With best regards,
Philip
Please check the version of /usr/bin/python.
If it is 2.6.6 then your PATH is most likely in the wrong order (/usr/bin should be before /usr/local/bin).
If it is 2.7.3 then you didn’t follow the steps in this guide. You can still salvage the situation by doing this:
rm /usr/bin/python
ln -s /usr/bin/python2.6 /usr/bin/python
Thanks a lot! This article really helps me.
Dear Daniel,
You are right, I may have done a lot of more than what the guide says (I have a 64 bit system). Please kindly help me resolve this error, as most of my work is pending because I am not able to use yum. We could connect via webex/ teamviewer, please let me know when you are free and I will send you my teamviewer login details.
I have tried out your suggestions, but the error with yum remains. Please find the below log-
==============================
$rm /usr/bin/python
rm: remove symbolic link `/usr/bin/python’? y
$ln -s /usr/bin/python2.6 /usr/bin/python
$yum
-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory
$python -V
Python 2.7
===============================
Thanking you and with best regards,
Philip
Verify that /usr/bin/python2.6 really is version 2.6. If it is then you have another binary called python somewhere else in your filesystem, in a location that is searched before /usr/bin. Check the order of your PATH variable.
Thanks Daniel for the great tutorial. I’ve installed PyCrypto, and it fails with the following:
Running pycrypto-2.6/setup.py -q bdist_egg –dist-dir /tmp/easy_install-wQetbc/pycrypto-2.6/egg-dist-tmp-FPHtu0
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
zip_safe flag not set; analyzing archive contents…
Then, I ran yum install mpir, and I got:
Package mpir-1.3.1-4.el6.x86_64 already installed and latest version .
Similarly, it said gmp was installed and latest.
I’ve been told I need python2.7.3-devel to get PyCrypto to build. I can get it from an RPM I found but I don’t think it’s a great idea (I have to symlink all kinds of things everywhere and then install the RPM — maybe not great.)
Any idea how I can solve this? Also, if I want to remove Python 2.7.3: Any specific instructions?
Thanks again!
Not sure I can help you with PyCrypto, at least not right now, but I can tell you that you already have what is included in the devel package (libs and headers).
Removing Python is pretty easy, all the files you need to delete are located under /usr/local and fairly easy to spot. This should get you going:
/usr/local/bin/python2.7
/usr/local/include/python2.7 (dir)
/usr/local/lib/libpython2.7.a
/usr/local/lib/python2.7 (dir)
There are a few more executables under /usr/local/bin that you can remove also (pydoc, idle, 2to3, …). List the dir in chronological order to see what other (if any) files were installed there at the same time as python2.7.
Worked on centos 5.8; I was upgrading python to 2.7.3…thanks!!
very useful article, great work, thanks!
Thanks! Worked perfectly on CentOS 6.4. I have 2.6.6, 2.7.3 and 3.3 all living in peaceful harmony.
Pingback: Living Cosmos
Hi Daniel,
This is by far the best tutorial I have found for installing python 3.3 on Centos. After running into some permission errors, I successfully ran the make && make altinstall. The folder created is /usr/local/bin/python3.3m as opposed to /usr/local/bin/python3.3.
When I try to run the command python3.3 setup.py install, I receive the error: “-bash: python3.3: command not found”. Any ideas on how to fix this?
Pingback: CentOS6にpython2.7を入れる | ちいさな創々
Daniel, thanks for the tutorial. This worked like a charm on Cent OS 5.7, which ships with Python 2.4.
FYI, the newest versions are 2.7.4 and 3.3.1, released yesterday and today, respectively. I installed 2.7.4 no problem by just replacing the release version number in your instructions. (Didn’t try 3.3.1.)
It’s great to have these straight-forward instructions for those of us who know enough about linux to easily understand & follow your tutorial, but don’t perform these actions regularly enough for them to be intuitive. (I would never have thought to install the development tools first, for example.) Thank you very much.
Thanks a lot man, It worked find!
Worked like a charm .. Thanx Daniel
I don’t even know how I ended up right here, but I believed this put up was great. I don’t recognise who you are but certainly you are going to a well-known blogger should you are not already. Cheers!
Pingback: linux tips linux命令锦集
Echoing everyone else – thanks for the quickie.
Sweet !!!…This worked like a charm, not even a single hiccup.
Hi Daniel,
I am new to Linux, I am following your steps to install python3.3 on cent0s 6, all the steps works until when I get to “make && make altinstall”.
Ithrows the following error: make: *** No targets specified and no makefile found. Stop.
it seems that, I am not getting something right. Could you please help. Many thanks
no answer yet
I haven’t done 3.3, but this sound like you’re running the command in the wrong directory. Are you in the directory containing the makefile (probably actually named Makefile) ?
Thanks for the reply Dave, yes I was in the Makefile directory. All the steps from Daniel worked until when I go to “make && make altinstall” Cheers.
Most strange. Does “make –file=Makefile” behave any better ? Or “make –file=./Makefile” ? If not, I’d suggest showing us the output of “pwd; ls -l; alias make ; env |grep -i make” (all just grasping at straws.)
Did anyone answer the question about setting the PYTHONPATH in the virtual environment or is that not necessary?
Just a philosophical question: what’s the point of the centos package management, if you have to install packages manually?
In this case, it’s because the CentOS repositories do not have Python 2.7 and 3 in them and the versions that they do contain are rather obsolete.
Hello,
I’ve just updated to python3-3.3.1 as rpm packages on:
http://jur-linux.org/download/el-updates/6/
This is based on the newest Fedora rpm to compile on RHEL6, any additional
testing more than welcome for people who don’t want to recompile themselves.
best regards,
Florian La Roche
hello, i’m trying to install anki 2.0.3 on my cent 6.x box, followed instructions, but anki rpm still says that it needs 2.7 to install? halp?
Following the instructions to build 2.7.4 on CentOS 6.4 I get
‘WARNING: old charecter set or encoding…’ message and a garbaged screen only starting /usr/local/bin/python2.7 but not Python
Pingback: How to install Python 2.7 – On Windows, Ubuntu and CentOS | Buzz Atom - Your Way Into The Core of Nimbuzz
Thanks a lott, really helpful
Fix: I think you’re missing a j in
tar xf Python-2.7.3.tar.bz2
should be
tar jxf Python-2.7.3.tar.bz2
You are so awesome. THANKS!!!!!!!!!!!!!!!
Worked perfectly with 2.7.4 and 3.3.1 on Fedora 18. I prefer to have my installations more self-contained, so I added the –prefix and –exec-prefix options to the configure command line.
thanks, worked really well
Thank you so much. This is very helpful!
You actually help it become seem to be not that hard along with your powerpoint presentation on the other hand locating this disorder being really an element that I’m I would personally in no way fully grasp. It seems very sophisticated and extremely vast to me. I am looking ahead to the following distribute, I am going to aim to find the their hands on the item!