title:Compile static python keywords:linux,python,standalone # Compile static python ## Intro Not all distros or environment have decent version of python. Even of year when python2 is deprecated some cases when system are stuck to some particular version of python just becouse of there is no new updated for distribuations. Only options is to compile lates python by yourself and stick to latest version of python. To easy distribution there is preference of static compilation. Also need of admin priviledges not needed if you compile and use recent staticly compiled python version. One of the example Centos6 its still used, but nothing is updated for this distro ... and if you whant to use freshest packages without admin permissions then its probably one way to go. ## Python 2 ### Download ```bash wget -c https://www.python.org/ftp/python/$(VERSION)/Python-$(VERSION).tgz tar -xvf Python-$(VERSION).tgz ``` ### Configure Set flags to make python compiled as static ```bash ./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-fPIC -static" --disable-shared --prefix=/custom/install/path ``` Inside Python directory there is files to change. Change Modules/Setup add *static* and uncomment modules that would like to have builtin. It make some time to try and run if any of modules have issuse. Here example file used [http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py2/Setup](http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py2/Setup) ### Compile ```bash make ``` ### Install Instalaton is going copy static python and modules that arent compiled to configured installed path. ```bash make install ``` Later on set env variables to point to correct location of custom installed python so can import all modules from correct location ```bash PYTHONPATH=/custom/output/lib PYTHONHOME=/custom/output ``` ## Python 3 ### Download ### Configure Set flags to make python compiled as static ```bash ./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-fPIC -static" --disable-shared --prefix=/custom/install/path ``` Inside Python directory there is files to change. Change Modules/Setup add *static* and uncomment modules that would like to have builtin. It make some time to try and run if any of modules have issuse. Here example file used [http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py3/Setup](http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py3/Setup) ### Compile Same as python2 ### Install Same as python2 ## Testing ### ArchLinux Did work by default without any changes ### Raspi4 Disabled this modules to make it compile ```bash _socket _posix pwd dl ``` Got alot of linking warnings but did worked and installed at the end. Selftest failed on: ```bash 0:01:10 load avg: 0.85 [119/404/14] test_email make: *** [Makefile:884: test] Segmentation fault ``` ### Centos6 Needed to disable modules in Modules/Setup to make it compile ``` _socket _posix pwd dl ``` Gcc suggested to use this options ```bash -pie -fPIC ``` Failed last state complaining that same version compiled libs should be used. Local setup issue probably. ## Compilation snippet Here is located compilation snippet that used to test static compilation [http://git.main.lv/cgit.cgi/compilation-snippets.git/](http://git.main.lv/cgit.cgi/compilation-snippets.git/) Clone it, goto python directory #### Python2 ```bash make download cp py2/Setup Python-2.X.X/Modules/Setup make configure make compile make install ``` Output is in **output** directory #### Python3 ```bash make download3 cp py3/Setup Python-3.X.X/Modules/Setup make configure3 make compile3 make install3 ``` Output is in __output3__ directory ## Links [https://wiki.python.org/moin/BuildStatically](https://wiki.python.org/moin/BuildStatically) [https://gist.github.com/ajdavis/9632280](https://gist.github.com/ajdavis/9632280) [https://stackoverflow.com/questions/1150373/compile-the-python-interpreter-statically](https://stackoverflow.com/questions/1150373/compile-the-python-interpreter-statically) [http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py2/Setup](http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py2/Setup) [http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py3/Setup](http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py3/Setup)