Unable to install librealsense for Raspberry Pi4 Ubuntu Xenial

I’m trying to install librealsense on my Raspberry Pi and the imaging dockerfile looks like:

FROM balenalib/raspberrypi3-ubuntu:xenial-build

RUN sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y

RUN sudo apt-get install -y python3 \
    python3-dev \
    python3-pip \
    python3-setuptools 

RUN sudo apt-get install -y git libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev cmake libglfw3-dev build-essential
RUN git clone https://github.com/IntelRealSense/librealsense.git
RUN cd librealsense/ && ./scripts/setup_udev_rules.sh
RUN mkdir build &&\
    cd build &&\
    cmake /librealsense/ -DBUILD_PYTHON_BINDINGS=true -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=false -DCMAKE_BUILD_TYPE=Release &&\
    make all -j4 &&\
    sudo make all 

COPY librealsense/build/ /usr/src/app/

# COPY /librealsense/build/ /usr/src/app/
#switch on systemd init system in container
ENV INITSYSTEM on

WORKDIR /usr/src/app
COPY ./app/ /usr/src/app/


#Run our binary on container startup
CMD ["python3", "/usr/src/app/test_server.py"]

and test_server.py looks like:

    import sys, os
    print("TEST")
    rootdir = '/usr/src/app'
    for f in os.listdir(rootdir):
      print(f)
    print(sys.version)
    try:
      user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
    except KeyError:
    user_paths = []
    print(user_paths)
    print(sys.path)
    sys.path.append('/usr/src/app/')
    import pyrealsense2 as rs

What am I missing to make pyrealsense import happen without error due to not finding librealsense files? I am not able to find the librealsense dir after the make step in the dockerfile.

More: python - Issues with installing librealsense with Balena. Unable to locate file after make install? - Stack Overflow

Hi @ddiddi, welcome to the forums!

IHave you tried setting LD_LIBRARY_PATH=/usr/src/app as an environment variable? I’m not really familiar with librealsense but this looks like python is not finding the library files automatically. Alternatively doing make install might write the files in the paths where python will know to look for them.

I also see you are using sudo in your dockerfile. Instructions on the dockerfile are usually run as the root user so you don’t really need the sudo.

Hope this helps, let us know if you get it to work or if we can help with anything else.

1 Like

I was able to remove the sudo and set the ENV but still get a librealsense.so not found when I run the following:

RUN apt-get install -y git libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev cmake libglfw3-dev build-essential
RUN git clone https://github.com/IntelRealSense/librealsense.git
RUN cd librealsense/ && ./scripts/setup_udev_rules.sh
RUN mkdir build &&\
    cd build &&\
    cmake /librealsense/ -DBUILD_PYTHON_BINDINGS=true -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=false -DCMAKE_BUILD_TYPE=Release &&\
    make all -j4 &&\
    make all &&\
    make DESTDIR=/usr/src/lib install

ENV LD_LIBRARY_PATH="/usr/local/lib"

and the output looks like:

01.04.21 15:37:03 (-0700) balena-cam TEST HERE v17
01.04.21 15:37:03 (-0700) balena-cam older
01.04.21 15:37:03 (-0700) balena-cam server.py
01.04.21 15:37:03 (-0700) balena-cam test_server.py
01.04.21 15:37:03 (-0700) balena-cam client
01.04.21 15:37:03 (-0700) balena-cam 3eight
01.04.21 15:37:03 (-0700) balena-cam 3.5.2 (default, Jan 26 2021, 13:30:48)
01.04.21 15:37:03 (-0700) balena-cam [GCC 5.4.0 20160609]
01.04.21 15:37:03 (-0700) balena-cam
01.04.21 15:37:03 (-0700) balena-cam [’/usr/src/app’, ‘/usr/lib/python35.zip’, ‘/usr/lib/python3.5’, ‘/usr/lib/python3.5/plat-arm-linux-gnueabihf’, ‘/usr/lib/python3.5/lib-dynload’, ‘/usr/local/lib/python3.5/dist-packages’, ‘/usr/lib/python3/dist-packages’]
01.04.21 15:37:03 (-0700) balena-cam Traceback (most recent call last):
01.04.21 15:37:03 (-0700) balena-cam File “/usr/src/app/test_server.py”, line 15, in
01.04.21 15:37:03 (-0700) balena-cam import pyrealsense2 as rs
01.04.21 15:37:03 (-0700) balena-cam ImportError: librealsense2.so.2.33: cannot open shared object file: No such file or directory

Hi @ddiddi,

A few follow-up questions:

  • Are you still getting the ‘librealsense/build/python does not exist’ error during the build?

  • Do you see any librealsense .so files in your container? For instance, do you see those files in some unexpected directory?

  • You clearly have a good start on this. If you are using some existing librealsense instructions, it would be great if you could share a link.

  • Thank you for your original dockerfile and for the excerpt from the updated dockerfile. It would be helpful if you could paste in the full updated dockerfile, just for clarity.

Thanks.

Thanks for connecting @rosswesleyporter ! Here’s some answers:

