Install Octoprint on Raspberry Pi Desktop with multiply instances (Guide)

So I have been at this for two days and finally after these two days I can say I figured out how to install Octoprint with multi instances on a laptop running Raspberry pi Desktop os. Now getting Octoprint installed wasn't that hard but getting the multi instances to work together was tricky. I tried following guides on YouTube and searched and searched on here but none of the guides worked. I could get the first instance to work but none of the other ones. I figured I would share what I did so that way if someone wants to do it they can without looking at a million YouTube videos, reading a bunch of posts.

Step 1: check for updates, then install python parts

sudo apt update
sudo apt install python-pip python-dev python-setuptools python-virtualenv git libyaml-dev build-essential

Step 2: create Octoprint dir

mkdir ~/OctoPrint && cd ~/OctoPrint

Step 3: virtual environment

virtualenv venv
source venv/bin/activate
pip install pip --upgrade
pip install octoprint
sudo usermod -a -G tty pi
sudo usermod -a -G dialout pi

Step 4: Setup Octoprint to autostart on restart/reboot

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

Set 5: edit octoprint file

sudo nano /etc/default/octoprint

Remove the hash comment character "#" from the start of the following line in the script file:

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

Save the file.

Set 6: dd the modified script to the autostart sequence by using this command

sudo update-rc.d octoprint defaults

If you want to test octoprint then run this command and then go to your browser and go to http://ipaddy:5000

~/OctoPrint/venv/bin/octoprint serve

Everything show be working great at this point. go ahead and setup octoprint in the browser and make sure it all works. once you have gone thru all the steps to get octoprint up and running let dive into getting more then one running.

Now lets setup another instances of octoprint. I am going to show you one more instance if you want more just change the number 2 to 3,4,5 so on and so on.

Step 1: First, we copy the Octoprint directory

cp -R /home/pi/.octoprint /home/pi/.octoprint2

Next copy the OctoPrint Configuration script /etc/defaults/octoprint to /etc/defaults/octoprint2

sudo cp /etc/default/octoprint /etc/default/octoprint2

Step 2: Modify the port and add a new basedir.

sudo nano /etc/default/octoprint2

