6

Problem

I'm trying to install Python 3 with the --enable-shared option. Installation "succeeds" but the resulting Python is not runnable. Trying to run Python after installation gives the following error:

$ /opt/python3/bin/python3.5
/opt/python3/bin/python3.5: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

Background

The OS is Debian (squeeze), and has a previous installation of Python 2.6, which is necessary to retain because other code relies on it, and Apache 2.2. Ultimately what I'm trying to do is set up Django to run on Apache, meaning I'm trying to install mod_wsgi (or mod_wsgi-express), which requires shared libraries. I have already tried to install mod_wsgi without using --enable-shared in the Python installation, and have gotten... well, the same thing, but this time from the mod_wsgi installer (and from pip install mod_wsgi, which I also tried): /opt/python3/bin/python3.5: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory.

Trace

Starting from an installation as described in Background above, here is the minimum list of commands I've executed that produce the error above (with verbosity removed).

user@server:~$ wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
user@server:~$ tar -zxvf Python-3.5.1.tgz
user@server:~$ cd Python-3.5.1
user@server:~/Python-3.5.1$ ./configure --prefix=/opt/python3 --enable-shared
user@server:~/Python-3.5.1$ make && sudo make install
(... appears to install correctly)

user@server:~/Python-3.5.1$ /opt/python3/bin/python3.5
/opt/python3/bin/python3.5: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

I have also tried this with LD_RUN_PATH set as described in the solution to this other question, with the same results:

user@server:~/Python-3.5.1$ sudo make distclean
user@server:~/Python-3.5.1$ ./configure --prefix=/opt/python3 --enable-shared
user@server:~/Python-3.5.1$ LD_RUN_PATH=/usr/local/lib make
user@server:~/Python-3.5.1$ sudo make install
user@server:~/Python-3.5.1$ /opt/python3/bin/python3.5
/opt/python3/bin/python3.5: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

I have also tried this with Python 3.4, with the same results. I have not tried this with Python 2, because I do not want future development to be limited to Python 2.7 (therefore even a successful installation would not satisfy my requirements). I'm also assuming the attempt would not provide any new or useful information.

user2100826
  • 335
  • 2
  • 3
  • 13

2 Answers2

7

I've repeated your steps on CentOS7 and get something similar:

$ /tmp/py3/bin/python3
/tmp/py3/bin/python3: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

If you look at the linking of python, it isn't providing a full path to the library:

$ ldd /tmp/py3/bin/python3
    linux-vdso.so.1 =>  (0x00007fff47ba5000)
    libpython3.5m.so.1.0 => not found
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fdfaa32e000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007fdfaa12a000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007fdfa9f27000)
    libm.so.6 => /lib64/libm.so.6 (0x00007fdfa9c24000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fdfa9862000)
    /lib64/ld-linux-x86-64.so.2 (0x000055e85eac5000)

For some reason, the Python build process isn't adding -rpath to the link line, which would "add a directory to the runtime library search path."

If you explicitly set your library path, it will work:

$ LD_LIBRARY_PATH=/tmp/py3/lib/ /tmp/py3/bin/python3
Python 3.5.1 (default, Jun 10 2016, 14:54:59) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

So now it becomes a question as to whether you want to:

  • set LD_LIBRARY_PATH globally on your system
  • Edit /etc/ld.so.conf (or /etc/ld.so.conf.d/*)
  • Use chrpath to change the embedded path
  • export LD_RUN_PATH={prefix}/lib before you run configure and make (Where {prefix} is what you passed to --prefix. You used the wrong path.)
Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
rrauenza
  • 6,285
  • 4
  • 32
  • 57
  • 2
    As for the last option, you can also pass loader flags to `./configure`. For example: `./configure --prefix=/opt/python3 --enable-shared LDFLAGS="-Wl,-rpath /opt/python3"`. – Rafa Viotti Jun 11 '16 at 18:22
  • Thanks @rrauenza! The part I was missing was that `LD_RUN_PATH` is related to `--prefix`, not an absolute/standard location. – user2100826 Jun 12 '16 at 12:03
2

As explained in the mod_wsgi documentation, set LD_RUN_PATH at the time of installing mod_wsgi.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • Thanks Graham. Maybe I shouldn't have tagged this mod-wsgi though because the problem is happening in the Python installation step, before I even get to installing mod-wsgi. I'll keep this in mind when I do, though. – user2100826 Jun 12 '16 at 11:50
  • 1
    Go read http://blog.dscpl.com.au/2015/06/installing-custom-python-version-into.html Ignore that it says Docker, all still relevant. – Graham Dumpleton Jun 12 '16 at 21:03