Setting up OctoPrint on a Raspberry Pi running Raspbian


#1

:point_up: Heads-up

If you want to get OctoPrint up and running as fast as possible, it is highly recommended to take a look at OctoPi, which is an SD card image based on Raspbian already prepared with OctoPrint, Webcamsupport, HAProxy and SSL. Just download it, flash it to an SD card and you are good to go -- you can follow this excellent video guide by Thomas Sanladerer who explains all needed steps in detail.

If on the other hand you want to run the latest versions of Raspbian, OctoPrint and all the other packages, and get a sense of how it all fits together, do follow the instructions below (warning: not for the faint of heart).


Important: This guide expects you to have a more than basic grasp of the Linux command line. In order to follow it you'll need to know:

  • how to issue commands on the shell,
  • how to edit a text file from the command line,
  • what the difference is between your user account (e.g. pi) and the superuser account root,
  • how to SSH into your Pi (so you don't need to also attach keyboard and monitor),
  • how to use Git and
  • how to use the Internet to help you if you run into problems.

This is not a "Linux for Beginners guide", those can be found for example here and here. For some Git basics please take a look here.


Basic setup

For the basic package you'll need Python 2.7 (should be installed by default) and pip. OctoPrint's dependencies will be installed by pip:

cd ~
sudo apt update
sudo apt install python-pip python-dev python-setuptools python-virtualenv git libyaml-dev build-essential
mkdir OctoPrint && cd OctoPrint
virtualenv venv
source venv/bin/activate
pip install pip --upgrade
pip install https://get.octoprint.org/latest

You may need to add the pi user to the dialout group and tty so that the user can access the serial ports:

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

You should then be able to start the OctoPrint server:

pi@raspberrypi ~ $ ~/OctoPrint/venv/bin/octoprint serve
 * Running on http://0.0.0.0:5000/

Updating & changing release channels & rolling back

OctoPrint should offer to update itself automatically and also allow you to switch to other Release Channels out of the box.

If for whatever reason you want or need to perform any of this manually however, perform the following commands to install

  • the current version from the "Stable" release channel:

    source ~/OctoPrint/venv/bin/activate
    pip install https://get.octoprint.org/latest
    
  • the previous version from the "Stable" release channel:

    source ~/OctoPrint/venv/bin/activate
    pip install https://get.octoprint.org/last
    
  • the current version from the "Maintenance RC" release channel:

    source ~/OctoPrint/venv/bin/activate
    pip install https://get.octoprint.org/maintenance-rc
    
  • the current version from the "Devel RC" release channel:

    source ~/OctoPrint/venv/bin/activate
    pip install https://get.octoprint.org/devel-rc
    

Support restart/shutdown through OctoPrint's system menu

In Settings > Commands, configure the following commands:

  • Restart OctoPrint: sudo service octoprint restart
  • Restart system: sudo shutdown -r now
  • Shutdown system: sudo shutdown -h now

:spiral_notepad: Note

If you disabled Raspbian's default behaviour of allowing the pi user passwordless sudo for every command, you'll need to explicitly allow the pi user passwordless sudo access to the /sbin/shutdown program for the above to work. You'll have to add a sudo rule by creating a file /etc/sudoers.d/octoprint-shutdown (as root) with the following contents:

pi ALL=NOPASSWD: /sbin/shutdown

Automatic start up

Download the init script files from OctoPrint's repository, move them to their respective folders and make the init script executable:

wget https://github.com/foosel/OctoPrint/raw/master/scripts/octoprint.init && sudo mv octoprint.init /etc/init.d/octoprint
wget https://github.com/foosel/OctoPrint/raw/master/scripts/octoprint.default && sudo mv octoprint.default /etc/default/octoprint
sudo chmod +x /etc/init.d/octoprint

Adjust the paths to your octoprint binary in /etc/default/octoprint. If you set it up in a virtualenv as described above make sure your /etc/default/octoprint is modified like this:

DAEMON=/home/pi/OctoPrint/venv/bin/octoprint

Note also the removed # at the start of the line, uncommenting it and making it effective!

Then add the script to autostart using sudo update-rc.d octoprint defaults.

This will also allow you to start/stop/restart the OctoPrint daemon via

sudo service octoprint {start|stop|restart}

Make everything accessible on port 80

If you want to have nicer URLs or simply need OctoPrint to run on port 80 (http's default port) due to some network restrictions, I recommend using HAProxy as a reverse proxy instead of configuring OctoPrint to run on port 80. This has the following advantages:

  • OctoPrint does not need to run with root privileges, which it would need to to be able to bind to port 80 thanks to Linux privileged port restrictions
  • You can make mjpg-streamer accessible on port 80 as well
  • You can add authentication to OctoPrint
  • Depending on the HAProxy version you can also use SSL to access OctoPrint

Setup on Raspbian is as follows:

pi@raspberrypi ~ $ sudo apt install haproxy

I'm using the following configuration in /etc/haproxy/haproxy.cfg, for further examples take a look here:

global
        maxconn 4096
        user haproxy
        group haproxy
        daemon
        log 127.0.0.1 local0 debug

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        option http-server-close
        option forwardfor
        maxconn 2000
        timeout connect 5s
        timeout client  15min
        timeout server  15min

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

backend octoprint
        reqrep ^([^\ :]*)\ /(.*)     \1\ /\2
        option forwardfor
        server octoprint1 127.0.0.1:5000

backend webcam
        reqrep ^([^\ :]*)\ /webcam/(.*)     \1\ /\2
        server webcam1  127.0.0.1:8080

This will make OctoPrint accessible under http://<your Raspi's IP>/ and make mjpg-streamer accessible under http://<your Raspi's IP>/webcam/. You'll also need to modify /etc/default/haproxy and enable HAProxy by setting ENABLED to 1. After that you can start HAProxy by issuing the following command

sudo service haproxy start

Pointing your browser to http://<your Raspi's IP> should greet you with OctoPrint's UI. Now open the settings and switch to the webcam tab or alternatively open ~/.octoprint/config.yaml. Set the webcam's stream URL from http://<your Raspi's IP>:8080/?action=stream to /webcam/?action=stream (leave the snapshotUrl at http://127.0.0.1:8080/?action=snapshot!) and reload the page.

If everything works you can add the following lines to ~/.octoprint/config.yaml (just create it if it doesn't exist yet) to make the server bind only to the loopback interface:

server:
    host: 127.0.0.1

Restart the server. OctoPrint should still be available on port 80, including the webcam feed (if enabled).

Optional: Webcam

If you also want webcam and timelapse support, you'll need to download and compile 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

:point_up: Heads-up

The required packages depend on the underlying version of Debian! The above is what should work on the current Debian Stretch based images of Raspbian.

For Jessie use:

sudo apt install subversion libjpeg62-turbo-dev imagemagick libav-tools libv4l-dev cmake

For Wheezy or older (you should update...) use:

sudo apt install subversion libjpeg8-dev imagemagick libav-tools libv4l-dev cmake

This should hopefully run through without any compilation errors. You should then be able to start the webcam server using:

./mjpg_streamer -i "./input_uvc.so" -o "./output_http.so"

This should give the following output:

MJPG Streamer Version: svn rev:
 i: Using V4L2 device.: /dev/video0
 i: Desired Resolution: 640 x 480
 i: Frames Per Second.: 5
 i: Format............: MJPEG
[...]
 o: www-folder-path...: disabled
 o: HTTP TCP port.....: 8080
 o: username:password.: disabled
 o: commands..........: enabled

For some webcams (including the PS3 Eye) you'll need to force the YUV mode by using the following start command:

./mjpg_streamer -i "./input_uvc.so -y" -o "./output_http.so" 

Please be aware that YUV mode will put additional strain on your Raspi's CPU which will then lower its performance, possibly up to the point of causing printing issues. If your camera requires the -y parameter to function, consider replacing it with one that doesn't.

:spiral_notepad: Note

If your webcam requires switching to YUV mode in order to work at all, it is strongly recommended to instead use a webcam that natively supports MJPG. For YUV cameras mjpg_streamer will need to transcode all data from the camera to MJPG on your Raspberry Pi, which will put a lot of strain on its CPU (YUV mode at around 30-40% vs MJPG mode at around 1-2%). This MIGHT negatively influence print quality, so better get yourself a cheap MJPG compatible webcam. See this wiki page for a compatibility list and steer clear of cams that require -y to work.

:spiral_notepad: Note

If you want to use the official RaspberryPi Camera Module you need to run

./mjpg_streamer -i "./input_raspicam.so -fps 5" -o "./output_http.so" 

If you now point your browser to http://<your Raspi's IP>:8080/?action=stream, you should see a moving picture at 5fps. (If you get an error message about missing files or directories calling the output plugin with -o "./output_http.so -w ./www" should help.)

Open OctoPrint's settings dialog and under Webcam & Timelapse configured the following:

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

:point_up: Heads-up

If for whatever reason you are still using a Raspbian image based on Debian Jessie or older, "Path to FFMPEG" should instead be /usr/bin/avconv.

Restart the OctoPrint server, clear the cache on your browser and reload the OctoPrint page. You should now see the stream from the webcam in the "Control" tab, and a "Timelapse" tab with options.

If you want to be able to start and stop mjpeg-streamer from within OctoPrint, put the following in /home/pi/scripts/webcam:

#!/bin/bash
# Start / stop streamer daemon

case "$1" in
    start)
        /home/pi/scripts/webcamDaemon >/dev/null 2>&1 &
        echo "$0: started"
        ;;
    stop)
        pkill -x webcamDaemon
        pkill -x mjpg_streamer
        echo "$0: stopped"
        ;;
    *)
        echo "Usage: $0 {start|stop}" >&2
        ;;
