Building Programs from Source on any Linux Distribution in a simple way

Today we will guide you on building and compiling your first program in Linux, and trust me, it will be a good adventure.

It is often good to stick with distribution for any particular packages, which is not compiled for another distribution. But learning to build and compile a program makes it possible to use your package in any Linux distribution.

With this, you can build a program from a source for any Linux Distribution. You can even switch to a different version of packages. Imagine today’s latest python version is 3.9.0, but you developed a project year ago that worked in python 3.6.0.

For new Linux users, compiling and installing programs from source code for the first time might be a nightmare. But it’s not; compiling from source code opens possibilities to use your favorite packages for any distribution.

Installation of packages from source code

Today, we demonstrate how to install python 3.6.0, that exact version from the source in Ubuntu. First, we visit the official python website to download the version we needed https://www.python.org/ftp/python/.

python source file
Python Official Site

After clicking 3.6.0, you will be redirected to another page to get all packages related to Python 3.6.0. In my case, I am choosing Python-3.6.0.tar.xz.

python source file download
Python 3.6.0 package

Download it using a browser by clicking on it or using wget. If you are going to use the wget command, make sure it is installed in your system. If it is not installed you can easily install it using the below command in Debian-based distribution.

$ apt install wget

Get information about 10 Wget Command Examples in Linux

Now we downloaded python 3.6.0 using wget and extract it in the home directory.

$ wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz -O /home/Python-3.6.0.tar.xz
$ tar -xf Python-3.6.0.tar.xz
$ cd Python-3.6.0

Configure, make and make install

Now here, the fun part begins. We will first configure python using configure script. If you think about what configure script is, configure script is a set of rules for getting ready to build the package on your specific distribution.

It ensures all of the required dependencies to build the package and start the installation process are available. And if required dependencies are not available, it finds out whatever it’s need to use the dependencies.

Below is a command to configure python for your system. If you decide to choose other than 3.6.0, then change ${PYTHON_VERSION} with your respective version.

$ ./configure \
    --prefix=/opt/python/${PYTHON_VERSION}
    --enable-shared
    --enable-ipv6
    LDFLAGS=-Wl,-rpath=/opt/python/${PYTHON_VERSION}/lib,--disable-new-dtags

In my case, I am going to show you using python 3.6.0.

./configure \
    --prefix=/opt/python/3.6.0
    --enable-shared
    --enable-ipv6
    LDFLAGS=-Wl,-rpath=/opt/python/3.6.0/lib,--disable-new-dtags

After we configure, let’s move forward and make a package. “Make” utility basically determine which pieces of a large program need to be re-compiled, and issue the command necessary to recompile them.

$ make

Once the compilation is done, you can install your software to its location, running the following command.

$ sudo make install

Before jumping to the terminal and typing python, there is one thing left to do. We have to set Python3.6 to PATH.

I suggest adding Python to the PATH using the version of Python that you installed to the system-wide PATH variable.

Type below command to add Python 3.6.0 to your system PATH.

$ PATH=/opt/python/3.6.0/bin/:$PATH

After adding Python 3.6.0 to system PATH, you can easily access it using the below command.

$ python3.6

Now test it:

$ python3.6 -v

Now compiling and installing same python version for RHEL/CentOS Linux

Again first, we have to download the python package which we want to install in our system.

$ wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz -O /home/Python-3.6.0.tar.xz
$ tar -xf Python-3.6.0.tar.xz
$ cd Python-3.6.0

Then we configure the python package to download and configure all required dependencies for our system using the configure script.

./configure \
    --prefix=/opt/python/3.6.0
    --enable-shared
    --enable-ipv6
    LDFLAGS=-Wl,-rpath=/opt/python/3.6.0/lib,--disable-new-dtags

This time there is one small issue which you will identify by looking at the following message below.

configure: error: in `/home/Python-3.6.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

Definitely, we need a C++ compiler to compile the package for our system. Here, I am going to install GCC GNU project C and C++ compiler.

$ yum install gcc

After successfully install C and C++ Compiler. We are going to perform the same steps of configuring the package again. Then we execute the make command to ensure all required dependencies are available before making an installation.

$ make

Finally, We going to start installation process by running below command.

$ sudo make install

Again like earlier, before hitting python3.6 in your terminal, make sure it is set in your PATH. To set python3.6 in your system PATH, execute the below command.

$ PATH=/opt/python/3.6.0/bin/:$PATH

Finally, we have compiled our first package from its source in two separate Linux Distribution.

How to remove the newly installed packages from source

Most of the compiled packages from the source are located in /opt/ directory. You can find our newly installed python3.6.0 packages in /opt/python, remove these packages require no more than using rm command to remove that directory.

sudo rm -rf /opt/python

Make sure you are entering the correct path; the wrong path can lead to the removal of a sensitive file. Using -rf, you do not prompt any confirmation before deleting a file.

Then, if you’ve set python in system PATH, make sure to remove it from there also after this process.

Conclusion

Installation of packages from a source varies differently for a different package. If you were learning about compiling of own package, then you may be coming to the term Dependency hell.

Dependency hell is a term to being user have to first a pre-requisite library, Which requires another library that might be incompatible with some other packages in your system.

For that user have to take care of the all libraries used by other packages and required a library for the current package.

That all how we compile and install packages in our system and make sure to read README file because some of the packages require extra steps. If you are facing any issues, feel free to ask in the comment section.

This Post Has 2 Comments

  1. anonim

    I advise you to use “./configure –help”
    This will show you all the available options.
    Sometimes some options may be very old and not work, please take this into account.

Leave a Reply