Build a Raspberry Pi Webcam Server in Minutes

This Raspberry Pi webcam server tutorial will take you through on how to have your very own Webcam that is visible on a web page.

Raspberry Pi Webcam Server

If you’re after more of a security like system, then 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 working especially if you have hardware that differs to the versions used in this tutorial. If you find that you can’t get this to work, then 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

Optional

Video on Setting up the Webcam

If you’re more of a visual person and would prefer to watch how to set this up, then you check out my video below. If you like the video, then please subscribe to me, so you’re kept up to date with all the latest videos.

Unfortunately, the video is now out of date and uses an older method then what’s explained in the text version of this tutorial. I highly recommend that you follow the written tutorial, while I work to update the video.

How to Setup a Raspberry Pi Webcam Server

Firstly, we will need to install Raspbian onto the Raspberry Pi. If you haven’t already done, this then check out my awesome guide on installing NOOBs Raspberry Pi. It will take you through all the steps that you will need to do to get up and running.

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.

Since the latest version of Raspbian is missing dependencies that Motion requires for it to work correctly, we will need to install a different precompiled version.

The package maintainer (Currently: Mr Dave | Previously: Calin Crisan) of Motioneye provides a package that has everything we need, and it works just fine with the Raspberry Pi.

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, so you’re running on the latest version.

sudo apt update
sudo apt upgrade

Depending on the version of Raspbian you’re using you will need to do some different steps. For this tutorial, we will be installing Motion for Raspbian Buster. If you’re not already on it then I highly recommend that you upgrade.

3. First, install the following packages. This command will work both on the full and lite version of Raspbian Buster.

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

4. Next, download the Motion deb file for Raspbian Buster from GitHub using the wget command and install it using the dpkg command.

sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.5.1/$(lsb_release -cs)_motion_4.5.1-1_$(dpkg --print-architecture
).deb

sudo dpkg -i $(lsb_release -cs)_motion_4.5.1-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 Pi.

Configuring Motion

1. Now we need to make some edits to the configuration file, motion.conf.

sudo nano /etc/motion/motion.conf

2. Find the following lines and ensure that they are set to the following values.

  • daemon off
  • stream_localhost off

Note: Change the following two lines from on to off if you’re having 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 & resources. Needs to be added to the config file, default is 1
  • framerate 100 Changing this option will allow for 100 frames to be captured per second allowing for smoother video, default is 50
  • width 640 #This line changes the width of the image displayed, default is 640
  • height 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. Now, we need to enable the Motion service by running the command below.

Using this command will make Motion start up when your Raspberry Pi powers on.

sudo systemctl enable motion

4. Once you’re done, simply save and exit by pressing CTRL + X then Y.

5. Now make sure the camera is connected and run the following line:

sudo systemctl start motion

6. If you need to stop the service, simply run the following command:

sudo systemctl stop motion

7. Now you should be able to check out the Webcam Stream at the IP address of our Pi so in your browser go to the following address.

192.168.1.103:8081

8. If the web page isn’t loading, try restarting the service.

sudo systemctl restart motion

9. If you’re using a Raspberry Pi camera, then you will need to do a few extra steps that are mentioned below.

There we have it a fully working Raspberry Pi Webcam server that you can place wherever you would like (Given it is within WiFi range) and be able to view the stream via the web browser.

If you want to allow external access to the camera, then check out my instructions towards the bottom of this tutorial.

raspberry pi USB webcam in action

Extra Steps for the Raspberry Pi Camera

If you want to use the Raspberry Pi camera module, then you will need to do a few additional steps to set it up.

Installing the Hardware

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, now insert the ribbon cable with the metal leads facing away from the Ethernet Port.

2. Once you have lined it up gently press the connector back down and the cable should now be locked in one place.

Clips and Ribbon Cable

Configuring the Software

To get the Raspberry Pi camera to work with Motion, we will need to do a few extra steps.

1. First make sure the camera is switched on within raspi config, enter the following command and then enable the camera (Found in interfacing options). You will need to restart once you have done this.

sudo raspi-config

2. Enter the following line to had the camera to modules. Make sure the camera is connected correctly before running this line otherwise it will thrown an error.