esac

Put this in /home/pi/scripts/webcamDaemon:

#!/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

Make sure both files are executable:

chmod +x /home/pi/scripts/webcam
chmod +x /home/pi/scripts/webcamDaemon

If you want different camera options put them in /boot/octopi.txt or modify the script accordingly.

If you want autostart of the webcam you need to add the following line to /etc/rc.local (Just make sure to put it above the line that reads exit 0).

/home/pi/scripts/webcam start

If you want to be able to start and stop the webcam server through OctoPrint's system menu, add the following to config.yaml:

system:
  actions:
   - action: streamon
     command: /home/pi/scripts/webcam start
     confirm: false
     name: Start video stream
   - action: streamoff
     command: sudo /home/pi/scripts/webcam stop
     confirm: false
     name: Stop video stream

:spiral_notepad: Note

If you want to view the stream directly on your Pi, please be aware that Midori will not allow you to see the webcam picture. Chromium works although it is a bit slow, but it still might be useful for testing or aiming the camera:

sudo apt install chromium-browser

In any case this is only recommended for debugging purposes during setup, running a graphical user interface on the Pi will put a lot of unnecessary load on the CPU which might negatively influence print results.

:spiral_notepad: Note

mjpegstreamer does not allow to bind to a specific interface to limit the accessibility to localhost only. If you want your octoprint instance to be reachable from the internet you need to block access to port 8080 from all sources except localhost if you don't want the whole world to see your webcam image.

