Build a Raspberry Pi Webcam Server in Minutes

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.

Raspberry Pi Webcam Server

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.

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 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. 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.

raspberry pi USB webcam in action

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.

Clips and Ribbon Cable

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.

Webcam Server Browser

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.

Leave a Reply

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

300 Comments

  1. Avatar for Jim
    Jim on

    Gus,
    I seem to have to unplug and then plug-back-in (re-plug?) the USB camera if I reboot the pi. Is this normal behavior? And if so, is there a work around?
    THANKS!
    J

  2. Avatar for Jim
    Jim on

    Oh… and by the way THANK YOU!!!!
    This is the best DYI site yet!!!!

    1. Avatar for Gus
      Gus on
      Editor

      Thanks 🙂

  3. Avatar for James
    James on

    I there a way to disable just the motion activated recording. I still want the snapshot feature but my memory is getting filled up to fast.

    1. Avatar for Gus
      Gus on
      Editor

      Looking through the settings.

      Switch this option to off will stop the program recording/saving video.

      ffmpeg_output_movies on

      Switching this on will snap pictures whenever motion is detected.

      output_pictures on

      Following will take a picture regardless of motion being detected.

      snapshot_interval 0

      Hope that helps, the page below might also help you. It contains all the options with descriptions.

      https://motion-project.github.io/motion_config.html

    2. Avatar for James
      James on

      Gus,

      Switch this option to off will stop the program recording/saving video.
      ffmpeg_output_movies on

      Will the above stop the timelapse? I would like to still collect time lapse.

      Thanks again…

    3. Avatar for Gus
      Gus on
      Editor

      It shouldn’t stop the timelapse but I haven’t been able to test it.

  4. Avatar for Jim
    Jim on

    If I have set up port forwarding correctly in my router… what address should I enter when outside my network…
    Yea… I’m new at this!
    THANKS

    1. Avatar for Gus
      Gus on
      Editor

      You should enter your external IP followed by the port you have forwarded. To find your external IP just google “My IP” whilst on your home network. The address you plug into your browser should end up looking something like this xxx.xxx.xxx.xxx:port_number

    2. Avatar for Jim
      Jim on

      Thanks Gus…
      Was your choice of port 48461 arbitrary?
      Thanks!

    3. Avatar for Gus
      Gus on
      Editor

      It was arbitrary but i made sure it was above 0-1023 because these ports are reserved (Well known ports/System Ports).

  5. Avatar for Rajitha
    Rajitha on

    hey when i try “./motion -n -c motion-mmalcam.conf” it gives an error called “error while loading shared libraries: libavformat.so .53: cannot open shared object file : no such file or Directory” can any one help me please.

    1. Avatar for Gus
      Gus on
      Editor

      This is a known issue, Raspbian removed the required libraries when they updated it from Wheezy to Jessie. I haven’t been able to work out a workaround yet unfortunately.

  6. Avatar for Davide
    Davide on

    Hi, how can I record video file and stream on the website at the same time?

  7. Avatar for Ivaylo
    Ivaylo on

    Hello Gus,

    thank you for this great tutorial . As you probably already know , the new motion comes with RTSP streaming feature . Could you please explain how to make RTSP Stream using motion ? Thank you in advance.

  8. Avatar for JonDoe297
    JonDoe297 on

    Hi

    I have a Raspberry Pi 2 Model B and a normal camera module. I’m stuck at step number 9. When I type ./motion -n -c motion-mmalcam.conf , I get an error saying “./motion error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory” . I open it from mmal directory. i’m a newbie to Raspberry Pi. Please help. Thanks.

    1. Avatar for justin
      justin on

      Try
      sudo apt-get install libavformat-dev

  9. Avatar for JayTee
    JayTee on

    Hi,

    Great work. I have one question. Will the audio microphone work too or
    is it just video?

    1. Avatar for Gus
      Gus on
      Editor

      Just video.

  10. Avatar for Bill
    Bill on

    It works, but only for a few seconds. I’m using a webcam. If I “sudo service motion restart”, then it works again for a few seconds. Any ideas on how to keep the video stream going?

    1. Avatar for Gus
      Gus on
      Editor

      I am unsure what could be causing this. It’s most likely an unstable driver for the Webcam. Can you please let me know what kind webcam you’re using and Ill look into it.

    2. Avatar for Raul Munif
      Raul Munif on

      Hi Gus, This is amazing it works like a charm! Is there any way that we can set it up to save to a location as well as streaming?? E.g so we can monitor the stream while also having the video saved to a location?? If I go on holiday and set up my pi security camera, come back to being robbed, is there anyway to see the robbers. Basically a playback function

    3. Avatar for Gus
      Gus on
      Editor

      Hey Raul,

      I recommend checking out our Raspberry Pi Security Camera Network tutorial, this shows you how to install a more fully featured piece of software that from memory allows you to actually record the stream, and add things such as motion detection.

      Cheers

  11. Avatar for Tony R
    Tony R on

    I have a question, I can’t seem to find a straight answer on the web, I have my raspberry pi with a usb cam on it for a weather cam. But I also have a windows machine broadcasting a bird feeder cam. I can’t figure out / or find out how to get the weather cam to broadcast at the same time, no matter what I do with the linksys router, nothing allows that second camera out of the house…
    Thanks if you are able to help

  12. Avatar for Sergan
    Sergan on

    Hi,
    Nice project you have there. 🙂

    I need some help.
    I want my pi cam server using login and logout before take to Ip address. (without motionpie)
    can you help me. Sorry for my bad at English.

  13. Avatar for Krisztián Balázs Farkas
    Krisztián Balázs Farkas on

    Hello everyone,

    Fist of all thank you for the guide, it’s awesome.

    I had hard times to get this work. At the end what helped me was: sudo motion
    I’m not 100% sure why it’s did not work out without that, but this did the trick for me. Hope it helps someone.

    1. Avatar for Krisztián Balázs Farkas
      Krisztián Balázs Farkas on

      Furthermore, I found out with sudo services motion status that I had no write privileges to the folder where it wanted to save the files… It’s also worth to check if you have difficulties 🙂

  14. Avatar for abhijeet awasthi
    abhijeet awasthi on

    Hey Gus,

    first of all it was really helpful tutorial.But i’m having some difficulties in setting up that webcam server.It works well for a while for about 25 seconds then it is getting stopped then i have to give start command again . are there any changes which i can make in motion.conf file to fix this up.
    thanks,hoping for reply

  15. Avatar for Quintin
    Quintin on

    Hi there! Thanks for this! Is there any way we can include audio as well? It seems a bit of a waste to have the mic on the webcam and then not use it.

  16. Avatar for Rob
    Rob on

    This was exactly what I needed! Thank you for taking the time to put these instructions together!!

  17. Avatar for Pramod
    Pramod on

    Thanks for the steps. Very helpful.I wanted to clarify what you mean by outside network? It means form anywhere in the world?

  18. Avatar for Farid
    Farid on

    hi guys i got some problem,
    it didnt work on my raspberry pi 3,i used logitech webcam and i have do sudo service motion start.and when i go to my browser and typ ip:8081 it refuse to connect 🙁

  19. Avatar for Parker Hemming
    Parker Hemming on

    Everything seemed to be working fine, but when I opened it up in the browser (chrome) by using ***.***.*.**:8081 it said unable to open video device. Before it also said in the browser that there was an error capturing the first image… I’m not sure what to do. Please Help Gus!
    I’m stuck at this point

  20. Avatar for Ryan
    Ryan on

    This is great, however how could I place this live feed on a html page? What additional code if any would be needed?