Search code examples
python-3.xdockergdaldjango-3.2

OSError: /usr/lib/libgdal.so: cannot open shared object file: No such file or directory


I'm using Django 3.2 on Ubuntu 22.04 LTS. I'm running a docker container with a django-rest-api web server with python 3.8.14.I'm getting an error with regards to the GDAL library when i run docker-compose and fire up the container.

OSError: /usr/lib/libgdal.so: cannot open shared object file: No such file or directory

My django settings.py has :

GDAL_LIBRARY_PATH = "/usr/lib/libgdal.so"

Upon checking the gdal library version :

gdal-config --version
3.4.1

With regards to above, i installed pip3 gdal == 3.4.1
my-api-webserver | Traceback (most recent call last):
my-api-webserver |   File "manage.py", line 25, in <module>
my-api-webserver |     execute_from_command_line(sys.argv)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
my-api-webserver |     utility.execute()
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
my-api-webserver |     django.setup()
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
my-api-webserver |     apps.populate(settings.INSTALLED_APPS)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
my-api-webserver |     app_config.import_models()
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 301, in import_models
my-api-webserver |     self.models_module = import_module(models_module_name)
my-api-webserver |   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
my-api-webserver |     return _bootstrap._gcd_import(name[level:], package, level)
my-api-webserver |   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
my-api-webserver |   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
my-api-webserver |   File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
my-api-webserver |   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
my-api-webserver |   File "<frozen importlib._bootstrap_external>", line 843, in exec_module
my-api-webserver |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
my-api-webserver |   File "/opt/my-api/kosmosnetwork_socialcommerce/src/commerce/apps/stores/models.py", line 3, in <module>
my-api-webserver |     from . import abstract_models
my-api-webserver |   File "/opt/my-api/kosmosnetwork_socialcommerce/src/commerce/apps/stores/abstract_models.py", line 1, in <module>
my-api-webserver |     from django.contrib.gis.db.models import Manager, PointField
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/db/models/__init__.py", line 3, in <module>
my-api-webserver |     import django.contrib.gis.db.models.functions  # NOQA
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/db/models/functions.py", line 3, in <module>
my-api-webserver |     from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/db/models/fields.py", line 3, in <module>
my-api-webserver |     from django.contrib.gis import forms, gdal
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/forms/__init__.py", line 3, in <module>
my-api-webserver |     from .fields import (  # NOQA
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/forms/fields.py", line 2, in <module>
my-api-webserver |     from django.contrib.gis.gdal import GDALException
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/gdal/__init__.py", line 28, in <module>
my-api-webserver |     from django.contrib.gis.gdal.datasource import DataSource
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/gdal/datasource.py", line 40, in <module>
my-api-webserver |     from django.contrib.gis.gdal.driver import Driver
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/gdal/driver.py", line 5, in <module>
my-api-webserver |     from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/gdal/prototypes/ds.py", line 9, in <module>
my-api-webserver |     from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/gdal/libgdal.py", line 50, in <module>
my-api-webserver |     lgdal = CDLL(lib_path)
my-api-webserver |   File "/usr/local/lib/python3.8/ctypes/__init__.py", line 373, in __init__
my-api-webserver |     self._handle = _dlopen(self._name, mode)
my-api-webserver | OSError: /usr/lib/libgdal.so: cannot open shared object file: No such file or directory
my-api-webserver exited with code 1

I can't seem to solve this problem and spent too much time on it. I was previously on Ubuntu 18.04 and switched to 22.04 to upgrade the libgdal library to see if upgrading version works that is done using :

sudo apt-get install libgdal-dev

gdal config --version on 18.04 was 2.4.2 which upgraded to 3.4.1 on Ubuntu 22.04

To check whether libgdal is installed :

ls -l /usr/lib/libgdal.so


Result : lrwxrwxrwx 1 root root 17 Mar 16  2022 /usr/lib/libgdal.so -> libgdal.so.30.0.1

I'm attaching my docker file :

FROM python:3.8.14

RUN apt-get -y update && apt-get -y upgrade && apt-get install -y ffmpeg 
COPY wait-for-it.sh /wait-for-it.sh


RUN export CPLUS_INCLUDE_PATH=/usr/include/gdal
RUN export C_INCLUDE_PATH=/usr/include/gdal

# Copy any files over
COPY entrypoint.sh /entrypoint.sh

# Copy any files over
COPY bootstrap_development_data.sh /bootstrap_development_data.sh

# Change permissions
RUN chmod +x /entrypoint.sh
RUN chmod +x /bootstrap_development_data.sh
RUN chmod +x /wait-for-it.sh
RUN groupadd -r docker && useradd -r -g docker earthling
RUN chown -R earthling /root/


ENTRYPOINT ["/entrypoint.sh"]

COPY requirements.txt /requirements.txt
RUN pip3 install --upgrade pip setuptools wheel
RUN pip3 install -r /requirements.txt
RUN yes | pip uninstall django-rq-scheduler
RUN yes | pip install -U django-rq-scheduler


VOLUME ["/opt/my-api"]

EXPOSE 80


CMD ["python", "manage.py", "runserver", "0.0.0.0:80"]

Help appreciated.


Solution

  • Thanks to @David for hinting in the right direction.

    Had to add this to my dockerFile :

    RUN apt-get -y install libgdal-dev