Score:2

Segmentation fault while installing Python 3.5: ensurepip

in flag

I got a segmentation fault while trying to install Python 3.5 on Ubuntu 21.10.

$ guettli@p15:~$ pyenv install 3.5.8

Downloading Python-3.5.8.tar.xz...
-> https://www.python.org/ftp/python/3.5.8/Python-3.5.8.tar.xz
Installing Python-3.5.8...

BUILD FAILED (Ubuntu 21.10 using python-build 20180424)

Inspect or clean up the working tree at /tmp/python-build.20211119144903.305257
Results logged to /tmp/python-build.20211119144903.305257.log

Last 10 log lines:
if test "xupgrade" != "xno"  ; then \
    case upgrade in \
        upgrade) ensurepip="--upgrade" ;; \
        install|*) ensurepip="" ;; \
    esac; \
     ./python -E -m ensurepip \
        $ensurepip --root=/ ; \
fi
Segmentation fault (core dumped)

The same happens, when I install Python-3.5.10.tar.xz (without pyenv). What can I do now?

Version: Ubuntu 21.10
Output of configure: https://pastebin.com/rie17s8Z
Output of make: https://pastebin.com/F3C4evR6
Output of make install: https://paste2.org/LKxHUYkM

With PYTHONFAULTHANDLER:

guettli@p15:~/Downloads/Python-3.5.10$ PYTHONFAULTHANDLER=1 LANG=C python3.5 -m ensurepip
Fatal Python error: Segmentation fault

Current thread 0x00007fb703b40740 (most recent call first):
  File "/usr/local/lib/python3.5/ctypes/__init__.py", line 276 in _reset_cache
  File "/usr/local/lib/python3.5/ctypes/__init__.py", line 541 in <module>
  File "<frozen importlib._bootstrap>", line 222 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 697 in exec_module
  File "<frozen importlib._bootstrap>", line 673 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "/usr/local/lib/python3.5/uuid.py", line 464 in <module>
  File "<frozen importlib._bootstrap>", line 222 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 697 in exec_module
  File "<frozen importlib._bootstrap>", line 673 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "/tmp/tmppx73eqwt/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/packages/urllib3/filepost.py", line 4 in <module>
  File "<frozen importlib._bootstrap>", line 634 in _load_backward_compatible
  File "<frozen importlib._bootstrap>", line 664 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "/tmp/tmppx73eqwt/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/packages/urllib3/request.py", line 7 in <module>
  File "<frozen importlib._bootstrap>", line 634 in _load_backward_compatible
  File "<frozen importlib._bootstrap>", line 664 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "/tmp/tmppx73eqwt/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 41 in <module>
  File "<frozen importlib._bootstrap>", line 634 in _load_backward_compatible
  File "<frozen importlib._bootstrap>", line 664 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "/tmp/tmppx73eqwt/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/packages/urllib3/__init__.py", line 8 in <module>
  File "<frozen importlib._bootstrap>", line 634 in _load_backward_compatible
  File "<frozen importlib._bootstrap>", line 664 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "<frozen importlib._bootstrap>", line 222 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1015 in _handle_fromlist
  File "/tmp/tmppx73eqwt/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/packages/__init__.py", line 27 in <module>
  File "<frozen importlib._bootstrap>", line 634 in _load_backward_compatible
  File "<frozen importlib._bootstrap>", line 664 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "<frozen importlib._bootstrap>", line 222 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 943 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "<frozen importlib._bootstrap>", line 222 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 943 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "/tmp/tmppx73eqwt/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/__init__.py", line 62 in <module>
  File "<frozen importlib._bootstrap>", line 634 in _load_backward_compatible
  File "<frozen importlib._bootstrap>", line 664 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "<frozen importlib._bootstrap>", line 222 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 943 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "<frozen importlib._bootstrap>", line 222 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 943 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "<frozen importlib._bootstrap>", line 222 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 943 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "/tmp/tmppx73eqwt/pip-9.0.1-py2.py3-none-any.whl/pip/__init__.py", line 21 in <module>
  File "<frozen importlib._bootstrap>", line 634 in _load_backward_compatible
  File "<frozen importlib._bootstrap>", line 664 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 957 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 968 in _find_and_load
  File "/usr/local/lib/python3.5/ensurepip/__init__.py", line 40 in _run_pip
  File "/usr/local/lib/python3.5/ensurepip/__init__.py", line 116 in bootstrap
  File "/usr/local/lib/python3.5/ensurepip/__init__.py", line 209 in _main
  File "/usr/local/lib/python3.5/ensurepip/__main__.py", line 4 in <module>
  File "/usr/local/lib/python3.5/runpy.py", line 85 in _run_code
  File "/usr/local/lib/python3.5/runpy.py", line 193 in _run_module_as_main
