Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Docker fails to install cffi with python:3.9-alpine in Dockerfile

Im trying to run the below Dockerfile using docker-compose.
I searched around but I couldnt find a solution on how to install cffi with python:3.9-alpine.

I also read this post which states that pip 21.2.4 or greater can be a possible solution but it didn’t work out form me

https://www.pythonfixing.com/2021/09/fixed-why-i-getting-this-error-while.html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Docker file

FROM python:3.9-alpine

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt .

RUN apk add --update --no-cache postgresql-client

RUN apk add --update --no-cache --virtual .tmp-build-deps \
    gcc libc-dev linux-headers postgresql-dev
RUN pip3 install --upgrade pip && pip3 install -r /requirements.txt

RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY . /app

RUN adduser -D user

USER user

This is the requirements.txt file.

asgiref==3.5.0
backports.zoneinfo==0.2.1
certifi==2021.10.8
cffi==1.15.0
cfgv==3.3.1
charset-normalizer==2.0.12
coreapi==2.3.3
coreschema==0.0.4
cryptography==36.0.1
defusedxml==0.7.1
distlib==0.3.4
dj-rest-auth==2.2.3
Django==4.0.3
django-allauth==0.49.0
django-extensions==3.1.5
djangorestframework==3.13.1
djangorestframework-simplejwt==5.1.0
drf-yasg==1.20.0
filelock==3.6.0
httpie==3.0.2
identify==2.4.11
idna==3.3
inflection==0.5.1
itypes==1.2.0
Jinja2==3.0.3
MarkupSafe==2.1.0
multidict==6.0.2
nodeenv==1.6.0
oauthlib==3.2.0
packaging==21.3
platformdirs==2.5.1
pre-commit==2.17.0
psycopg2==2.9.3
pycparser==2.21
Pygments==2.11.2
PyJWT==2.3.0
pyparsing==3.0.7
PySocks==1.7.1
python3-openid==3.2.0
pytz==2021.3
PyYAML==6.0
requests==2.27.1
requests-oauthlib==1.3.1
requests-toolbelt==0.9.1
ruamel.yaml==0.17.21
ruamel.yaml.clib==0.2.6
six==1.16.0
sqlparse==0.4.2
toml==0.10.2
uritemplate==4.1.1
urllib3==1.26.8
virtualenv==20.13.2

Error message:

#9 44.81 Installing collected packages: pytz, nodeenv, itypes, distlib, certifi, urllib3, uritemplate, toml, sqlparse, six, ruamel.yaml.clib, PyYAML, PySocks, pyparsing, PyJWT, Pygments, pycparser, psycopg2, platformdirs, oauthlib, multidict, MarkupSafe, inflection, idna, identify, filelock, defusedxml, charset-normalizer, cfgv, backports.zoneinfo, asgiref, virtualenv, ruamel.yaml, requests, python3-openid, packaging, Jinja2, Django, cffi, requests-toolbelt, requests-oauthlib, pre-commit, djangorestframework, django-extensions, cryptography, coreschema, httpie, djangorestframework-simplejwt, dj-rest-auth, coreapi, drf-yasg, django-allauth
#9 47.45   Running setup.py install for cffi: started
#9 47.98   Running setup.py install for cffi: finished with status 'error'
#9 47.99   error: subprocess-exited-with-error
#9 47.99   
#9 47.99   × Running setup.py install for cffi did not run successfully.
#9 47.99   │ exit code: 1
#9 47.99   ╰─> [58 lines of output]
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       running install
#9 47.99       running build
#9 47.99       running build_py
#9 47.99       creating build
#9 47.99       creating build/lib.linux-aarch64-3.9
#9 47.99       creating build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/__init__.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/cffi_opcode.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/commontypes.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/vengine_gen.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/vengine_cpy.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/backend_ctypes.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/api.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/ffiplatform.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/verifier.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/error.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/setuptools_ext.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/lock.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/recompiler.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/pkgconfig.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/cparser.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/model.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/_cffi_include.h -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/parse_c_type.h -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/_embedding.h -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/_cffi_errors.h -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       warning: build_py: byte-compiling is disabled, skipping.
#9 47.99       
#9 47.99       running build_ext
#9 47.99       building '_cffi_backend' extension
#9 47.99       creating build/temp.linux-aarch64-3.9
#9 47.99       creating build/temp.linux-aarch64-3.9/c
#9 47.99       gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DUSE__THREAD -DHAVE_SYNC_SYNCHRONIZE -I/usr/include/ffi -I/usr/include/libffi -I/usr/local/include/python3.9 -c c/_cffi_backend.c -o build/temp.linux-aarch64-3.9/c/_cffi_backend.o
#9 47.99       c/_cffi_backend.c:15:10: fatal error: ffi.h: No such file or directory
#9 47.99          15 | #include <ffi.h>
#9 47.99             |          ^~~~~~~
#9 47.99       compilation terminated.
#9 47.99       error: command '/usr/bin/gcc' failed with exit code 1
#9 47.99       [end of output]
#9 47.99   
#9 47.99   note: This error originates from a subprocess, and is likely not a problem with pip.
#9 47.99 error: legacy-install-failure
#9 47.99 
#9 47.99 × Encountered error while trying to install package.
#9 47.99 ╰─> cffi
#9 47.99 
#9 47.99 note: This is an issue with the package mentioned above, not pip.
#9 47.99 hint: See above for output from the failure.

>Solution :

The libffi library is missing.

Add it to your dockerfile:

RUN apk add libffi-dev
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading