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 John Jennings
    John Jennings on

    Really excellent guide. Thanks.

  2. Avatar for armen
    armen on

    Thanks for this guide. I’ve used it successfully a few times now.

  3. Avatar for Raymond Stewart
    Raymond Stewart on

    I am getting a string of broken dependencies. Motion cannot install. Raspian 9.11 stretch.

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Raymond,

      Raspbian Stretch is out of date. As stated in the tutorial you should be running Raspbian Buster for this tutorial to work.

      You can either upgrade from Raspbian stretch to buster, or create a new installation using the Raspbian Buster image.

      Cheers,
      Emmet

  4. Avatar for Gregor
    Gregor on

    Hi, I tried that. Everything works fine although at the beginning I faced the trouble with the dependencies… just go again to the update or try “sudo apt –fix-broken install”. However, the view is upside down. Any idea how to flip it?

    1. Avatar for Gregor
      Gregor on

      never mind, I’ve got it 🙂
      in motion.conf add “rotate 180”

  5. Avatar for Garth
    Garth on

    Thanks for the previous response, new question. I’m having issues downloading some of the dependant packages. Example is it says: Unable to locate package libmariab3. There are a few other packages it could not locate.

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Garth,

      I just tried on a fresh installation of Raspbian Buster and all the packages installed successfully.

      Try running sudo apt update again before installing the packages.

      Cheers,
      Emmet

    2. Avatar for Garth
      Garth on

      I figured it out that the mirror repository that I was downloading packages from was down so I changed the repository I was downloading from. Thanks.

    3. Avatar for Mike Sims
      Mike Sims on

      I was having the same issue even after running all of the updates, so what I did was I first purged the failed install attempt:

      *sudo dpkg -P motion*

      Then I just did a full install of whatever the most recent version, which will also make sure that all of the dependencies are installed. HOWEVER, when you do this, note that the settings in the file motion.conf will not match these instructions exactly. For example, *picture_output* is now *output_picture* … so I recommend just going through the config file and change the settings that make sense for your desired config. In my case, I just needed to stream my logitech USB cam over the local network, so I turned off *stream_localhost*, increased the streaming framerate to 50 and I changed the image size to 640×480. I’m only using this to monitor my 3D printer during print jobs.

      Here is the command to do a full clean install of the most recent version:

      *sudo apt-get install motion*

      Good Luck!

  6. Avatar for Garth
    Garth on

    Hi do you know if this will work for raspbian jessie or will I have to upgrade to buster?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Garth,

      To use the latest version of the motion server software you will need to be running Raspbian Buster.

      Cheers,
      Emmet

  7. Avatar for Jim
    Jim on

    On 11-22-2019 I used this page to set up motion on Pi3b.

    “For this tutorial, we will be installing Motion for Raspbian Stretch.
    If you’re not already on it then I highly recommend that you upgrade.”
    <<<< I loaded stretch on card.

    Step 3 shows command for Buster but not for stretch 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 " <<<< this command fails and I cannot find what will work with stretch

    I loaded SD with stretch and did update and upgrade (long time). Since I couldn't for command for stretch I used
    the buster command above. Failed. Should I just reload card with buster?

    [SNIP, PACKAGES NOT FOUND, SNIP]

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Jim,

      Sorry about that, the tutorial should of been updated to mention that we are installing Motion for Raspbian Buster isntead of Stretch.

      I have now corrected this.

      We recommend upgrading your Raspbian image to Buster then trying the tutorial again.

      Cheers,
      Emmet

  8. Avatar for Branson
    Branson on

    How to make it run script on startup? Would adding service motion start to the end of /etc/rc.local work?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Branson,

      Try running the following command.

      sudo systemctl enable motion

      This command should allow the service manager to handle it automatically for you.

      Cheers,
      Emmet

  9. Avatar for Aron Kertesz
    Aron Kertesz on

    Hi,

    I just got a new Pi4b and tried to go through the installation. It has Raspbian GNU/Linux 10 (buster) and it complains that the libmariadbclient18 cannot be found and installed.
    Now, I have an older Pi3 with Raspbian GNU/Linux 8 (jessie) and there it seems to work.
    Is this a release issue? Some incompatibility? Should I try to install Jessie on the new one as well?

    thanks,

    aron

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Aron,

      I took a quick look into this and it looks like some of the packages changed in Debian Buster.

      We have updated the package list so it should now work fine.

      The updated command is the following one.

      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

      Cheers,
      Emmet

    2. Avatar for Aron Kertesz
      Aron Kertesz on

      Thanks a lot! I will check it now.

  10. Avatar for John Tempest
    John Tempest on

    cheers Emmet I will try that

  11. Avatar for John Tempest
    John Tempest on

    I am now using raspbian Buster. Do I just alter the stretch to Buster in
    “sudo dpkg -i pi_stretch_motion_4.2.1-1_armhf.deb”

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi John,

      We have gone ahead and updated the tutorial so that it points to the new buster version of Motion.

      You should be able to run the following two commands.

      sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.2.2/pi_buster_motion_4.2.2-1_armhf.deb
      sudo dpkg -i pi_buster_motion_4.2.2-1_armhf.deb

      Cheers,
      Emmet

Leave a Reply

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