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 Giovanni Filippi
    Giovanni Filippi on

    Hi,

    i have used a webcam tutorial, but when i read motion.conf i don’t find the line : webcam_localhost off
    I don’t find any line with webcam, any idea?

    Thank’s
    Giovanni

  2. Avatar for Brendan
    Brendan on

    Thanks for the tutorial! All goes well until the motion.conf step. I look for “webcam_localhost” And I can’t find it anywhere in the file, I don’t know if this will majorly effect the Pi, as when I continue with the other steps, I finish with no problem, but when I type the Pi’s IP and port 8081 into my browser, it doesn’t load. Tried on Safari and Chrome and the Pi just doesn’t load, I know it is on the network as I access it through SSH, any ideas?

  3. Avatar for Kepler
    Kepler on

    Hey, great tutorial! I’m a little nervous about opening up my raspberry to outside connections. Are there any security concerns from forwarding the 48461 port to the pi?

  4. Avatar for Stefan
    Stefan on

    Hi

    Using a PI3 with Jessie
    produces the following error:
    ./motion: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory

  5. Avatar for darshan
    darshan on

    how much speed internet connection does it need

  6. Avatar for Aaron
    Aaron on

    Hi

    I just tried your guide and i am stuck at the test the motion:

    pi@raspberrypi_Bplus:~/mmal $ ./motion -n -c motion-mmalcam.conf

    when i enter the command i get this error:

    ./motion: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory

    Where or what directory is it looking for? Doesn’t the first part of the command mean to goo up one level in the directory tree and enter the directory “motion”?

    what is the absolute path to that directory?

    Thanks
    Aaron

  7. Avatar for Hezarfen
    Hezarfen on

    I was following your steps until step number 8, then it seems so hard to change the file with theses settings because it doesn’t seem to match. Suggestions anybody?

  8. Avatar for beka beridze
    beka beridze on

    hi, it works but has a bug. when i reboot pi the stream starts to work for a few seconds but then i crashes. please tell me what to do 🙁

  9. Avatar for amin
    amin on

    when i run ./motion -n -c motion-mmalcam.conf
    i get
    /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

  10. Avatar for Ian
    Ian on

    H,

    Thankyou for the tutorial. It is easy to follow. However, I have run into a problem. I am using a pi 2B and a pi a+ with the latest Jessie. I have seen here a few comments ie., in the motion file the daemon is already set to “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.
    My webcam works fine with fswebcam taking stills so I am confident that it is compatible.
    If I enter “sudo motion start” instead then it does come up with “Motion 3.2.12+git20140228 Started” but still no streaming in the browser – either IE11 or Firefox or Chrome!
    Is this an issue to do with jessie? Please can someone help?

  11. Avatar for Danny Martin
    Danny Martin on

    Hello Gus. I am trying unsuccessfully to install and run motion on a Raspberry Pi Zero. Any suggestions on how to make this work? I will be using a USB webcam.
    Thanks

    1. Avatar for Gus
      Gus on
      Editor

      Haven’t been able to get my hands on a Raspberry Pi zero yet but I’ll take a look when I finally do. Unsure why it wouldn’t work, looking at the GIT page this seems to be a common issue.

  12. Avatar for Mia
    Mia on

    Hi, thanks for sharing. My Lifecam HD-3000 works perfectly.
    How do we record the motion? maybe for few mins.

  13. Avatar for Matt
    Matt on

    I followed this tutorial to set up my streaming webcam. One issue I ran into is that motion would crash almost immediately when something moved. I looked in /var/log/syslog and saw that the issue was that motion didn’t have permission to write in /var/lib/motion. I resolved the issue by running the following command:

    sudo chmod -R 777 /var/lib/motion

    This is just a FYI in case anybody runs into the same issue in the future.

    1. Avatar for James
      James on

      Matt,

      That worked on my setup! I now have a steady stream. Thanks for sharing that!

  14. Avatar for Stefan
    Stefan on

    Hi,

    I’ve tried to run this webcam server with two different USB cameras, and I get the same result: it’s only possible to run the camera and view it in a browser more than 7 sec (approx). then I have to open the ‘pagea again.

    And after a couple of minutes I have to restart motion to be able to watch it again.

    My cameras are a Logitech QuickCam Pro 9000 and a PlayStation 3-camera. I’ve tried to connect them directly to an USB-port on my Pi, and to a port on a USB-hub with external power.

    Any ideas on what is wrong? Any setting in the motion software?

    Thnx in advance for your support.

  15. Avatar for Rajap
    Rajap on

    Hi, I tried doing this, but I don’t know what URL to put in the browser. I am trying to access it remotely from another network.

  16. Avatar for Ulrich Engel
    Ulrich Engel on

    Hi,
    I followed your tutorial to install motion for the Pi Camera. At the end I started “startmotion” file and opened my firefox browser with the local IP for the raspi.
    A new window opened with a video-window 320×240 (width and high of the conf) but no video. Error: Unable to open video device.
    In the raspi-config I had enabled the camera.

  17. Avatar for Stefan
    Stefan on

    Hi,

    I’ve just tested the gude for setting up an webcam server using an USB-cam (Logitech Pro 9000).

    It starts up fine, but each session/video stream won’t last more than 7 secs, then I have to reload my browser for another 7 secs. This is possible to repeat a couple of minutes the I have to restart motion on my RasPi to be able to see the next 7 sec video stream.

    Any idea if it depends on any setting?

    Thanks in advance,

    Sponkenberg

  18. Avatar for James
    James on

    Hey Gus,

    Loved this tutorial made it really simple to get setup and started. I am having only one issue. I’ve looked through all of the comments and did not see anyone else having this issue.
    The issue I am having is when i get the stream going and I have to manually focus my camera, when my hand goes across the screen it kills the live stream. I am using the RPi 2 with an older usb camera.

    1. Avatar for Ryan
      Ryan on

      Hey James, I get the same issue as you. Originally I thought it was related to bandwidth or processing power. But after extensive experimenting with lots of different Low res/low framerate setting I don’t think this is the case. From what I’ve read on other forums, I think it potentially depends on what sort of usb cam you are using. Some need drivers, some dont- I think. If I had another on on hand I’d check it. What sort do you have?

    2. Avatar for James
      James on

      Ryan,

      I don’t know what kind I have, all I know is that it was a really cheap off brand camera. I’m going to be purchasing the camera that attaches to the board and give that a shot probably will work better. I will post here again and let you know my success.

    3. Avatar for Douwe
      Douwe on

      Hey guys!

      Did you found a solution to this problem yet?
      I have the same issue, as soon as the camera get moved or someone walks across it, the stream goes down.

      Let me know

  19. Avatar for david
    david on

    hi there
    is it possible to have the video stored online at all on my own server?

    Dave

    1. Avatar for Mark
      Mark on

      Hi David,
      I am Mark (see Jan 15, 2016 above). Not sure what versions you are using but I was not able to get Motion to work with Raspberry Pi 2 and the Raspberry Camera (B) 2.0. Gus has a great tutorial but I think as he mentioned above, some software sections were dropped out since the rev he wrote the tutorial on. I have seen similar problems with other tutorials. Check out my Raspberry Post here to find out what works with the latest revisions as of this writing… https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=134141&p=893318#p893318 This is a recipe of tutorials to allow you to stream live video from anywhere in the world, even modify the software from anywhere in the world. Since it is YouTube live, I think it records the last 4 hours of video that you can do what you want with it.

Leave a Reply

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