88

I have a shell that runs CentOS.

For a project I'm doing, I need python 2.5+, but centOS is pretty dependent on 2.4.

From what I've read, a number of things will break if you upgrade to 2.5.

I want to install 2.5 separately from 2.4, but I'm not sure how to do it. So far I've downloaded the source tarball, untarred it, and did a ./configure --prefix=/opt which is where I want it to end up. Can I now just make, make install ? Or is there more?

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106
tehryan
  • 24,405
  • 11
  • 28
  • 17
  • 8
    I personally used to use CentOS alot and made the switch to Ubuntu because of how crufty the RedHat/CentOS distributions are (Python 2.4...). If you politically can make the jump, do it. You'll be much happier and won't have to worry about this type of thing anymore. – Adam Nelson Sep 25 '09 at 20:10
  • 1
    Compile from source and installing them in `/usr/local` (the default) shouldn't cause any problem. Or, even better, trash centos and install debian :P – o0'. May 06 '10 at 09:52
  • 19
    Huh? The point of CentOS/RHEL is long term support. If there is an option to switch to the latest distribution then switching to Fedora makes much more sense than switching to Ubuntu for somebody with a CentOS background. – Muhammad Alkarouri Sep 20 '10 at 18:30
  • 1
    Just to be clear, without any sarcastic sidebars: YES. You can install a newer version of Python, from source, into /opt just as you listed. Or by default it goes into /usr/local/{bin,lib,share,man} which also works great and will not interfere with python 2.4, which is STILL the version in CentOS 5. CentOS 6 comes with python 2.6.6. – IcarusNM May 20 '14 at 21:41
  • http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/ – Dominik Antal Oct 02 '14 at 15:18

15 Answers15

80

You could also use the EPEL-repository, and then do sudo yum install python26 to install python 2.6

Xavi
  • 20,111
  • 14
  • 72
  • 63
boerre
  • 902
  • 1
  • 7
  • 6
  • 8
    How to use EPEL: http://fedoraproject.org/wiki/EPEL/FAQ#howtouse -- But remember to replace the i386 with your arch if it's not 32-bit x86 – rakslice Dec 13 '10 at 21:31
  • Worked for me! I needed these for another project as well so I'm double happy! – rholmes Jan 19 '11 at 14:08
  • 2
    seems Python 2.6 is not longer in epel. – mujimu Jul 25 '14 at 15:27
  • 1
    @mujimu, I think they may of made 2.6 the default over 2.4. So commands like `yum install python26` may be just `yum install python` depending on the OS flavor/version. – James Oravec Aug 28 '14 at 17:07
32

Try epel

wget http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
sudo rpm -ivh epel-release-5-4.noarch.rpm
sudo yum install python26

The python executable will be available at /usr/bin/python26

mkdir -p ~/bin
ln -s /usr/bin/python26 ~/bin/python
export PATH=~/bin:$PATH # Append this to your ~/.bash_profile for persistence

Now, python command will execute python 2.6

Iker Jimenez
  • 7,105
  • 9
  • 49
  • 46