To do this simply add iptables rules like this:

sudo /sbin/iptables -A INPUT -p tcp -i wlan0 ! -s 127.0.0.1 --dport 8080 -j DROP    # for ipv4
sudo /sbin/ip6tables -A INPUT -p tcp -i wlan0 ! -s ::1 --dport 8080 -j DROP         # for ipv6

Replace the interface with eth0, if you happen to use ethernet.

To make them persistent, they need to be saved. In order to be restored at boot time, the easiest way is to install iptables-persist:

sudo apt install iptables-persistent

The only thing left to do now, is save the rules you have added:

sudo /sbin/ip6tables-save > /etc/iptables/rules.v6
sudo /sbin/iptables-save > /etc/iptables/rules.v4

Optional: Touch UI

Touch UI is a plugin that provides an interface for touch screens, e.G. mobile phones or the small 3,5 inch LCDs you can connect to the pi's GPIO pins.

Install the plugin using the plugin manager in the OctoPrint settings. If you want to use is for a local LCD, you need to setup epiphany to start automatically. To do so, first install xautomation to send the keypress for fullscreen later and the epiphany browser if it is not already installed:

sudo apt install epiphany-browser xautomation

Next, create a file startTouchUI.sh in ~/ and add:

#!/bin/bash

function check_octoprint {
    pgrep -n octoprint > /dev/null
    return $?
}

