Install Octoprint on Linux – Complete Guide!


octoprint bed visualizer monitor

This guide is meant to provide the commands I cover in my YouTube video covering installing Octoprint on Linux.

Many of the commands were provided by the Octoprint community, I’m just presenting them in an easy-to-follow format based on my video.

Assumptions

  • You already have a Linux distro installed
  • You created a pi user account that has sudo rights
  • You are logged into the pi account
  • Your using a device like a Raspberry Pi that has USB ports
  • You know the IP address of the Linux device
  • This guide covers setting up a USB camera but you can skip that section if you don’t plan on using one

Base Install

Run Updates

sudo apt update
sudo apt upgrade

Install Packages

sudo apt install python3-pip python3-dev python3-setuptools python3-venv git libyaml-dev build-essential

Setup Octoprint Folder

cd ~
mkdir OctoPrint && cd OctoPrint

Setup Virtual Environment

python3 -m venv venv
source venv/bin/activate

Install Octoprint

pip install pip --upgrade
pip install octoprint

Add pi User to Serial Ports

sudo usermod -a -G tty pi
sudo usermod -a -G dialout pi

Note: You can test the Octproint install now by starting the service “~/OctoPrint/venv/bin/octoprint serve” and connecting to HTTP://<Your IP>:5000. You should get a setup page.

Set Octoprint to Automatically Start

wget https://github.com/OctoPrint/OctoPrint/raw/master/scripts/octoprint.service && sudo mv octoprint.service /etc/systemd/system/octoprint.service
ExecStart=/home/pi/OctoPrint/venv/bin/octoprint
sudo systemctl enable octoprint.service

You can get the status with

sudo service octoprint {start|stop|restart }

HAProxy Install/Setup

HAProxy is used to serve the front end of the site on port 80 and direct the traffic to the backend ports as needed.

Install HAProxy

sudo apt install haproxy

Update HAProxy Config

sudo nano /etc/haproxy/haproxy.cfg

Add this to the bottom of the config

frontend public
        bind *:80
        use_backend webcam if { path_beg /webcam/ }
        default_backend octoprint

backend octoprint
        option forwardfor
        server octoprint1 127.0.0.1:5000

backend webcam
        http-request replace-path /webcam/(.*)   /\1
        server webcam1  127.0.0.1:8080

Install/Setup Webcam Support

Build mjpg-streamer

cd ~
sudo apt install subversion libjpeg62-turbo-dev imagemagick ffmpeg libv4l-dev cmake
git clone https://github.com/jacksonliam/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer-experimental
export LD_LIBRARY_PATH=.
make

Set mjpg-streamer to Autostart

mkdir /home/pi/scripts
nano /home/pi/scripts/webcamDaemon

Copy code below into webcamDaemon file just created

#!/bin/bash

MJPGSTREAMER_HOME=/home/pi/mjpg-streamer/mjpg-streamer-experimental
MJPGSTREAMER_INPUT_USB="input_uvc.so"
MJPGSTREAMER_INPUT_RASPICAM="input_raspicam.so"

# init configuration
camera="auto"
camera_usb_options="-r 640x480 -f 10"
camera_raspi_options="-fps 10"

if [ -e "/boot/octopi.txt" ]; then
    source "/boot/octopi.txt"
fi

# runs MJPG Streamer, using the provided input plugin + configuration
function runMjpgStreamer {
    input=$1
    pushd $MJPGSTREAMER_HOME
    echo Running ./mjpg_streamer -o "output_http.so -w ./www" -i "$input"
    LD_LIBRARY_PATH=. ./mjpg_streamer -o "output_http.so -w ./www" -i "$input"
    popd
}

# starts up the RasPiCam
function startRaspi {
    logger "Starting Raspberry Pi camera"
    runMjpgStreamer "$MJPGSTREAMER_INPUT_RASPICAM $camera_raspi_options"
}

# starts up the USB webcam
function startUsb {
    logger "Starting USB webcam"
    runMjpgStreamer "$MJPGSTREAMER_INPUT_USB $camera_usb_options"
}

# we need this to prevent the later calls to vcgencmd from blocking
# I have no idea why, but that's how it is...
vcgencmd version

# echo configuration
echo camera: $camera
echo usb options: $camera_usb_options
echo raspi options: $camera_raspi_options

# keep mjpg streamer running if some camera is attached
while true; do
    if [ -e "/dev/video0" ] && { [ "$camera" = "auto" ] || [ "$camera" = "usb" ] ; }; then
        startUsb
    elif [ "`vcgencmd get_camera`" = "supported=1 detected=1" ] && { [ "$camera" = "auto" ] || [ "$camera" = "raspi" ] ; }; then
        startRaspi
    fi

    sleep 120
done

Setup Permissions on webcam File

chmod +x /home/pi/scripts/webcamDaemon

Create webcamd Service

sudo nano /etc/systemd/system/webcamd.service

Copy code below into webcamd service file just created

[Unit]
Description=Camera streamer for OctoPrint
After=network-online.target OctoPrint.service
Wants=network-online.target

[Service]
Type=simple
User=pi
ExecStart=/home/pi/scripts/webcamDaemon

[Install]
WantedBy=multi-user.target

Enable the webcamd Service

sudo systemctl daemon-reload
sudo systemctl enable webcamd

Reboot

sudo reboot

Webcam URLs

Stream URL: /webcam/?action=stream
Snapshot URL: http://127.0.0.1:8080/?action=snapshot
Path to FFMPEG: /usr/bin/ffmpeg

Related Articles

Make sure you check out our YouTube channel, and if you would like any additional details or have any questions, please leave a comment below. If you liked this article and want to read others click here.

Rob

I'm Rob, the founder of 3dprintscape.com. I’m a Marine Corps vet with a master’s degree in Information Systems and have been working in the technology field for over a decade. I started working with 3D printers because I was fascinated by the technology and wanted a hobby that my kids and I can enjoy together.

6 thoughts on “Install Octoprint on Linux – Complete Guide!

  1. Hi Rob,
    Thanks for your tutorial.
    I’m trying to install Octoprint on an old laptop with LinuxFX 11 (Ubuntu based)
    Everything worked fine until the instuctions
    python3 -m venv venv
    source venv/bin/activate
    The “source” command returns “sh: 5: source: not found”
    Any idea what might be the cause?

    Thanks in advance for your help,

    Marc

  2. I was getting:

    ERROR opening V4L interface: Permission denied

    So, I needed to add my user to the video group to get the camera to work:

    sudo usermod -aG video pi <<– or whatever username your using

  3. for everyone who is getting the
    E: Unable to locate package libjpeg62-turbo-dev error
    try
    apt-get install libjpeg8-dev
    instead. worked for me

Leave a Reply

Recent Posts