sudo modprobe bcm2835-v4l2

3. Alternatively, the following process should also work. First, open up the modules file by entering the following line.

sudo nano /etc/modules

4. Enter the following line at the bottom of the file if it doesn’t already exist.

bcm2835-v4l2

Once done, save and exit by pressing CTRL + X then Y.

5. Now reboot the Pi, and the stream should now work.

sudo reboot

6. 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.103:8081

You should now have a fully working Raspberry Pi camera server that is accessible within your local network. If you want to allow external access to the camera, then please follow my instructions below.

Webcam Server Browser

Saving Motion Footage and Photos

By default, you will likely run into permission problems for saving the photos and video triggered by motion. I will 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 now 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.

Make sure you remove the ; from the start of the line.

target_dir /motion

5. Next, restart the motion service, so the configuration is loaded correctly.

sudo systemctl restart motion

6. Now, whenever the camera detects motion, it should be saved into the folder we specified above. You can check this by running the following command.

ls /motion

Be sure to check out our Linux permissions guide for more information on setting permissions correctly.

Setting Up External Access

In order 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 brand of router.

Please note, opening ports to the internet comes with a security risk.

If you need a more in-depth guide, then be sure to take a look at my guide on how to set up Raspberry Pi port forwarding and dynamic DNS.

The following steps are what I did on mine in order to get it to work. My router is an AC1750 TP-Link Router.

1. Go to the router admin page (This will typically be 192.168.1.1 or 192.168.254)

2. Enter the username and password. Default typically is admin & admin.

3. Once in go to forwarding->Virtual Server and then click on add new.

4. In here enter:

  • Service port: In this case 48461
  • IP Address: 192.168.1.103 (Address of your Pi)
  • Internal Port: We want this to be the same as the webcam server so make it 8081
  • Protocol: All
  • Status: Enabled

5. These settings will route all traffic destined for port 48461 to the webcam server located at the IP address and port you provided. For example: 192.168.1.103:8081

Router Port Forwarding

6. You should now be able to connect to the Raspberry Pi webcam stream outside your network. You may need to restart the router for changes to take effect.

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 something called dynamic DNS to counter this you can find out more information via the link mentioned above.
  • Restart the router.

I hope that you have enjoyed this tutorial on how to build a Raspberry Pi webcam server if you want more then, please check out all my Raspberry Pi projects I currently have completed. There will be much more coming in the future.

300 Comments

  1. Avatar for Mark
    Mark on

    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.

  2. Avatar for hazwer
    hazwer on

    I am stuck at the 2nd step, there is no “webcam_localhost” in my motion.conf file. Please help me.

  3. Avatar for Ryan
    Ryan on

    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

  4. Avatar for Ryan
    Ryan on

    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?

  5. Avatar for Konrad
    Konrad on

    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.

  6. Avatar for George
    George on

    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!

  7. Avatar for Akshay
    Akshay on

    hey can we give some indication on detection of motion …. like Turn on led or do some other embedded operation

  8. Avatar for David Moisson
    David Moisson on

    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.

  9. Avatar for Tarek
    Tarek on

    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 ?

  10. Avatar for Charlie Candergart
    Charlie Candergart on

    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.

    1. Avatar for Gus
      Gus on
      Editor

      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.

  11. Avatar for Matthew Hoyt
    Matthew Hoyt on

    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!

  12. Avatar for zero
    zero on

    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.

  13. Avatar for Martin
    Martin on

    Great post. Worked first shot. Thanks a lot

  14. Avatar for Alex
    Alex on

    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?

  15. Avatar for John
    John on

    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

  16. Avatar for Ben
    Ben on

    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.

  17. Avatar for Andreas
    Andreas on

    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

    1. Avatar for Isaac
      Isaac on

      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.

    2. Avatar for Gus
      Gus on
      Editor

      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.

    3. Avatar for Isaac
      Isaac on

      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

  18. Avatar for Jerry
    Jerry on

    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.

  19. Avatar for Scott
    Scott on

    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.

  20. Avatar for enas
    enas on

    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

Leave a Reply

Your email address will not be published. Required fields are marked *