Installing Python From Source

You don't alway get up to date stable release with repository, better install from the source

Rodrigo Queiroz
21 Sep 2016
  • 4 min read
  • 101
  • 50
  • 200

Getting the Python release

First we need to go to Python's source download link at https://www.python.org/downloads/source/ and choose our Python version, for this tutorial I have chosen Python 3.5.2 as it's the latest stable release as of this writing.

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

or directory specific:

wget -P /tmp https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

The above command will download python 3.5.2 Gzipped source tarball to the current directory I'm currently in /root, you can specify -P as specified above to save on that directory.

If you get:

bash: wget: command not found

Install with:

yum install wget

Once the file is downloaded you can cd to the directory you choose and extract the tarball file:

tar xf Python-3.5.2.tgz

The parameters passed in to the tar simply means "extract from a file", don't expect any output as we have not specified, or you can pass in an extra parameter to force the output xvf, which means "extract verbosely from a file".

You should now have a directory called Python-3.5.2, which contains all python source.

cd Python-3.5.2

Building Python

Most Unix based operating systems uses the same process to build, compile and install applications from source.

./configure prepare and link files to build the environment, generates makefile. make uses the makefile to connect libraries and compile. make install or altinstall in our case - copy compiled files generated by make and install the application as configured using ./configure.

Please note that we will be using make altinstall to avoid future problems with other versions of python.

You would generally see these 3-steps when building from the source.

./configure
make
make install

Now we have almost everything set to start building Python, if you are installing on a fresh minimal Linux distribution as I am, you are probably going to need to install some other tools to compile, you can install the Development tools group which will contain all dependencies such as gcc, make etc.

yum groupinstall "Development tools"

Because python will try to install pip, we need to install some of it's dependencies so that our build works flawless.

Pip is part of the Extra Packages for Enterprise Linux (EPEL), which is a community repository of non-standard packages for the RHEL distribution.

Install the repository:

yum -y install epel-release

Now refresh the repository:

yum repolist

Pip is dependent on SSL/TLS, so let's install openssl.

yum install openssl openssl-devel

Next, we are going  to configure the installation directory  on /usr/local, we can change the path by passing extra parameters to ./configure, to be exact --prefix=/usr/local:

./configure --prefix=/usr/local

Now that we have a directory prefix set and configure we can execute make:

make

The build process will kick off and it should compile smoothly, since we are using a stable version of Python.

Whenever we are installing a custom version of Python we need to specifically use altinstall because we don't want to end up with two different versions of Python in the filesystem both name python. It can lead to problems that would be hard to diagnose.

Install:

make altinstall

You should now have Python installed in your system ready to go:

python3.5 --version
Python 3.5.2

For convenience you can make use of pip by creating a symlink:

ln -s /usr/local/bin/pip3.5 /usr/local/bin/pip

and

pip --version
pip 8.1.1 from /usr/local/lib/python3.5/site-packages (python 3.5)