Segmentation fault (core dumped)
user535733 avatar
cn flag
What does the log say?
guettli avatar
in flag
@user535733 in above output you see the last ten lines of the output. There is nothing which looks relevant in the lines above these ten lines.
user535733 avatar
cn flag
@guetti, indeed. But that begs the question *why is a shell script being output to your log?* Look at more than the last 10 lines.
in flag
Is there anything unique to your installation? I've installed 3.5.10 (via the `.tar.xz`) on a 20.04 an a 21.04 installation in a virtual machine without complaint. Not sure what might be preventing you from installing this specific version ...
Carles Mateo avatar
cn flag
I suspect that some dependencies for building this version of Python are not compatible with Ubuntu 21.10. Do you need version 3.5.8? Can't you use a more modern version like 3.8?. I would try if it work in a Docker container with Ubuntu 20.04 LTS.
guettli avatar
in flag
@CarlesMateo the same error happens if I use Python 3.5.10. Unfortunately I need Python 3.5 for this case.
ru flag
you might want to be aware that modern Django does not work properly on Python 3.5 - so if you're stuck on old django which has a lot of security holes you have a larger problem here.
abu-ahmed al-khatiri avatar
in flag
i've installed 3.5.8 via the .tar.xz without complaint. probably you need `echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrc`
guettli avatar
in flag
@abu-ahmedal-khatiri did you do this on Ubuntu 21.10?
abu-ahmed al-khatiri avatar
in flag
@guettli i'm on 20.04
Score:4
in flag

I found the solution here: https://giters.com/pyenv/pyenv/issues/1889

Step 1: create file segfault.patch:

--- Include/objimpl.h
+++ Include/objimpl.h
@@ -250,7 +250,7 @@
         union _gc_head *gc_prev;
         Py_ssize_t gc_refs;
     } gc;
-    double dummy;  /* force worst-case alignment */
+    long double dummy;  /* force worst-case alignment */
 } PyGC_Head;

 extern PyGC_Head *_PyGC_generation0;
--- Objects/obmalloc.c
+++ Objects/obmalloc.c
@@ -643,8 +643,8 @@
  *
  * You shouldn't change this unless you know what you are doing.
  */
-#define ALIGNMENT               8               /* must be 2^N */
-#define ALIGNMENT_SHIFT         3
+#define ALIGNMENT               16               /* must be 2^N */
+#define ALIGNMENT_SHIFT         4

 /* Return the number of bytes in size class I, as a uint. */
 #define INDEX2SIZE(I) (((uint)(I) + 1) << ALIGNMENT_SHIFT)

Step 2: apply the patch:

user@host:Python-3.5.10$ patch -p0 < segfault.patch

Step 3: make && sudo make install

abu-ahmed al-khatiri avatar
in flag
i'm already on Impish. i tried it, a voted to awesome answered.
gorandp avatar
ng flag
Almost give up. Your patch worked for Python 3.6.15 in Kubuntu 21.10. Although I've already installed it from source, I made the patch, executed `make && sudo make altinstall` and now `python3.6 -m pip list` doesn't return a segmentation fault, thanks. Upvoted!
spacediver avatar
cn flag
You've saved my hair! Worked with vanilla python 3.6.15 on ubuntu 22.04.1.
MusashiAharon avatar
de flag
Also works in Python 3.5.9 using an alpine v3.17.2 VM! Before, `pyenv install 3.7.9` was failing on `./python -c 'import ctypes'`. Thanks.
Score:1
pk flag

Miniconda

I suggest installing miniconda to manage your virtual environment and installing the desired python version within the newly created env.

In my opinion, it's a decent solutions not as bloated as anaconda and you can still use pip, poetry or whatever package management software that you would like.

Abbas Jafari avatar
sa flag
Short and useful
Score:1
cn flag

I tried this and got the same error; also tried 3.5.10 the best in this release line and still the same. I assumed it was a problem with pyenv and tried 2.2.0 but still the same result. So I downloaded Python 3.5.8 and proceeded to install manually (at the install directory, see Python docs) with:

./configure --with-pydebug --enable-optimizations --with-lto

make

But in make the python got hung up on testing the distribution. So the advice is don't use any options with ./configure. Then run:

make

sudo make install or sudo make altinstall

There is more than one problem in this build 3.5.8 of which the main reason why pyenv doesn't work seems to be due to this:

/home/<me>/Python-3.5.8/Modules/nismodule.c:17:10: fatal error: rpc/rpc.h: No such file or directory 17 | #include <rpc/rpc.h>

Then use Pycharm to build you project or simply use venv or virtulenv.

guettli avatar
in flag
Thank you for reproducing it. Unfortunately I still get the seg-fault, even if I use a newly extracted sources and don't provide any options to `configure`. It happens during `sudo make altinstall`, too.
mike mcleod avatar
cn flag
@guettli could you give more info: what you did exactly, and the output? It could be that 3.5.8 has a fatal bug in the build but I can still produce an executable.
guettli avatar
in flag
you said that you can reproduce the error. Nice. I don't do anything special. Just untar, configure, make, sudo make install. I uploaded the logs of each step to the question.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.