CentOS 5 uses python 2.4, and replacing it is not really on option since yum and other core packages depend on it. My solution is to compile 2.6 and use /opt or /usr/local for the prefix. I also create a virtualenv with the new python executable, so when I’m in the environment 2.6 becomes the default python. It also isolates all my python libraries for a given project. Most of this article is actually distribution agnostic, and the yum build requirement install will likely work on other versions of CentOS and other RedHat derivatives like Fedora.
Install Build Requirements
Run the following command as root (or with sudo) to install gcc and the development libraries used by python:
yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel \ bzip2-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel
Compile Python
Download and unpack the source tarball for your version of choice. For example:
VERSION=2.6.1 mkdir ~/src cd ~/src wget http://python.org/ftp/python/$VERSION/Python-$VERSION.tar.bz2 tar xjf Python-$VERSION.tar.bz2 rm Python-$VERSION.tar.bz2 cd Python-$VERSION ./configure --enable-ipv6 --prefix=/opt make sudo make install
Run ./configure –help for more configure options.
If you plan to install multiple versions of python to the same prefix, use “sudo make altinstall” instead of “make install” for the versions you want installed as python2.6 etc, and “make install” for the default version you want installed as simply python. See the README for more details.
After running the make command you will see a list of modules that were not built. If you installed all of the devel libraries listed above, the only missing modules should be bsddb185 and sunaudiodev. You probably don’t need these – bsddb185 is the old version of the berkely db module, and sunaduiodev is for solaris. On x86_64 db and imageop may also be in the list, but looking at the setup.py, it looks like this is normal.
Install setuptools
Download setuptools – get the egg if it’s available for your version of python, otherwise get the tarball. In either case prepend PREFIX/bin to your path, where PREFIX is what you passed to configure when building python (/opt in my example). Then either run the egg as a shell script or unpack the tarball and run “python setup.py install”. Here’s an example for setuptools 0.6c9 and python 2.6:
wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c9-py2.6.egg#md5=ca37b1ff16fa2ede6e19383e7b59245a sudo PATH=/opt/bin:$PATH sh setuptools-0.6c9-py2.6.egg rm setuptools-0.6c9-py2.6.egg
Install virtualenv and virtualenvwrapper
sudo /opt/bin/easy_install virtualenv sudo /opt/bin/easy_install virtualenvwrapper
Install More Packages (Optional)
If you plan on using other modules in all your projects, and don’t need different versions, you can install them once in the main site-packages dir for your new python install. Just make sure to use the full path to easy_install (or prepend /opt/bin to the PATH when running eggs or python setup.py), otherwise they will be installed for the system python (2.4 in CentOS 5).
Setup virtualenvwrapper
UPDATED 4-9: Thanks Brian for the feedback. Hopefully this is more clear.
Create a directory to store your virtual environments:
mkdir ~/.virtualenvs
Add the following lines to your bashrc (~/.bashrc):
export PATH=/opt/bin:$PATH export WORKON_HOME=$HOME/.virtualenvs source /opt/bin/virtualenvwrapper_bashrc
If you used a prefix other than /opt (e.g. /usr/local) when installing python, then change the PATH line and the source line accordingly. This also assumes you are using bash. I’m not sure how to set up virtualenvwrapper for other shells.
The PATH line will actually make python 2.6 your default interpreter for login sessions. If this is undesirable you may be able to copy virtualenv to some other location already in your path, and it will should still use the appropriate version of python. Be careful if you have virtualenv installed under your main system python – virtual environments created with other version will be tied to that version of python.
New login sessions will automatically source ~/.bashrc and have support for virtualenvwrapper – to update your currently running shell, source it explicitly:
source ~/.bashrc
IMPORTANT: you should use “#!/usr/bin/env python” for your scripts instead of “#!/path/to/python” to make them more portable and work in different virtualenv.
More documentation: virtualenvwrapper, Doug Hellmann’s original virtualenvwrapper article, virtualenv
Create a virtualenv (using virtualenvwrapper)
Choose a name for your new environment. I will use NewEnv as a placeholder. You can create new environments with mkvirtualenv:
mkvirtualenv NewEnv
This command will also automatically activate the new environment. To activate NewEnv in another shell, use the “workon” command:
workon NewEnv
Running workon without any arguments will give you a list of all the virtualenv’s you’ve created. Here are some common packages you may want to easy_install into your new virtualenv. Note that you don’t need to use sudo to install stuff into your virtualenv:
workon NewEnv easy_install MySQL-python easy_install twisted easy_install egenix-mx-base
Install PostgreSQL and psycopg2
Centos 5 has postgres 8.1. To install a 8.3, setup the PostgreSQL yum repository, remove any old versions (but not -libs), and yum install postgresql-devel. Note that you may need to remove nfs-utils to avoid a libevent conflict (or maybe find an updated nfs-utils if you really need it).
sudo yum erase nfs-utils sudo yum erase postgresql postgresql-devel sudo rpm -ivh http://yum.pgsqlrpms.org/reporpms/8.3/pgdg-centos-8.3-6.noarch.rpm sudo yum update sudo yum install postgresql-devel workon NewEnv easy_install psycopg2
Following the instructions I was able to get virtual wrapper up and running. I had a few rough spots setting up my bashrc. So here is my specific information:
# User specific aliases and functions
export PATH=/opt/bin:$PATH
export WORKON_HOME=$HOME/.virtualenvs
source /opt/bin/virtualenvwrapper_bashrc
Looking at the last line where the virtualenvwrapper_bashrc is sourced, an error is going to occur each time this is run. The guide nor does the above instructions mention creating the .virtualenvs directory. So in your home directory:
mdkir .virtualenvs
Once the workon is run and then the mkvirtualenv temp completes, the next bashrc start will have the temp for the virtual environment.
Otherwise great instructions and very helpful for creating a controlled python environment. This is a good way of keeping a team working together by using the same code levels.
Thank you for this fantastic set of instructions. Just one amendment/suggestion. The line in ~/.bashrc should read:
“source /opt/bin/virtualenvwrapper.sh“ as per the Installation/’Upgrading from 1.x’ section at http://pypi.python.org/pypi/virtualenvwrapper
Thanks for this. Very easy to follow.
Some problems with python 2.7.2, virtualenvwrapper 2.10.1, solved by setting the python instance for virtualenv in ~/.bashrc
#use virtualenvs per user:
mkdir ~/.virtualenvs
#add/modify the following to the ~/.bashrc
echo >> ~/.bashrc
#I dont’ want to affect the python binary instance that is offered to all the users, so we’re commenting out the following line:
echo \#export PATH=\$PATH:/opt/bin >> ~/.bashrc
echo export WORKON_HOME=\$HOME/.virtualenvs >> ~/.bashrc
echo export VIRTUALENVWRAPPER_PYTHON=/opt/bin/python >> ~/.bashrc
echo source /opt/bin/virtualenvwrapper.sh >> ~/.bashrc
source ~/.bashrc
I was receiving:
“ImportError: No module named virtualenvwrapper.hook_loader”
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is set properly.
[…] The following section is a technical simplification of the following page to install python into a specific location: http://bda.ath.cx/blog/2009/04/08/installing-python-26-in-centos-5-or-rhel5/ […]
Ugh, these instructions were absolutely amazing. I just spent the last 24 hours trying to install Twisted on CentOS 5, and it was an absolute nightmare. These instructions worked beautifully and now I’m running Twisted 11 on py2.6.
After I followed the instructions I just did /opt/bin/easy_install twisted and it worked like a charm.
Thanks so much, you’re amazing!
Can anyone help? I got errors when trying “/opt/bin/easy_install virtualenvwrapper” and couldn’t seem to find anything from google.
/opt/bin/easy_install virtualenvwrapper
Searching for virtualenvwrapper
Reading http://pypi.python.org/simple/virtualenvwrapper/
Best match: virtualenvwrapper 4.1.1
Downloading https://pypi.python.org/packages/source/v/virtualenvwrapper/virtualenvwrapper-4.1.1.tar.gz#md5=f18f2c612b936583a8ec0f7114b6637e
Processing virtualenvwrapper-4.1.1.tar.gz
Running virtualenvwrapper-4.1.1/setup.py -q bdist_egg –dist-dir /tmp/easy_install-gIYCea/virtualenvwrapper-4.1.1/egg-dist-tmp-oaCjsg
Checking .pth file support in .
/opt/bin/python2.6 -E -c pass
Searching for pbr>=0.5.19
Reading http://pypi.python.org/simple/pbr/
Best match: pbr 0.5.21
Downloading https://pypi.python.org/packages/source/p/pbr/pbr-0.5.21.tar.gz#md5=1dafd1ef666b9bce4d880170ddc39387
Processing pbr-0.5.21.tar.gz
Running pbr-0.5.21/setup.py -q bdist_egg –dist-dir /tmp/easy_install-gIYCea/virtualenvwrapper-4.1.1/temp/easy_install-Prh_Pq/pbr-0.5.21/egg-dist-tmp-PLfFJs
Installed /tmp/easy_install-gIYCea/virtualenvwrapper-4.1.1/pbr-0.5.21-py2.6.egg
Searching for pip>=1.0
Reading http://pypi.python.org/simple/pip/
Best match: pip 1.4.1
Downloading https://pypi.python.org/packages/source/p/pip/pip-1.4.1.tar.gz#md5=6afbb46aeb48abac658d4df742bff714
Processing pip-1.4.1.tar.gz
Running pip-1.4.1/setup.py -q bdist_egg –dist-dir /tmp/easy_install-gIYCea/virtualenvwrapper-4.1.1/temp/easy_install-N07Olv/pip-1.4.1/egg-dist-tmp-cH_0sg
warning: no files found matching ‘*.html’ under directory ‘docs’
warning: no previously-included files matching ‘*.rst’ found under directory ‘docs/_build’
no previously-included directories found matching ‘docs/_build/_sources’
Installed /tmp/easy_install-gIYCea/virtualenvwrapper-4.1.1/pip-1.4.1-py2.6.egg
/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/dist.py:245: UserWarning: Module pbr was already imported from /tmp/easy_install-gIYCea/virtualenvwrapper-4.1.1/temp/easy_install-Prh_Pq/pbr-0.5.21/pbr/__init__.py, but /tmp/easy_install-gIYCea/virtualenvwrapper-4.1.1/pbr-0.5.21-py2.6.egg is being added to sys.path
Traceback (most recent call last):
File “/opt/bin/easy_install”, line 8, in
load_entry_point(‘setuptools==0.6c9’, ‘console_scripts’, ‘easy_install’)()
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 1671, in main
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 1659, in with_ei_usage
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 1675, in
File “/opt/lib/python2.6/distutils/core.py”, line 152, in setup
dist.run_commands()
File “/opt/lib/python2.6/distutils/dist.py”, line 975, in run_commands
self.run_command(cmd)
File “/opt/lib/python2.6/distutils/dist.py”, line 995, in run_command
cmd_obj.run()
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 211, in run
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 446, in easy_install
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 476, in install_item
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 655, in install_eggs
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 930, in build_and_install
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/command/easy_install.py”, line 919, in run_setup
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/sandbox.py”, line 27, in run_setup
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/sandbox.py”, line 63, in run
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/sandbox.py”, line 29, in
File “setup.py”, line 7, in
File “/opt/lib/python2.6/distutils/core.py”, line 113, in setup
_setup_distribution = dist = klass(attrs)
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/dist.py”, line 223, in __init__
File “/opt/lib/python2.6/distutils/dist.py”, line 270, in __init__
self.finalize_options()
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/setuptools/dist.py”, line 256, in finalize_options
File “/opt/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/pkg_resources.py”, line 1913, in load
ImportError: No module named core
I would try a newer version of setuptools:
https://pypi.python.org/pypi/setuptools/1.1.6
bda, thank you. That worked. I had to change “source /opt/bin/virtualenvwrapper_bashrc” to “source /opt/bin/virtualenvwrapper.sh” though in case anyone runs into the same problem I had.