How to install Python from source code

Here, let us see how to install any python version from source code.

Step 1:

First, in order to get your python source file, go to the link, https://www.python.org/ftp/python/ and select the python version which you want to get installed.

For example, if you want to install python 3.0, select the link with the name as 3.0. It will open a list of python 3.0 zip files. 

Now select the gunzipped source file.

Step 2:

Now to get the file into your server, give the following command in the server.

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

For example, to get python 3.0 gunzipped source file, use the following command to get it in your server.

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

Step 3:

Now untar it using the following command.

tar xf Python-x.x.tgz

So once it is done, you will find a new folder with the same name as your gunzipped source file(Ex. Python-3.0)

Now we should execute the following commands from the newly created folder.
So navigate to the new folder using the command,

cd Python-x.x

Step 4:

Now we need to execute the following commands in order from the newly created folder to install the required python version successfully

  1. ./configure

This command runs a configure script that figures out how to call your compiler, where to find the libraries the software needs, where the new software should be installed, etc.

./configure cannot find a compiler, a required library, etc. then it will report an error. It automatically generates a script called Makefile.

2. make

The Makefile script is executed with the make command. The Makefile contains instructions on how to achive various tasks like “build” or “make the executable called python” or “run the test suite”

So running make command, will start the build process.

3. make test

Once the build process is completed, to make sure everything works as expected, you should run this command make test. This will eventually take a long time to complete.

4. make install

So once the execution of the above command is successful and everything is fine, we are now ready to install this python in your server.

Now give make install command to install the python.

It will by default install the software in /usr/local path.

Now python is successfully installed in your server!!

How to install python in a permanent custom path and not in the default location.

Now let us assume, you have another python version available, and you additionally want this new version.
But you also need not have any impact of this new version on any of the files of the older version.

So the best solution is to install the new python version in any path other than the default location.

If you give ./configure command at the start, the newer version will get installed globally in the location /usr/local as mentioned above.

This will have an impact on the older version files.

So to avoid this, we need to get this new version installed on a customized path.

To achieve this,
we need to make a change in the ./configure command. The rest of the steps are the same.

In the configure command, add the –prefix and provide the custom path where you want the newer version to be installed.

./configure --prefix=/custom/directory

so the configuration will happen in the custom path that you are providing in the prefix field

Then give the rest of the commands as ususal,

make

make test

make install

Well!! Now the python is installed in a custom path that you preferred!.

Thank you!!!

Thanks for using Pheonix solutions.

Leave a Reply