Setting up OctoPrint on Windows

Sounds like the Python install has not gone correctly. How did you install it? What version of Python have you installed? You could try reinstalling it I guess.

I am wondering the same thing.
In the setting, Server what do we input for "Restart OctoPrint" ? Instead of killing the process and running the .vbs again ?

I have this same problem too. Python works, correct version shows up, but "import netifaces" gives an error whether I do it in python or as part of the OctoPrint install.

How did you install it? Through the windows store, did you install for all users (as admin)?

From the installer from python.org. It was working until I tried to update OctoPrint last week.

Thanks to all who've contributed to this guide so far! I know Linux is the most common environment for OctoPrint, but given the limited availability of Raspberry Pi units in my area and the fact that I have a Windows PC (old desktop repurposed as backup server, basic workstation for Chrome & Cura, etc.) already running right near my printer, I figured I'd see how well I could make this setup work.

The PC in question is shared by multiple users, rebooted periodically, etc., so I wanted to come up with a foolproof Windows service setup. I also tried to mirror the Pi setup where I could (e.g., running a reverse proxy even though it's not strictly needed) to avoid weird issues down the line.

So, here's a guide to setting up OctoPrint, cam2web (MJPEG server), and Caddy (reverse proxy) as Windows services that automatically start on reboot, can easily be stopped/restarted at will, redirect stderr to files for debugging, etc.

OctoPrint setup

First off, install OctoPrint itself, but don't run it yet:

  1. Install Python and Visual Studio Build tools as described in the core guide above.
  2. Since we're going to run as a service and try to follow idiomatic Windows practices, install OctoPrint to C:\Program Files\OctoPrint rather than the recommended path. (You'll need to elevate via a UAC prompt, but don't worry, the actual service will run as a user with limited privileges.)
  3. Since we don't have a virtual service account for OctoPrint yet, skip the steps involving running octoprint serve at this time.

cam2web (MJPEG server) setup

The standard guide recommends Yawcam, and there's really nothing wrong with it, but it's a Java application. I strongly dislike having to install the JRE on home PCs, and as a Java app I've seen Yawcam use more than 1 GiB of RAM.

That's not ideal in my opinion, so I prefer cam2web. It requires no special dependencies, has a much cleaner UI, and uses almost no RAM. Set it up as follows:

  1. Download the latest release from GitHub..
  2. Since cam2web doesn't ship with a sample configuration file, this time you will want to run it once manually as your normal user account.
  3. Set up your camera as appropriate in the cam2web GUI, making sure you can access it at http://127.0.0.1:8000/camera/mjpeg.
  4. Exit the cam2web GUI, stopping the streaming server in the process.
  5. Navigate to the %USERPROFILE%\cam2web folder and copy (or move, if you prefer) the app.cfg file from that directory to C:\Program Files\cam2web.

As I write this in September 2021, the latest prebuilt Windows binary on cam2web's GitHub is outdated (from 2017). The latest Git HEAD has been improved to use a lot less CPU when not actively serving traffic, dropping from around 8% idle CPU usage (comparable to YawCam) to around 0.1% on my system. As such, you may want to build from source (which can be done with the free Visual Studio Community) instead.

Caddy (reverse proxy) setup

OctoPi uses HAProxy as a reverse proxy to make both OctoPrint and mjpg-streamer accessible on port 80, but HAProxy doesn't seem to have a Windows port. The de facto standard nginx does have a Windows port, but it has a number of limitations compared to the Linux version, and additionally, nginx requires extra configuration to proxy WebSocket traffic correctly (required for OctoPrint to work).

Instead, let's use Caddy, a lightweight, cross-platform reverse proxy written in Go. Here's how we'll set it up:

  1. Download the latest release from Caddy's Web site and unpack it to C:\Program Files\Caddy.
  2. Create a Caddy configuration file in C:\Program Files\Caddy\Caddyfile with the following contents:
{
	# Disable the REST API that listens on port 2019 by default since we do not
	# need to dynamically reconfigure Caddy.
	admin off
}
:80 {
	# Proxy traffic to OctoPrint backend by default.
	reverse_proxy 127.0.0.1:5000
	# Proxy webcam traffic to the cam2web backend instead.
	reverse_proxy /camera/* 127.0.0.1:8000 {
		# Disable buffering for MJPEG streams to reduce latency.
		flush_interval -1
		# Disable chunked encoding since the OctoEverywhere plugin's HTTP proxy
		# doesn't handle it correctly (truncating the webcam stream instead).
		header_down Transfer-encoding identity
	}
}

Caddy is really powerful, and I encourage y'all to read their docs and see what it can do. But the above should be enough to get you started. It's probably fine to leave their admin API on (I think it only listens on localhost), but I had no need for it and felt safer turning it off, so that's what I did for my own setup.

You can remove the Transfer-encoding: identity hack if you don't care about using OctoEverywhere for remote access, or if they fix the relevant bug in their HTTP proxy. :slight_smile: For now, though, it doesn't hurt to leave it around, since there's really no reason to care about keep-alive for the camera backend anyway.

Creating Windows services

At this point, you should have all the relevant software installed and configured, and none of it running. Let's fix that. :slight_smile: Since none of these applications natively implement Windows services, we'll need a service wrapper to run them. There's a number of options, but I've found nssm to be easy and effective:

  1. Download the latest release of nssm and unpack it to C:\Program Files\nssm.
  2. Open an elevated Command Prompt (right click and select "Run as administrator"), then CD "C:\Program Files\nssm".

Now we're ready to install some services! Rather than running as a highly privileged user, we'll give each binary its own virtual service account that will only have access to the files it actually needs at runtime.

OctoPrint service

From the elevated Command Prompt, run nssm install OctoPrint. Configure as follows:

  • Application > Application > Path: C:\Program Files\OctoPrint\venv\Scripts\octoprint.exe
  • Application > Application > Startup directory: C:\Program Files\OctoPrint\venv
  • Application > Application > Arguments: --basedir "C:\Program Files\OctoPrint\config" serve
  • Log on > Log on as > Virtual service account
  • I/O > I/O redirection > Output (stdout): C:\Program Files\OctoPrint\service.log
  • I/O > I/O redirection > Error (stderr): C:\Program Files\OctoPrint\service.log

cam2web service

From the elevated Command Prompt, run nssm install cam2web. Configure as follows:

  • Application > Application > Path: C:\Program Files\cam2web\cam2web.exe
  • Application > Application > Startup directory: C:\Program Files\cam2web\cam2web
  • Application > Application > Arguments: /start /fcfg:"C:\Program Files\cam2web\app.cfg"
  • Log on > Log on as > Virtual service account
  • I/O > I/O redirection > Output (stdout): C:\Program Files\cam2web\service.log
  • I/O > I/O redirection > Error (stderr): C:\Program Files\cam2web\service.log

Caddy service

From the elevated Command Prompt, run nssm install Caddy. Configure as follows:

  • Application > Application > Path: C:\Program Files\Caddy\caddy.exe
  • Application > Application > Startup directory: C:\Program Files\Caddy\caddy.exe
  • Application > Application > Arguments: run
  • Log on > Log on as > Virtual service account
  • I/O > I/O redirection > Output (stdout): C:\Program Files\Caddy\service.log
  • I/O > I/O redirection > Error (stderr): C:\Program Files\Caddy\service.log

Setting folder permissions for service accounts

Now we've got all the relevant software installed and services set up, but if we start any of the services now, they'll fail in various ways since their virtual service accounts don't have access to the appropriate files yet. Fix that as follows:

  1. Open C:\Program Files\OctoPrint in File Explorer.
  2. Right click on the folder and select "Properties".
  3. Switch to the "Security" tab.
  4. Click "Edit", then click "Add".
  5. In the "Enter the object names to select" text box, type NT Service\OctoPrint (the name of the virtual service account that Windows automatically created).
  6. Click "Okay".
  7. Now, under "Permissions for OctoPrint", find the row saying "Full control" and check the "Allow box".
  8. Click "OK" to save the security settings, then close the Properties dialog.

This gives OctoPrint access to the relevant directory, but not the rest of your filesystem. Repeat these steps for the directories where you installed cam2web and Caddy, using the NT Service\cam2web and NT Service\Caddy user names, respectively.

Wrapping up

Okay! At this point you can safely start the services. Open the "Services" management console, find the three services you created, right click on each, and select start.

You should now have an accessible OctoPrint instance listening port 80 that starts automatically on reboot. Browse to http://127.0.0.1/ and configure OctoPrint to your hearts content, using these webcam settings (if you followed my recommended cam2web and Caddy setup):

  • Stream URL: /camera/mjpeg
  • Snapshot URL: http://127.0.0.1/camera/jpeg

If you want to see console output from OctoPrint, look in C:\Program Files\OctoPrint\service.log. Same for the other services you set up. To update OctoPrint, stop and start the service through usual means, just like any other Windows service. :slight_smile:

Bonus: Restarting OctoPrint from server UI

OctoPrint on Windows doesn't prepopulate the commands to restart itself or the system, but with this service setup, you can do this pretty easily. In the OctoPrint Web app, go to "Settings > OctoPrint > Server > Commands" and enter the following:

  • Restart OctoPrint: START "" "C:\Program Files\nssm\nssm.exe" restart OctoPrint
  • Restart system: shutdown /r /c "Restart initiated from OctoPrint."
  • Shutdown system shutdown /s /c "Shutdown initiated from OctoPrint."

(Note the START command for "Restart OctoPrint". Without this, nssm will end up killing its own restart command when it stops the service, and the service will never come back up. Oops!)

In the weirdness of Windows, the virtual service account has permission to restart (or shut down) the whole system by default... but not to stop or start the service itself. It turns out there's not a convenient way to grant that permission by default, but the SetACL third-party utility makes this easy:

SetACL -on OctoPrint -ot srv -actn ace -ace "n:NT Service\OctoPrint;p:start_stop"

Bonus: Persistent camera settings

Does it frustrate you that your webcam settings (focus, exposure, etc.) reset themselves on every reboot? You can use the WebCameraConfig program to work around that by restoring webcam settings from a file.

If you used nssm to install the cam2web service, you can use the "Hooks" tab of your nssm settings (nssm edit cam2web if you already made the service) to create an "Application start > Successful application startup hook" to restore camera settings every time the cam2web service (re)starts.

Note that my camera seems to require I restore the settings twice for them to actually take effect. I use the following batch file in my "Successful application startup" hook and it works fine:

@ECHO OFF
:: Restore camera settings (focus, exposure, etc.) that do not normally persist
:: across system restarts. Some settings must be set in a particular order or
:: else they have no effect, so we brute force this by simply running twice.
::
:: See also: https://community.octoprint.org/t/setting-up-octoprint-on-windows/383/134?u=bcat
"C:\Program Files (x86)\WebCameraConfig\WebCameraConfig.exe"
"C:\Program Files (x86)\WebCameraConfig\WebCameraConfig.exe"
3 Likes

It's nice to see an alternative streamer for Windows.

My guess is that it's similar to setting those settings on Linux: some settings can only be altered during certain conditions. Such as, you can only set Focus Absolute after setting Focus Auto to manual. The program probably doesn't know the inter-dependencies since they'll vary by camera, so it's just setting things at random, more or less. So, if things are 'out of order' it will take multiple passes to get everything set.

Oh, yeah, that's a good point. It would explain the behavior I saw. At first I thought it was a race condition between cam2web startup and when the camera settings got applied, but a 30-second sleep didn't change anything. Undocumented ordering dependencies between the individual settings makes sense more sense.

Hi foosel, I installed octoprint on my windows .
now my question is how can I connect my windows to the printer and control my 3D printer?

you connect your printer in the webinterface
http://localhost:5000 or http://ip-of-your-pc:5000

You'll need a USB cable as well to connect the printer to the windows machine physically.

1 Like

I did not mean that

thank you. It works.
now how can I access my octo from another computer?

@PrintedWeezl already told you.

Here is a video I created walking through the basics of the process. How to install OctoPrint on Windows 10 (& Run Multiple Instances) - YouTube
Im no expert but might help someone!

1 Like

On my system I get as far as py -m venv venv and all it does is come straifght back to the command prompt. I don't get the venv prompt. (this is running on a 3rd Gen i5 with 8GB RAM and 120GB SSD with Win 10 Pro 20H2)

py --version brings up Python 3.10.0 and as far as I can see I have everything else installed as it should be.

There's probably something simple that I either didn't know about or haven't seen yet that needs to be done - anyone got any ideas?

Rototype.

You missed the 3rd command in the list:

venv\Scripts\activate.bat

OK, got it now. It wasn't clear that the command prompt would come up again after the second command and that the 3rd command needed to be typed in there, I was expecting a different command prompt.

so i followed this guide.... however the setup wizard does not show up? and im at a lost as to what to do, i had octopi set up before but now i have a dedicated pc for the printer but i like the aspect of being able to access my printer remotely

not sure what i did wrong but the initial setup wizard doesnt launch at all

If you had OctoPrint setup on your Windows PC before, it will have found the old configuration.

You can delete the configuration by browsing to %APPDATA%\OctoPrint - this works in the search bar automatically, or the full path (for me) is C:\Users\charl\AppData\Roaming\OctoPrint.