Joyce Babu
  • 19,602
  • 13
  • 62
  • 97
  • Thanks for the last bit - I just installed python26 and was wondering how to make the 'default' (2.4) refer to the 2.6. Creating a symlink and adjusting the path did it. – Jay Sidri Jan 15 '12 at 05:26
  • This will obviously work...for one user...until one replaces python2.6 with 3.0 or whatever...but it appears to be the recommended way to get this done. I am having trouble believing that CentOS does not come with a tool to select the version of various concurrently installed packages to use (I often encounter 2 versions of Java, PostgreSQL and now python), something like configure_python -s 2.6 and everything "magically" falls into place behind the curtains, without the user having to think about corner cases years in the future. – Tomislav Nakic-Alfirevic Dec 13 '12 at 15:53
  • I think the reason is that, being an enterprise OS mainly used for servers, people don't do much development on Cent OS. Once everything is configured correctly, they don't touch the server unless for occasional security updates. – Joyce Babu Dec 14 '12 at 05:56
  • 1
    @TomislavNakic-Alfirevic I have not tried it yet, but https://github.com/yyuu/pyenv looks promising. It builds everything from source and installs each version in it's own directory. It looks like it's targeted more for developers' installing in their own home directories, but the builds can be run as sudo and target a shared directory. – Mark Evans Aug 20 '13 at 23:06
  • @MarkEvans Python is really not my strong point, so I could easily be wrong, but pyenv looks like a solution for a different problem. If you replaced "python" with "JDK", I would definitely not be thrilled about compiling it just to be able to deploy a Java app: I want the JDK version that someone else compiled, tested, retested and tested again with which ever version of CentOS I'm using. I would tend to apply the same logic to Python. That said, I'm sure there are use cases where pyenv shines, such as dev. (as opposed to deployment) machines. – Tomislav Nakic-Alfirevic Aug 21 '13 at 10:57
29

When I've run into similar situations, I generally avoid the package manager, especially if it would be embarrassing to break something, i.e. a production server. Instead, I would go to Activestate and download their binary package:

https://www.activestate.com/activepython/downloads/

This is installed by running a script which places everything into a folder and does not touch any system files. In fact, you don't even need root permissions to set it up. Then I change the name of the binary to something like apy26, add that folder to the end of the PATH and start coding. If you install packages with apy26 setup.py installor if you use virtualenv and easyinstall, then you have just as flexible a python environment as you need without touching the system standard python.

Edits... Recently I've done some work to build a portable Python binary for Linux that should run on any distro with no external dependencies. This means that any binary shared libraries needed by the portable Python module are part of the build, included in the tarball and installed in Python's private directory structure. This way you can install Python for your application without interfering with the system installed Python.

My github site has a build script which has been thoroughly tested on Ubuntu Lucid 10.04 LTS both 32 and 64 bit installs. I've also built it on Debian Etch but that was a while ago and I can't guarantee that I haven't changed something. The easiest way to do this is you just put your choice of Ubuntu Lucid in a virtual machine, checkout the script with git clone git://github.com/wavetossed/pybuild.git and then run the script.

Once you have it built, use the tarball on any recent Linux distro. There is one little wrinkle with moving it to a directory other than /data1/packages/python272 which is that you have to run the included patchelf to set the interpreter path BEFORE you move the directory. This affects any binaries in /data1/packages/python272/bin

All of this is based on building with RUNPATH and copying the dependent shared libraries. Even though the script is in several files, it is effectively one long shell script arranged in the style of /etc/rc.d directories.

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106
  • 4
    I don’t know what the license was in the past, but unfortunately now it doesn’t allow you to use ActivePython on external-facing servers unless you buy the Business Edition ($$$). https://www.activestate.com/compare-editions – Nate Aug 30 '11 at 19:54
  • 2
    to avoid breaking a server with package manager you rely on third party provider's scripts? huh – akostadinov Oct 15 '13 at 13:50
29

No need to do yum or make your own RPM. Build python26 from source.

wget https://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz
tar -zxvf Python-2.6.6.tgz
cd Python-2.6.6
./configure && make && make install

There can be a dependency error use

yum install gcc cc

Add the install path (/usr/local/bin/python by default) to ~/.bash_profile.

It will not break yum or any other things which are dependent on python24.

the swine
  • 10,713
  • 7
  • 58
  • 100
Rahul Arora
  • 1,036
  • 9
  • 8
  • 1
    Note: A C compiler is required for this to work. It doesn't look like one comes stock with CentOS 5 (at least, mine doesn't have one on the PATH) – Cody S Jan 02 '13 at 21:16
  • ./configure && make && make alt install ( http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/ ) – luismartingil Nov 12 '14 at 17:01
25