Background: I’m using balena-cam as template and created a file test_server.py to test out librealsense and opencv imports to deploy an app on the Raspberry Pi.

  • Yes. The build process DOES give me a /build/python/ does not exist. In earlier docker files when I’ve tried to use ‘COPY’ or 'RUN cp -r ’ to copy those .so files into the root for my project app to import. I get a file does not exist in the build and it errors out.
  • I do see the librealsense .so files from my project in my container. They are part of the project because locally we use those .so files to import pyrealsense2 as well. My current estimate is that those pre built .so files are not getting imported because they were generated with a different python version.
  • Here’s some librealsense instructions which have worked for us in the past:

The following is my latest dockerfile code:

FROM balenalib/raspberrypi3-ubuntu:xenial-build

RUN apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y

RUN apt-get install -y python3 \
    python3-dev \
    python3-pip \
    python3-setuptools 

RUN apt-get install -y git libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev cmake libglfw3-dev build-essential
RUN git clone https://github.com/IntelRealSense/librealsense.git
RUN cd librealsense/ && ./scripts/setup_udev_rules.sh
RUN mkdir build &&\
    cd build &&\
    cmake /librealsense/ -DBUILD_PYTHON_BINDINGS=true -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=false -DCMAKE_BUILD_TYPE=Release &&\
    make -j4 &&\
    make install

ENV LD_LIBRARY_PATH="/usr/local/lib"
# RUN export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
# COPY --from=buildrs /usr/src/usr/local /usr/local
# COPY /usr/src/librs /usr/src/app
# COPY librealsense/build/ /usr/src/app/

# COPY /librealsense/build/ /usr/src/app/
#switch on systemd init system in container

ENV INITSYSTEM on

# COPY /usr/src/librs/usr/local /usr/src/app/

WORKDIR /usr/src/app
COPY ./app/ /usr/src/app/


CMD ["python3", "/usr/src/app/test_server.py"]

Hi @ddiddi,

Here are a few more thoughts. I want to preface these by saying that we have customers using RealSense cameras, but as pipex said, we are not specifically librealsense experts ourselves.

  • Your dockerfile looks good. That is, we don’t see any general dockerfile issues. I reviewed it. And a colleague also reviewed it today.

  • Thank you for the link to the Ubuntu instructions. In the same repo, there are some Python Wrapper specific instructions. Which match up nicely with your dockerfile, with the exception of the bool=true bit in -DBUILD_PYTHON_BINDINGS:bool=true .

  • The same repo has an issue referencing the specific librealsense2.so error message that you encountered. I suspect that this is not relevant, but you’ll probably know if it is or not.

  • FYI, my colleague noticed that there is a vcpkg method for building librealsense.

  • The Ubuntu instructions have a step for installing a kernel module. I’ll confirm this, but it’s very likely be necessary here as well. You probably already had this in mind. You should be able to build your own kernel module and push it to the running kernel. Here’s an example. If this turns out to be a DKMS kernel module, I can send some additional information.

I hope this helps. Please keep us posted.

Hello again @ddiddi,

Which RealSense camera model are you using?

We’re using a Intel Realsense D415.

I’m yet to try the kernel module installation but I’ll have update you accordingly.

Hi @ddiddi,

BTW, I did get confirmation from our devices team that installing a kernel module is necessary in this case. So I am glad that you are going to give it a try. Please let us know how it goes.

Hey @rosswesleyporter !

Thanks for the kernel update! Earlier today we were able to make changes to the docker file to install debian and make install librealsense work. The latest docker file looks like:

# Build image for Raspberry Pi 4 Base 64
FROM balenalib/aarch64-debian:stretch-build
LABEL io.balena.device-type="raspberrypi4-64"
RUN echo "deb http://archive.raspberrypi.org/debian stretch main ui" >>  /etc/apt/sources.list.d/raspi.list \
	&& apt-key adv --batch --keyserver ha.pool.sks-keyservers.net  --recv-key <key>

RUN apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y

RUN apt-get install -y python3 \
    python3-dev \
    python3-pip \
    python3-setuptools 

# Install and Build Librealsense (For D415 Intel RealSense Camera)
RUN apt-get install -y git libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev cmake libglfw3-dev build-essential
RUN git clone https://github.com/IntelRealSense/librealsense.git
RUN cd librealsense/ && ./scripts/setup_udev_rules.sh
RUN mkdir build &&\
    cd build &&\
    cmake /librealsense/ -DBUILD_PYTHON_BINDINGS:bool=true -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=false -DCMAKE_BUILD_TYPE=Release &&\
    make -j4 &&\
    make install

ENV LD_LIBRARY_PATH="/usr/local/lib"

ENV INITSYSTEM on

WORKDIR /usr/src/app
COPY ./app/ /usr/src/app/

CMD ["python3", "/usr/src/app/test_server.py"]

The fixing piece was adding the python librealsense library folders to sys.path in

sys.path.append('/usr/local/lib')
sys.path.append('/usr/local/lib/python3.5/pyrealsense2')
sys.path.append('/usr/local/pkgconfig')

which I updates the sys path to include the appropriate .so files for the project!

Thank you for the help!

Hello again @ddiddi,

Excellent news! That is a nice way to end the day. And thanks for including the Dockerfile.