This tutorial will take you through how to set up a webcam server on the Raspberry Pi. You can have your very own Webcam visible on a web page.
In this tutorial, we will be using a package called Motion. If you want to learn more about it, you can check out their website at Motion. This software will allow us to stream footage from a webcam connected to the Raspberry Pi. You will be able to view the stream via a web page in your favorite browser.
If you’re after more of a security-like system, check out the Raspberry Pi security camera tutorial as it features fully-fledged web streaming, motion detection, recording, and multi-camera functionality.
This tutorial is marked intermediate as it can be challenging to get it working, especially if you have hardware that differs to the versions used in this tutorial. If you can’t get this to work, the security camera solution linked above is much easier to get going.
Equipment
Below are some of the pieces of equipment that you will need to be able to complete this tutorial.
Recommended
- Raspberry Pi ( Amazon )
- Micro SD Card ( Amazon )
- Ethernet Cable ( Amazon ) or Wi-Fi ( Amazon )
- Raspberry Pi Camera ( Amazon ) or USB Webcam ( Amazon )
Optional
Setting up the Webcam Server on the Raspberry Pi
Firstly, we will need to install Raspberry Pi OS onto the Raspberry Pi. If you haven’t already done this, check out our guide on installing Raspberry Pi OS. It will take you through all the steps that you will need to do to get up and running.
1. We will be using the terminal, so open the terminal on the Pi or connect to it via SSH.
2. To begin, first, update the Raspberry Pi OS, so you’re running on the latest packages.
sudo apt update
sudo apt upgrade
3. Next, install the following packages. This command will work on the full and lite versions of Raspberry Pi OS.
sudo apt install autoconf automake build-essential pkgconf libtool git libzip-dev libjpeg-dev gettext libmicrohttpd-dev libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev default-libmysqlclient-dev libpq-dev libsqlite3-dev libwebp-dev libcamera-v4l2
4. Next, download the Motion deb file for Raspberry Pi OS from GitHub using the wget command and install it using the dpkg command. You can view all the releases for Motion over at their Github.
Run the two lines below to download and install Motion.
sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.6.0/$(lsb_release -cs)_motion_4.6.0-1_$(dpkg --print-architecture).deb
sudo dpkg -i $(lsb_release -cs)_motion_4.6.0-1_$(dpkg --print-architecture).deb
That’s all you need to do before moving on to configuring Motion so that it will run on your Raspberry Pi.
Configuring Motion
1. We need to edit the configuration file, motion.conf. To do this, open the file in the nano text editor.
sudo nano /etc/motion/motion.conf
2. Find the following lines and ensure they are set to the values below.
daemon off
stream_localhost off
Note: Change the following two lines from on to off if you have issues with the stream freezing whenever motion occurs.
picture_output off
movie_output off
Optional (Don’t include the text after the #)
stream_maxrate 100
# This change will allow for real-time streaming but requires more bandwidth and resources. It needs to be added to the config file. Default is 1framerate 100
# Changing this option will allow for 100 frames to be captured per second, allowing for smoother video. Default is 50width 640
# This line changes the width of the image displayed. Default is 640height 480
# This option changes the height of the image displayed. Default is 480
Altering these options can drastically affect the performance. You may need to tinker to get the best results.
3. Once done, save and exit by pressing CTRL + X then Y.
4. We need to enable the Motion service by running the command below.
This command will make Motion start when your Raspberry Pi powers on.
sudo systemctl enable motion
5. Ensure the camera is connected and run the following line.
sudo systemctl start motion
If you need to stop the service, run the following command.
sudo systemctl stop motion
6. You should be able to check out the webcam stream at the IP address of the Raspberry Pi. To get the IP address, use the hostname command.
hostname -I
7. In your browser, go to the IP address of your Raspberry Pi, followed by the port 8081.
192.168.1.121:8081
If you are using a Raspberry Pi camera, you will need to do a few extra steps, mentioned further down the page. You will see a grey box with an error if you do not do these steps.
8. If the web page isn’t loading, try restarting the service.
sudo systemctl restart motion
9. You should now have a fully working Raspberry Pi Webcam server that you can place wherever you would like (Given it is within Wi-Fi range) and be able to view the stream via the web browser.
If you want to allow external access to the camera, check out my instructions at the bottom of this tutorial.
Additional Steps for Using a Raspberry Pi Camera
If you want to use the Raspberry Pi camera module, you will need to do a few additional steps to set it up.
A Raspberry Pi 5 uses a different port, so you may need to acquire a ribbon cable (Amazon) designed to fit into the new ports.
1. First, go to the CSI (Camera Serial Interface) ribbon cable slot on the Pi and with two fingers on each side of the connector, pull up. This technique should now open the connector. This port is labeled CAM/DISP 0 on the Raspberry Pi 5.
2. Insert the ribbon cable with the metal leads facing away from the Ethernet Port. If you are using a Raspberry Pi 5, the metal leads must face toward the ethernet port.
3. Once you have lined it up, gently press the connector back down, and the cable will lock in place.
4. Since the Raspberry Pi camera relies on a library called libcamera we will need to edit the service file. To do this, enter the following line.
sudo nano /lib/systemd/system/motion.service
5. In this file, find the line that contains the following text.
ExecStart=/usr/bin/motion
Update the line to the following text.
ExecStart=/usr/bin/libcamerify /usr/bin/motion
Once done, exit the file by pressing CRTL + X and then Y.
6. Since we just changed the source service file, we will need to reload the systemctl daemon. You can do this by running the following command.
sudo systemctl daemon-reload
7. Lastly, restart the motion service to ensure our changes have taken effect.
sudo systemctl restart motion
8. You should now be able to access the Raspberry Pi webcam stream by going to the Pi’s IP address on port 8081.
192.168.1.121:8081
You should now have a fully working Raspberry Pi camera server accessible within your local network. If you want to allow external access to the camera, please follow my instructions below.
Saving Motion Footage and Photos
By default, you will likely have permission problems when saving the photos and video triggered by motion. I go through the steps below on how to set this up to work correctly.
1. Firstly, let’s make a new directory for our photos and videos.
sudo mkdir /motion
2. Next, we need to change the group of the motion folder so motion can write into it. After that, we give the group; read, write, and execute permissions using the chmod command.
sudo chgrp motion /motion
sudo chmod g+rwx /motion
3. We need to go back into the motion configuration file and change the target_dir
to our new directory. First, open up the file with the nano text editor.
sudo nano /etc/motion/motion.conf
4. Update the target_dir line so it looks like the one below. Ensure you remove the ;
from the start of the line.
target_dir /motion
5. Next, restart the motion service to load the configuration correctly.
sudo systemctl restart motion
6. When the camera detects motion, it will be saved into the specified folder. You can check this by running the following command.
ls /motion
Check out our Linux permissions guide for more information on setting permissions correctly.
Setting Up External Access
To enable external access to the Raspberry Pi webcam server, we will need to change some settings on the router. However, all routers are designed differently, so you may need to look up instructions for your router brand.
Please note that opening ports to the internet comes with a security risk. Please use strong security measures to prevent unauthorized access to your local network.
If you need a more in-depth guide, be sure to look at my guide on how to set up Raspberry Pi port forwarding and dynamic DNS.
If you’re unable to connect outside your local network, then you can try the following.
- Check your router settings and confirm they are correct.
- Check your IP hasn’t changed. Some internet service providers will provide you with a dynamic IP rather than a static IP. You can set up dynamic DNS to counter this.
- Ensure that the port mapping matches correctly with the motion port. For example, the internal port must be set to 8081.
- Restart the router.
Conclusion
I hope you enjoyed this tutorial on how to build a Raspberry Pi webcam server. You should now be able to view your camera stream using a web browser.
If you want more projects, please check out all our Raspberry Pi projects. We are always working on more, so be sure to check back in the future or stay up to date by signing up to our newsletter.
Please feel free to use the comment section below to provide feedback for this tutorial.
I really like what you did here but can’t seem to get it to work. I have spent hours reinstalling my RP2 and Pi Camera 4 times from scratch and following your video and written instructions. When I hit
pi@raspberrypi:~/mmal $ ."/ sudo motion -n -c motion-mmalcam.conf"
(without quotes).I get the following error:
"./motion: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory"
. I am a newbie to the pi but have tried googling the error for a solution and nothing comes up that works.I tried ldconfig -v, and a few other things. Please help as I would really like to make this work. As a newbie, please provide instructions accordingly.
Like if you say compile this or go to this library file, etc. I might need to know how to do those commands. Prior to one of my reformat exercises, I was able to view the camera through vlp before I found your site so I know camera works.
I am stuck at the 2nd step, there is no “webcam_localhost” in my motion.conf file. Please help me.
Hi,
This tutorial works great, have you been able to view the mjeg stream on an ios device?
Ultimately I’d like to be able to check my webcam when away from home, which would be on my phone
This is great. Easiest pi tutorial I have seen.
I works fine when viewing the steam from my laptop browser, but when i try and view it from my iphone it is constantly reloading the page and can’t keep up. I get one frame at the start and then nothing afterwards.
Any ideas?
Hi to all,
On my raspberry pi 2 B with raspberrian “Jessie”.
As some people have mentioned here: in the motion.conf file the daemon is originally “on” and there is no webcam_localhost configuration any more…
When I start the motion with “sudo service motion start” there is no [OK] that appears (as in the video) and the motion stream is not found by my browser, even localy on the board. There is even no error.
Excellent tutorial. I set my webcam server up and running! Problem is that I can’t find a way to password protect my stream. I have port forwarded the stream and it’s viewable from outside. An help would be greatly appreciated!
hey can we give some indication on detection of motion …. like Turn on led or do some other embedded operation
I wasn’t able to get the raspberry pi camera to work on my model B+ until I used the following command (after completing the install) : sudo modprobe bcm2835-v4l2
Now the camera works fine with motion.
Gus, amazing tutorial ! I got to the point where i see the grey box in the browser , with a message saying “unable to open video device”
My camera is waveshare.
any idea what might be going wrong here ?
Hi.
I am very grateful for guides like these so a big thank you.
But I have a slight problem.
I am running into a libavformat.so.53 library missing.
I am using the noobs raspbian with the noir camera.
I followed the guide for this camera on this page, twice, reinstalling from scratch in between.
Not running as root or such, I mean literally following your guide.
But I end up with this error:
pi@raspberrypi:~/mmal $ ./motion -n -c motion-mmalcam.conf
./motion: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory
Could you advice on how to fix this?
An ldd motion shows this:
pi@raspberrypi:~/mmal $ ldd motion
linux-vdso.so.1 (0x7ec2a000)
/usr/lib/arm-linux-gnueabihf/libarmmem.so (0x76eff000)
libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0x76ec5000)
libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0x76e4a000)
libjpeg.so.62 => /usr/lib/arm-linux-gnueabihf/libjpeg.so.62 (0x76df5000)
libavformat.so.53 => not found
libavcodec.so.53 => not found
libavutil.so.51 => not found
libz.so.1 => /lib/arm-linux-gnueabihf/libz.so.1 (0x76dcd000)
libmysqlclient.so.18 => /usr/lib/arm-linux-gnueabihf/libmysqlclient.so.18 (0x76abf000)
libpq.so.5 => not found
librt.so.1 => /lib/arm-linux-gnueabihf/librt.so.1 (0x76aa8000)
libbcm_host.so => /opt/vc/lib/libbcm_host.so (0x76a89000)
libvcos.so => /opt/vc/lib/libvcos.so (0x76a77000)
libvchiq_arm.so => /opt/vc/lib/libvchiq_arm.so (0x76a69000)
libmmal_core.so => /opt/vc/lib/libmmal_core.so (0x76a52000)
libmmal_util.so => /opt/vc/lib/libmmal_util.so (0x76a3a000)
libmmal_vc_client.so => /opt/vc/lib/libmmal_vc_client.so (0x76a2e000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x768f1000)
/lib/ld-linux-armhf.so.3 (0x54ab3000)
libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0x768de000)
libstdc++.so.6 => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0x76802000)
libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0x767d5000)
libvcsm.so => /opt/vc/lib/libvcsm.so (0x767c8000)
and I understand that these libraries are missing but … well.
I tried fixing that manually at the first attempt and that did not work so good.
Another thing that might be related is that “
sudo apt-get install libjpeg62
” results in “Note, selecting ‘libjpeg62-turbo
‘ instead of ‘libjpeg62
‘”libjpeg62-turbo is already the newest version.
libjpeg62-turbo set to manually installed.
Any advice would be greatly appreciated.
Hi Charlie,
I have looked into this problem and haven’t been able to work out a viable workaround. It occurs on any Raspberry Pi 2 or/and if you’re running Raspbian Jessie.
I am looking at redoing this part of the tutorial using either Python or a different package to achieve the same result.
Love the tutorial. Get to take pics, but can’t find the lines in motion.conf to edit. “daemon on
webcam_localhost off”. Also I can’t get the page to load ” page is not available”. I did open the port. Any help would be great!
I have also got the same issue as few other above, did you find a fix yet ?
./motion -n -c motion-mmalcam.conf
./motion: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory
Love the tutorial’s some very nice work.
Great post. Worked first shot. Thanks a lot
Hi Guys, I got it to work, but it only works for so long and then I have to type “sudo service motion restart” it will then work for another minute or so? Any ideas? (Talking about the first part of just viewing the cam)
Also, its quite blury with movement, will that be the limitation of the pi or webcam?
Hello Gus, thank you for your detailed projects. I have had success with two so far, this one and the Security camera project. I am a complete beginner with the Raspberry Pi and your projects are helping me immensely. I have a Raspberry PI 2 and I started by building this one (Web Cam server) about a month ago. I was able to follow your instructions and it worked first time using a USB Web Cam. Following that success I then built the Pi Security camera network which again worked fine.
I then decided to rebuild the Web Cam server so to do this I again reformatted the SD and downloaded NOOBS and reinstalled Raspbian. I upgraded it and then installed Motion when I came to make the changes to motion.conf I could not find the webcam_localhost, the webcam_maxrate or the framerate. After I had completed making other changes to the height and width I saved the file and ran it. The Webcam worked but as soon as it detected some movement it switched off and stayed off.
This puzzled me for a while and then realised that I was using Raspbian Jessie and assumed that this was using a newer version of Motion. I reformatted the SD and downloaded Raspbian Wheezy and I was then able to build the server following your original instructions. As I said at the beginning I am new to this and I may be jumping to the wrong conclusion, have you come across this problem before? What changes will I need to do in order to build it using Rasbian Jessie? John
Hello Gus, I followed the instructions but I get the following text when I try to access it on my link:8081 and get “Motion 3.2.12+git20140228 Running [1] Threads All” with no video. Driving me crazy and can’t figure out what’s the problem.
Hi guys, when i have gave it now several trys, but everytime i try to execute command “./motion -n -c motion-mmalcam.conf” to test the Settings, step 9, Server with the Raspi-cam, i will get fault:
./motion: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or Directory
does anybody know what i do wrong?
Thanks in advance
I’m stuck at the same step. Lib53 missing. I have looked everywhere. As far as I can tell there is no fixing it. This is an old guide anyways. I looked around and found a newer guide which uses python and openvc. I kind of got stuck following that guide but I should be able to get help with it since it was published rather recently. Look it up if you want.
Hi Andreas,
In the latest version of Raspbian (Jessie) they have removed the package that motion-mmalcam relies on from their repositories (ffmpeg) and cannot be installed automatically. Without this package you will get errors like the one above. I am looking into a solution around this.
Not sure what happened but apparently the Lib53 file can no longer be installed. I tried it on b+ and pi2. I ended up using a different set up which uses openvc and python. The guide is quite recent and the author is very helpful. Good luck
Very useful tutorial, thank you. I have connected a USB webcam to the Pi and can view the stream through the Pi web browser no problem. I have not tried any other computer on the LAN. However, I cannot access it over the internet, either from work or via 4G mobile phone. I have set up port forwarding on my router for ports 8080 and 8081 to the pi. Any suggestions? It’s a Virgin Superhub 2 router if that helps.
Hello Gus,
Thank you for creating all of these helpful tutorials. I have learned a lot thanks to you.
While editing the motion.conf file, I searched for “webcam_localhost” and got a “not found” returned. When I searched for just “webcam”, there is only one instance. I am attempting to set up a Logitech USB webcam. The camera worked fine with fswebcam when taking stills.
I have searched for some other entries that are similar but I don’t want to go mucking about and change something that will break the thing.
Hi Gus,
Thanks for the walk through! I am trying to locally view the video but my browser will not display the video.Am i missing an extension?
i did every thing without errors
i’m using pi cam