Build And Install Python From Source

Updated: 2 minute read

Background/Problem

At the time of writing this, the current version of Python on my Arch Linux system was Python 3.10.4. For one of my older python projects I needed version 3.8. Therefore I tried to install it from the AUR as I described it in an earlier post. Unfortunately this didn’t work due to some PGP keys that could not be verified. I didn’t want to deal with that problem, and figured “why not build Python myself and then install it?”.

Well, this turned out to be a little more complicated than I expected. In order to avoid the same problem in case I ever need that again, here the solution that worked.

Solution

First of all, I looked on the Download Python [1] page for the version I needed (3.8.13) and downloaded the source files.

After extracting the downloaded archive, I changed into the new directory. In there I run:

./configure \
    --prefix=/opt/python \
    --enable-shared LDFLAGS=-Wl,-rpath=/opt/python/lib \
    --with-computed-gotos \
    --enable-optimizations \
    --with-lto \
    --enable-ipv6 \
    --with-system-expat \
    --with-dbmliborder=gdbm:ndbm \
    --with-system-ffi \
    --enable-loadable-sqlite-extensions \
    --without-ensurepip \
    --with-tzpath=/usr/share/zoneinfo \
    --with-threads

I added the --prefix option, because I wanted this python version (and potentially others as well) to be installed in /opt/python. I did that to avoid any possibility that python version that are installed this way would interfere with the system version of python. Also, since the generated Makefile does not have an “uninstall” target, having the manually installed python version in /opt/python would it make easier to remove them if needed.

I’m not exactly sure if adding --enable-shared is a good idea or not, but I seems as if this was done in different resources that I found online. After building python and installing it for the first time, I couldn’t open python. It terminated with an error message letting me know that libpython3.8.so.1.0 could not be found. In order to fix that, I had to add “LDFLAGS=-Wl,-rpath=/opt/python/lib”. I found this here [2].

At the beginning, I had also added the option --with-system-libmpdec set, but this caused a compiler error when building the module _decimal. Since this library is shipped with python’s source code, I just got rid of the option --with-system-libmpdec.

After configuring, I ran

make -j
make test
sudo make altinstall

After that Python 3.8.13 was available by running:

/opt/python/bin/python3.8

Change Log

2022-09-20:

  • Changed wording a little bit.
  • Adjusted the formatting slightly.



Take care,
Andreas


References

  1. Python Software Foundation, “Download Python.” [Online]. Available at: https://www.python.org/downloads/. [Accessed: 04-Apr-2022].
  2. Python Software Foundation, “Issue 27685.” [Online]. Available at: https://bugs.python.org/issue27685. [Accessed: 04-Apr-2022].

Updated:

Leave a comment