change the configuration file (these are if you have haproxy installed (I don't)

PORT=5001
DAEMON_ARGS="--host=$HOST --port=$PORT --config /home/pi/.octoprint2/config.yaml --basedir /home/pi/.octoprint2/"

Now change the default one

sudo nano /etc/default/octoprint
PORT=5000
DAEMON_ARGS="--host=$HOST --port=$PORT --config /home/pi/.octoprint/config.yaml --basedir /home/pi/.octoprint/"

If you don't have haproxy installed do the following (this is what I did)

sudo nano /etc/default/octoprint
PORT=5000
DAEMON_ARGS="--port=$PORT --config /home/pi/.octoprint/config.yaml --basedir /home/pi/.octoprint/"
sudo nano /etc/default/octoprint2
PORT=5001
DAEMON_ARGS="--port=$PORT --config /home/pi/.octoprint2/config.yaml --basedir /home/pi/.octoprint2/"

In your browser it will be http://ipaddy:5000 and http://ipaddy:5001

Step 3: Get a second instance automatically running after booting

sudo cp /etc/init.d/octoprint /etc/init.d/octoprint2

Now we need to modify the it a little

sudo nano /etc/init.d/octoprint2

You need to make some changes at the top of this file! Change every name from "octoprint" to "octoprint2" and "OctoPrint" to "OctoPrint2" but leave the line

DAEMON=/usr/bin/octoprint

UNTOUCHED!!!

Mine looks like this

#!/bin/sh


### BEGIN INIT INFO
# Provides:          octoprint2
# Required-Start:    $local_fs networking
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: OctoPrint2 daemon
# Description:       Starts the OctoPrint daemon with the user specified in
#                    /etc/default/octoprint2.
### END INIT INFO


# Author: Sami Olmari


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="OctoPrint2 Daemon"
NAME="OctoPrint2"
DAEMON=/usr/bin/octoprint
PIDFILE=/var/run/$NAME.pid
PKGNAME=octoprint2
SCRIPTNAME=/etc/init.d/$PKGNAME
. . . . . .

Save File

Step 4: Now we need to use systemctl

sudo systemctl daemon-reload

run this command to have it start automatically after booting

sudo update-rc.d octoprint2 defaults

To start the service manual

sudo /etc/init.d/octoprint2 start

To check if it has been added successfully to sysemctl

systemctl status octoprint2.service

Step 5: enable restart of second octoprint instance over the web interface

nano /home/pi/.octoprint2/config.yaml

Change the service name in the restart command to octoprint2.

serverRestartCommand: sudo service octoprint2 restart

If there are no errors it should all work

Now reboot

This is a combination of a few guides that are on the net. I just pieced them together that way if someone was trying to get something like this to work they wouldn't have to search and search.
credit for the Multi instances is from here

http://thomas-messmer.com/index.php/14-free-knowledge/howtos/79-setting-up-octoprint-for-multiple-printers

this first part is just your basic install guide of octoprint on linux.

I hope this helps people as I struggled with getting this to work for the last two day and I don't want anyone else to struggle with it..

I didn't include Haproxy because I didn't use it. but on the multi instances link it shows you how to use Haproxy with multi octoprints running!!!

1 Like

Hello
This tutorial doesn't work with octoprint v0.18 because it uses systemd not /etc/init.d
I built a bash script to create many instances for octoprint.
Juste type ./OctopiUtil.sh --addprinter 5001 and it add an instance listing on port 5001.

Best regards, Vince.
Processing: OctopiUtil.sh...
OctopiUtil.zip (1.6 KB)

#!/bin/bash
UDEVFILE=/etc/udev/rules.d/99-usb.rules
SYSD=/etc/systemd/system
PIHOME=/home/pi
PIUSER=pi

syntax()
{
	echo "$0 action [options]"
	echo "$0 version 0.1 for octoprint 0.18 and above"
	echo "$0 help you to create octoprint printer instance to use multi printers and help to humanize USB port with a printer name like /dev/E10 ou /dev/CR10."
	echo
	echo "Actions are :"
	echo "   --help           display this help"
	echo
	echo "   --addprinter internet_port"
	echo "                  add printer on web interface port 'internet_port'."
	echo "                  ex : $0 --addprinter 5001"
	echo
	echo "   --remprinter internet_port"
	echo "                  Remove printer identified by 'internet_port'."
	echo "                  ex : $0 --remprinter 5001"
	echo
	echo "   --addudev ttyUSBx Human_name"
	echo "                  Add a humane name (like /dev/E12) to ttyUSBx where x is a USB port number."
	echo "                  ex : $0 --addudev ttyUSB1 CR10s"
	echo
	echo "   --remudev Human_name"
	echo "                  Remove humane name."
	echo "                  ex : $0 --remudev CR10s"
}

addprinter()
{
	if [ -f "/etc/init.d/octoprint" ]; then
		echo "Old version of octoprint not supported"
		return 1
	fi
	if [ -f "$SYSD/octoprint$1.service" ]; then
		echo "Port $i allready exists for octoprint"
		return 1
	fi

	netstat -ltn | sed "s/\ \ */\ /g" | grep ^tcp | cut -d ' ' -f 4 | grep -q :$1$
	if [ "$?" = "0" ]; then
		echo "Port $1 allready used"
		return 1
	fi
	sed "s/PORT=.*/PORT=$1\"/" <"$SYSD/octoprint.service" >"$SYSD/octoprint$1.service"
	if [ "$?" != "0" ]; then
		echo "Impossible to modify or create the file '$SYSD/octoprint$1.service'"
		return 1
	fi
	sed -i "s/HOST=.*/HOST=0.0.0.0\"/" "$SYSD/octoprint$1.service"
	sed -i "/^\[Service\]$/aEnvironment=\"OCTPRINTBASEDIR=$PIHOME/.octoprint$1\"" "$SYSD/octoprint$1.service"
	sed -i "s|--host=\${HOST} --port=\${PORT}.*$|--host=\${HOST} --port=\${PORT} --basedir=\${OCTPRINTBASEDIR}|" "$SYSD/octoprint$1.service"
	if [ -d "$PIHOME/.octoprint$1" ]; then
		echo "$PIHOME/.octoprint$1 allready exists. Please remove it manualy"
		rm -f "$SYSD/octoprint$1.service"
		return 1
	fi
	if [ ! -d "$PIHOME/.octoprint" ]; then
		echo "$PIHOME/.octoprint doesn't exist. May be this octoprint version isn't supported."
		rm -f "$SYSD/octoprint$1.service"
		return 1
	fi
	mkdir "$PIHOME/.octoprint$1"
	if [ "$?" != "0" ]; then
		echo "Impossible to create dir $PIHOME/.octoprint$1"
		rm -f "$SYSD/octoprint$1.service"
		return 1
	fi
	chown $PIUSER:$PIUSER -R "$PIHOME/.octoprint$1"
	if [ "$?" != "0" ]; then
		echo "Impossible to chown $PIHOME/.octoprint$1"
		rm -f "$SYSD/octoprint$1.service"
		rm -fR "$PIHOME/.octoprint$1"
		return 1
	fi
	systemctl start octoprint$1.service
	return 0
}

remprinter()
{
	systemctl | grep octoprint$1.service | grep -q running
	if [ "$?" = "0" ]; then
		systemctl stop octoprint$1.service
	fi
	rm -f "$SYSD/octoprint$1.service"
	rm -fR "$PIHOME/.octoprint$1"
	return 0
}

addudev()
{
	if [ ! -f "$UDEVFILE" ]; then
		echo "# 3D printers declaration and alias" >$UDEVFILE
	fi
	cat $UDEVFILE | grep -v ^# | grep -q "SYMLINK+=\"$2\""
	if [ "$?" = "0" ]; then
		echo "$2 allready exists"
		return 1
	fi
	DEVICE=$(find /sys -name "$1" | grep "devices.*[0-9]/$1$")
	if [ "$?" != "0" ]; then
		echo "device $1 not found. Try ls /dev to find a device like ttyUSB0."
		return 1
	fi
	DEVICE=$(echo "$DEVICE" | sed "s|/sys||" | sed "s|$1$||")
	cat $UDEVFILE | grep -v ^# | grep -q "DEVPATH==\"$DEVICE"
	if [ "$?" = "0" ]; then
		ALIAS=$(cat $UDEVFILE | grep -v ^# | grep "DEVPATH==\"$DEVICE" | grep -o "SYMLINK+=\".*\"")
		echo "$2 is allready assigned to $DEVICE -> $ALIAS"
		return 1
	fi
	echo "SUBSYSTEM==\"tty\", DEVPATH==\"$DEVICE*\", SYMLINK+=\"$2\"" >>$UDEVFILE
	return 0
}

remudev()
{
	cat $UDEVFILE | grep -q "SYMLINK+=\"$1\""
	if [ "$?" != "0" ]; then
		echo "$1 doesn't exist"
		return 1
	fi
	mv $UDEVFILE $UDEVFILE.bak
	cat $UDEVFILE.bak | grep -v "SYMLINK+=\"$1\"" | sed "/^$/d" >$UDEVFILE
	rm -f $UDEVFILE.bak
	return 0
}

if [ "$#" = "0" ]; then
	syntax
	exit 0
fi

if [ "$UID" != "0" ]; then
	echo "You must be root to execute $0"
	exit 1
fi

case $1 in
	--help)
		syntax
		;;
	--addprinter)
		if [ "$#" != "2" ]; then
			echo "internet_port missing"
			exit 1
		fi
		addprinter $2
		;;
	--remprinter)
		if [ "$#" != "2" ]; then
			echo "internet_port missing"
			exit 1
		fi
		remprinter $2
		;;
	--addudev)
		if [ "$#" != "3" ]; then
			echo "ttyUSBx or Human_name missing"
			exit 1
		fi
		addudev "$2" "$3"
		;;
	--remudev)
		if [ "$#" != "2" ]; then
			echo "Human_name missing"
			exit 1
		fi
		remudev "$2"
		;;
	*)
		echo "unkown action $1"
		exit 1
		;;
esac

ERR=$?
if [ "$ERR" != "0" ]; then
	echo "$1 failed"
	exit $ERR
fi
echo "$1 success"
exit 0

1 Like