until check_octoprint
do
    sleep 5
done

sleep 5s
epiphany-browser http://127.0.0.1:5000 --display=:0 &
sleep 10s;
xte "key F11" -x:0

Make it executable: chmod +x startTouchUI.sh and add the following to ~/.config/lxsession/LXDE-pi/autostart

@/home/pi/startTouchUI.sh

This will launch the mobile webinterface on startup and put it into fullscreen mode.

Optional: Additional user authentication

In order to protect OctoPrint from unauthorized access, you have two options. For OctoPrint's built-in access control, please see this guide.

For additional security through authentication directly on haproxy before OctoPrint, take a look here.

Optional: Reach your printer by typing its name in address bar of your browser - Avahi/zeroconf/bonjour-based

If you want to reach your printer by typing its name (the hostname of the RasPi running OctoPrint) instead of its IP into your browser's address bar, then you can use the Raspberry Pi Avahi setup (See only section "The flexible way: set up avahi / zeroconf"). Note: "Avahi" is called "Zeroconf", "Rendezvous" or "Bonjour", too.

Installation is simple, on your RasPi just type:

sudo apt update && sudo apt install avahi-daemon

For a network of Linux computers you are done here with the avahi setup. Jump to the paragraph relating the change of the hostname. If you want to enable Avahi support on Windows computers too you'll have to install Bonjour, allow traffic on UDP port 5353 within your firewall and grant internet access to the mDNSresponder.exe on these machines. Have a look here (search for "Get Bonjour for Windows") for a detailed description of the Windows setup.

The next step is to change the hostname of your RasPi into something more printer specific (e.g. <yourprinter>) via editing the files etc/hostname and the etc/hosts on your RasPi. Change the default name into <yourprinter> in the hostname-file via

sudo nano /etc/hostname

and do the same (here change the name behind the 127.0.1.1 into <yourprinter>) in the hosts-file via

sudo nano /etc/hosts

Now restart your RasPi via

sudo reboot.

You can now reach your RasPi running OctoPrint within your network by pointing your browser to

<yourprinter>.local .

Note you can use this too, when you want to ssh into your RasPi:

ssh <username>@<yourprinter>.local.

Additional Resources:


Octoprint No interface, only webcam
Re-install 1.3.9 from command line
Webcam start/stop buttons not working
Plugin Manager not working (self installed Octoprint on Linux)
Autostart on the Asus Tinker board
Printer moves unexpectedly when stopping SD-card copy
Using ffmpeg for webcam streaming and timelapse support
OctoPrint webcam is not working
C270 on Debian Stretch
Deleted oprint folder
Sarting Octoprint at the startup
The 'pip' command could not be found or does not correctly work for this installation of OctoPrint
Octoprint on Nanopi M4
Pi 3 Model A+ unable to boot latest OctoPi stretch image 0.15
Sjcam 5000 working with octoprint?
Information about OctoPrint and Advice, Please?
Octopi high cpu usage (browser side)
Problem installing OctoPrint
Installing on other linuxes
#2