No, that's it. You might want to make sure you have all optional library headers installed too so you don't have to recompile it later. They are listed in the documentation I think.

Also, you can install it even in the standard path if you do make altinstall. That way it won't override your current default "python".

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
12

Chris Lea provides a YUM repository for python26 RPMs that can co-exist with the 'native' 2.4 that is needed for quite a few admin tools on CentOS.

Quick instructions that worked at least for me:

$ sudo rpm -Uvh http://yum.chrislea.com/centos/5/i386/chl-release-5-3.noarch.rpm
$ sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CHL
$ sudo yum install python26
$ python26
Kimvais
  • 38,306
  • 16
  • 108
  • 142
3

If you want to make it easier on yourself, there are CentOS RPMs for new Python versions floating around the net. E.g. see:

http://www.geekymedia.com/python_26_centos.html

oggy
  • 3,483
  • 1
  • 20
  • 24
3

When you install your python version (in this case it is python2.6) then issue this command to create your virtualenv:

virtualenv -p /usr/bin/python2.6 /your/virtualenv/path/here/
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
user1427258
  • 68
  • 1
  • 7
2

Late to the party, but the OP should have gone with Buildout or Virtualenv, and sidestepped the problem completely.

I am currently working on a Centos server, well, toiling away would be the proper term and I can assure everyone that the only way I am able to blink back the tears whilst using the software equivalents of fire hardened spears, is buildout.

chiggsy
  • 8,153
  • 5
  • 33
  • 43
  • 2
    can you use virtualenv to install python2.6 when only python2.4 is installed? – Collin Anderson Oct 11 '10 at 16:53
  • Certainly. Use --no-site-packages option in virtualenv I believe, or build a python in buildout. – chiggsy Oct 16 '10 at 04:30
  • NO, virtualenv just deals with python modules/packages and their versions. It still needs to be pointed at a working python interpreter binary (already on the system). – Jason Antman Jun 07 '13 at 03:24
1
# yum groupinstall "Development tools"
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

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

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

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
# python --version
Python 3.3.0
Moddasir
  • 1,449
  • 13
  • 33
1

you can always make your own RPM:

http://www.grenadepod.com/2009/12/26/building-python-2-6-4-rpm-for-centos-5-4/

rytis
  • 2,649
  • 22
  • 27
1

Missing Dependency: libffi.so.5 is here :

ftp://ftp.pbone.net/mirror/centos.karan.org/el5/extras/testing/i386/RPMS/libffi-3.0.5-1.el5.kb.i386.rpm

lykich
  • 11
  • 1
1
rpm -Uvh http://yum.chrislea.com/centos/5/i386/chl-release-5-3.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CHL
rpm -Uvh ftp://ftp.pbone.net/mirror/centos.karan.org/el5/extras/testing/i386/RPMS/libffi-3.0.5-1.el5.kb.i386.rpm
yum install python26
python26

for dos that just dont know :=)

bstpierre
  • 30,042
  • 15
  • 70
  • 103
Cheap VPN
  • 11
  • 1
0

I unistalled the original version of python (2.6.6) and install 2.7(with option make && make altinstall) but when I tried install something with yum didn't work.

So I solved this issue as follow:

  1. # ln -s /usr/local/bin/python /usr/bin/python
  2. Download the RPM package python-2.6.6-36.el6.i686.rpm from http://rpm.pbone.net/index.php3/stat/4/idpl/20270470/dir/centos_6/com/python-2.6.6-36.el6.i686.rpm.html
  3. Execute as root rpm -Uvh python-2.6.6-36.el6.i686.rpm

Done

-1

Type the following commands on the terminal to install Python 3.6 on CentOS 7:

$ sudo yum install https://centos7.iuscommunity.org/ius-release.rpm

Then do :

$ sudo yum install python36u 

You can also install any version instead of 3.6 (if you want to) by just replacing 36 by your version number.

iBug
  • 35,554
  • 7
  • 89
  • 134