How to install and run the linuxcnc-grpc server on your LinuxCNC machine.

Prerequisites

  • A Linux machine with LinuxCNC 2.9 or newer installed (physical or simulation)
  • Python 3.10+ (included with most LinuxCNC installations)
  • Network access if you want to connect from a remote machine

Install

SSH into your LinuxCNC machine (or open a terminal on it) and install:

pip install linuxcnc-grpc

Or with uv:

uv pip install linuxcnc-grpc

This installs both the gRPC server and the Python client library.

Start the Server

The server can be started before or after LinuxCNC. If LinuxCNC is not running yet, the server waits for it and answers UNAVAILABLE until it has connected.

# Start LinuxCNC (if not already running)
linuxcnc /path/to/your/machine.ini

# Start the gRPC server
linuxcnc-grpc --host 0.0.0.0 --port 50051

--host 0.0.0.0 makes the server listen on all network interfaces so remote clients can connect. Use 127.0.0.1 to restrict to local-only access.

You should see:

2024-01-15 10:30:45 [INFO] linuxcnc_grpc.server: Server configured on 0.0.0.0:50051
2024-01-15 10:30:45 [INFO] linuxcnc_grpc.server: LinuxCNC + HAL gRPC Server

Verify It’s Working

From the LinuxCNC machine

python3 -c "
import grpc
from linuxcnc_pb import linuxcnc_pb2, linuxcnc_pb2_grpc

channel = grpc.insecure_channel('localhost:50051')
stub = linuxcnc_pb2_grpc.LinuxCNCServiceStub(channel)
status = stub.GetStatus(linuxcnc_pb2.GetStatusRequest())
print(f'Connected! LinuxCNC version: {status.version}')
print(f'Machine state: {linuxcnc_pb2.TaskState.Name(status.task.task_state)}')
channel.close()
"

From a remote machine

Install the client (pip install linuxcnc-grpc) and run the check above with LINUXCNC_IP:50051 in place of localhost:50051.

The server also answers the standard gRPC health service for its two services, linuxcnc.LinuxCNCService and hal.HalService:

python3 -c "
import grpc
from grpc_health.v1 import health_pb2, health_pb2_grpc

channel = grpc.insecure_channel('LINUXCNC_IP:50051')
health = health_pb2_grpc.HealthStub(channel)
for service in ('linuxcnc.LinuxCNCService', 'hal.HalService'):
    reply = health.Check(health_pb2.HealthCheckRequest(service=service), timeout=5)
    print(service, health_pb2.HealthCheckResponse.ServingStatus.Name(reply.status))
channel.close()
"

This should print:

linuxcnc.LinuxCNCService SERVING
hal.HalService SERVING

linuxcnc.LinuxCNCService is NOT_SERVING while the server is waiting for LinuxCNC. Use Check for probes; Watch is limited to a few clients at a time (see Health service). The server does not offer gRPC reflection, so grpcurl ... list does not work.

Or just test network connectivity:

nc -zv LINUXCNC_IP 50051

If this fails, check your firewall settings — see Server Configuration.

Auto-Start with LinuxCNC

For production use, you’ll want the gRPC server to start automatically when LinuxCNC launches. The simplest approach is to add it to your machine’s HAL file:

loadusr linuxcnc-grpc --host 0.0.0.0 --port 50051

See Server Configuration for all auto-start methods (HAL file, dedicated HAL file, systemd service).

A server started with loadusr starts and stops with LinuxCNC. A server run as a systemd service (or under any other supervisor) is stopped by every LinuxCNC shutdown and restarted by the supervisor, so the unit needs Restart=always and a restart delay of about 2 seconds:

[Service]
ExecStart=/usr/local/bin/linuxcnc-grpc --host 0.0.0.0 --port 50051
Restart=always
RestartSec=2

Do not set RestartSec below about 0.5 seconds (systemd’s default is 0.1 s). A server restarted that quickly comes up while the old LinuxCNC is still shutting down and can attach to parts of it: on the LinuxCNC 2.9 simulator, earlier versions of the server answered wrongly after 15 of 19 restarts with a delay of 0 to 0.1 seconds. This version notices, logs an error and exits with status 1, so the supervisor starts it again (after 6 of 24 restarts measured at those delays, all of which then worked), but each is a wasted restart, and systemd gives up on a unit that fails 5 times in 10 seconds. With 2 seconds there was no such restart in 10 of 10. The full unit file is in Server Configuration.

What’s Next

  • Client Quickstart — Install a client library and make your first API calls
  • Server Configuration — Advanced options, TLS, firewall, performance tuning
  • Tutorial — Full walkthrough of connecting from a remote development machine