OpenCV FAST API issue
spiral25
PROOP

a year ago

We're trying to deploy a FastAPI app with OpenCV on Railway, but it fails with "ImportError: libGL.so.1: cannot open shared object file: No such file or directory" when importing cv2, and our attempts to install system dependencies via nixpacks.toml are failing with "undefined variable" errors for packages like libsm, libGL, etc. - what's the correct way to install OpenGL/OpenCV system dependencies in Railway's Nixpacks environment?

PS. Also I was contacted on email by Melissa from Railway after my deployment failed. She was offering help, proposing to reply to her message directly. After I did so, my message bounced - apparently the group wasn't monitored. Frustrating experience across the board so far.

Solved$10 Bounty

Pinned Solution

a year ago

I believe Nixpacks' nixPkgs field is case-sensitive.

Can you please try changing libsm to libSM?

5 Replies

OpenCV is known to require a lot of apt system packaged. Can you share your nixpacks.toml?


Status changed to Awaiting User Response Railway 12 months ago


angelo-railway

OpenCV is known to require a lot of apt system packaged. Can you share your nixpacks.toml?

spiral25
PROOP

a year ago

my current nixpacks.toml:

toml
[phases.setup]
nixPkgs = ["libGL", "glib", "libsm", "libxext", "libxrender"]

[variables]
QT_QPA_PLATFORM = "offscreen"
OPENCV_IO_ENABLE_OPENEXR = "1"

I'm getting build failures - error: undefined variable 'libsm'

My app uses opencv-python-headless==4.8.0.74 with ultralytics for YOLO inference


Status changed to Awaiting Railway Response Railway 12 months ago


spiral25

my current nixpacks.toml: ``` toml ``` ```toml [phases.setup] nixPkgs = ["libGL", "glib", "libsm", "libxext", "libxrender"] [variables] QT_QPA_PLATFORM = "offscreen" OPENCV_IO_ENABLE_OPENEXR = "1" ``` I'm getting build failures - `error: undefined variable 'libsm'` My app uses `opencv-python-headless==4.8.0.74` with ultralytics for YOLO inference

a year ago

I believe Nixpacks' nixPkgs field is case-sensitive.

Can you please try changing libsm to libSM?


samgordon

I believe Nixpacks' `nixPkgs` field is case-sensitive. Can you please try changing `libsm` to `libSM`?

spiral25
PROOP

a year ago

Thank you for the hint! This helped to progress one more step. After a few more iterations with chats, I ended up having this

# nixpacks.toml

providers = ["python"]

[variables]

PYTHONUNBUFFERED = "1"

[build]

nixpkgsArchive = "https://github.com/NixOS/nixpkgs/archive/refs/tags/23.05.tar.gz"

[phases.setup]

nixPkgs = ["python312", "gcc", "libGL", "glib", "glibc", "libGLU", "pkg-config"]

[phases.install]

cmds = [

"python -m venv --copies /opt/venv",

". /opt/venv/bin/activate && pip install -r requirements.txt"

]

[phases.build]

cmds = [

"chmod +x /app/boot.sh",

"echo 'Linking OpenCV dependencies...'",

"GL=$(find /nix/store -name 'libGL.so.1' | head -n 1) && ln -sf $GL /usr/lib/libGL.so.1 || echo 'libGL link failed'",

"GLIB=$(find /nix/store -name 'libglib-2.0.so.0' | head -n 1) && ln -sf $GLIB /usr/lib/libglib-2.0.so.0 || echo 'glib link failed'",

"GTHREAD=$(find /nix/store -name 'libgthread-2.0.so.0' | head -n 1) && ln -sf $GTHREAD /usr/lib/libgthread-2.0.so.0 || echo 'gthread link failed'",

"ldd /opt/venv/lib/python3.12/site-packages/cv2/*.so || echo 'ldd check failed'"

]

[start]

cmd = "./boot.sh uvicorn app:app --host 0.0.0.0 --port 8000"

and also

boot.sh

#!/bin/bash

echo "[runtime] Linking missing shared libs..."

GL=$(find /nix/store -name 'libGL.so.1' | head -n 1)
GLIB=$(find /nix/store -name 'libglib-2.0.so.0' | head -n 1)
GTHREAD=$(find /nix/store -name 'libgthread-2.0.so.0' | head -n 1)

ln -sf "$GL" /usr/lib/libGL.so.1
ln -sf "$GLIB" /usr/lib/libglib-2.0.so.0
ln -sf "$GTHREAD" /usr/lib/libgthread-2.0.so.0

echo "[runtime] Running app..."
exec "$@"

Deployed successfully now.


spiral25

Thank you for the hint! This helped to progress one more step. After a few more iterations with chats, I ended up having this \# nixpacks.toml providers = \["python"\] \[variables\] PYTHONUNBUFFERED = "1" \[build\] nixpkgsArchive = "<https://github.com/NixOS/nixpkgs/archive/refs/tags/23.05.tar.gz>" \[phases.setup\] nixPkgs = \["python312", "gcc", "libGL", "glib", "glibc", "libGLU", "pkg-config"\] \[phases.install\] cmds = \[ "python -m venv --copies /opt/venv", ". /opt/venv/bin/activate && pip install -r requirements.txt" \] \[[phases.build](http://phases.build)\] cmds = \[ "chmod +x /app/[boot.sh](http://boot.sh)", "echo 'Linking OpenCV dependencies...'", "GL=$(find /nix/store -name '[libGL.so](http://libGL.so).1' | head -n 1) && ln -sf $GL /usr/lib/[libGL.so](http://libGL.so).1 || echo 'libGL link failed'", "GLIB=$(find /nix/store -name '[libglib-2.0.so](http://libglib-2.0.so).0' | head -n 1) && ln -sf $GLIB /usr/lib/[libglib-2.0.so](http://libglib-2.0.so).0 || echo 'glib link failed'", "GTHREAD=$(find /nix/store -name '[libgthread-2.0.so](http://libgthread-2.0.so).0' | head -n 1) && ln -sf $GTHREAD /usr/lib/[libgthread-2.0.so](http://libgthread-2.0.so).0 || echo 'gthread link failed'", "ldd /opt/venv/lib/python3.12/site-packages/cv2/\*.so || echo 'ldd check failed'" \] \[start\] cmd = "./[boot.sh](http://boot.sh) uvicorn app:app --host 0.0.0.0 --port 8000" and also [boot.sh](http://boot.sh) ```shellscript #!/bin/bash echo "[runtime] Linking missing shared libs..." GL=$(find /nix/store -name 'libGL.so.1' | head -n 1) GLIB=$(find /nix/store -name 'libglib-2.0.so.0' | head -n 1) GTHREAD=$(find /nix/store -name 'libgthread-2.0.so.0' | head -n 1) ln -sf "$GL" /usr/lib/libGL.so.1 ln -sf "$GLIB" /usr/lib/libglib-2.0.so.0 ln -sf "$GTHREAD" /usr/lib/libgthread-2.0.so.0 echo "[runtime] Running app..." exec "$@" ``` Deployed successfully now.

a year ago

Glad to hear you solved it!


Status changed to Solved chandrika 12 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...