Thanks for posting it here!

I'm having one problem compared to the version where you download octoprint via git,
after running pip install https://get.octoprint.org/latest i only have the venv folder inside ~/OctoPrint.

I can manually start the octoprint server, but the Scripts folder is missing and thus i can't configure Octoprint for automatic startup, any ideas on how i can fix this?


#3

Same problem, I ended up copying the scripts from the octoprint github project and created the files mentioned in this guide using vi, which worked out for me.


#4

@aurror @John thanks for the heads-up, I've adjusted the guide accordingly.


#5

Still having one problem with this version, I can't install any plugins from the repository.
I've included the octoprint.log error message.
I'm not using a raspberry, maybe it's because of that, but the git version works fine.

octoprint.log (10.9 KB)


#6

That's this issue:

Caused by a backwards incompatible patch upgrade of a third party library. Run pip install sarge==0.1.4.


#7

Need to add the line

sudo /usr/bin/easy_install virtualenv

before the virtualenv venv line in the "basic setup" instructions

I am installing on armbian though


#8

To be able to restart octoprint service in case of the pi user asking a password, /usr/sbin/service should also be added on the passwordless sudoers (cf. Note)
But this implies a security issue because this user could then start/stop any services on the host...


#9

I'm having problems with the octopi support plugin not passing the initial startup, and as a result, I cant install any plugins. I am running a basic install on a raspberry pi 3 model B.

octoprint.log (15.1 KB)


#10

Nope, that's not the cause of the issue. Rather you are running into a compatibility problem with the latest version of the sarge dependency which sadly is backwards incompatible. Easily solved though by downgrading that dependency (which 1.3.9 will ensure out of the box):

source /home/pi/OctoPrint/venv/bin/activate
pip install sarge==0.1.4
sudo service octoprint restart

See also


#11

thank you it fixed the problem immediately


#12

I can't seem to get the systemd service working properly. I can run the server just fine manually but for some reason the sudo service octoprint start etc doesn't seem to be working to start the service. I've even started it manually and checked the logs but there isn't anything in there for when I try to use the service handler. Any advice?


#13

I'm trying to get the official raspberry Pi cam to stream on octoprint.

Im getting stuck here
git clone https://github.com/jacksonliam/mjpg-streamer.git

it says that this couldn't be found.


#14

@rog can you copy-paste the full error message please? "This" can mean a lot here :wink:


#15

How are you!
Now I am following up this manual one by one. I thought that it has been almost done but I stuck another problem again now.

I would like to put this code into /home/scripts/webcam

But all I know thing is $sudo vi .
What am I have to do ?
Thanks for your great support and manual!

Regards,
Clara


#16

/home/pi/scripts/webcam start

Should be /home/pi/.octoprint/scripts/webcam start

The hidden directory is not in the path
The DEV team might need to update the main page "howto" otherwise it will cause alot of confusion


#17

No, it shouldn't. The scripts for controlling/firing up the webcam server a) have to be created and b) don't make sense in OctoPrint's settings folder since the webcam is something that is unrelated to OctoPrint and merely used by it.


#18

When following the guide and trying to start OctoPrint, I got the: NameError: name 'unicode' is not defined error.

Found this Github issue: https://github.com/foosel/OctoPrint/issues/2349, figured it's because of a wrong python version.

Forced to create virtualenv using python 2.7 and it worked. Instead of virtualenv venv, I used virtualenv -p /usr/bin/python2.7 venv.

Maybe others encountered same issue. Possibly update the guide?

Installing on Raspberry Pi 3.


#20

Is it possible to in the future have the option to preset the pi hostname in the same manner that we can preset the wifi setting via octopi-wpa-supplicant.txt? When you are setting up multiple Pi(s) it would be nice if you could make sure they don't all try to point to the same address at once from the get go.


#21

Are you a 3D printer manufacturer? Most people only setup one at a time.