Install Python 3.x in CentOS / RedHat 6.x from sources

CentOS and RedHat 6.x ship with Python 2, so you cannot/shouldn't install Python 3 at the system level (with or without the package manager). But you can setup Python 3.x (3.4.3 in our example below) in a separate "virtualenv" environment, not affecting the system packages and allowing you to develop Python 3.x programs.

This minitutorial is based on a stackoverflow tutorial that provided the steps for v3.2.

As we're going to compile python3, we need to install the required development packages:

# yum groupinstall "Development tools"
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel \
      sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel

Download Python 3.4 sources and compile / install them (note that any other version can be installed by downloading a different .tar.xz file and replacing -3.4 in the commands below by the proper version number):

# cd /usr/local/src
# wget --no-check-certificate
# tar xf Python-3.4.3.tar.xz
# cd Python-3.4.3
# ./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
# make && sudo make altinstall
# /usr/local/bin/python3.4 --version

Now download and install Setuptools + pip:

# cd /usr/local/src
# wget
# /usr/local/bin/python3.4
# /usr/local/bin/pip3.4 install --upgrade setuptools
# /usr/local/bin/pip3.4 install --upgrade distribute
# /usr/local/bin/easy_install-3.4 pip

Create your isolated Python 3.4 environment (replace /path/to/your/virtenv3.4 with the desired path for Python3 development, such as /home/youruser/prog/python3.4env/):

# /usr/local/bin/pip3.4 install virtualenv
# /usr/local/bin/virtualenv /path/to/your/virtenv3.4
# cd /path/to/your/virtenv3.4
# source bin/activate
# python --version # (To check version)
# pip install --upgrade pip

Now, each time you do "source bin/activate" inside the virtualenv directory, you'll start your virtualenv with Python 3.4. Inside this virtualenv, you must install the desired third party modules with pip:

pip install PyMySQL

Or, for pycurl:

pip install --compile pycurl

Once you've installed the desired modules, you can create folders for each project inside your virtualenv environment (or create new virtualenvs for each project using the examples above) and enjoy Python 3.x.

(NOTE: If you need to recompile / reinstall any pip package -like pycurl after replacing the PYCURL_SSL_LIBRARY variable-, first remove /root/.cache/pip/ or you will install again and again the same compiled binary)

<Volver a la sección de GNU/Linux>