Implementación de LM Studio como servicio en Ubuntu 25.04

Heathrow Closed: Flight Suspension Expected to Continue for the Coming Days, Says London Airport Manager

From the BBC website

British Airways estimated that 85% of its scheduled flights would operate on Saturday, but all flights were delayed. By 7:00 a.m. GMT, most departures had proceeded as expected, but of the arrivals, nine of the first 20 flights scheduled to land were canceled.

Somewhere between versions 0.3.31-2 and 0.3.31-7, a change occurred that forcibly changes the listening interface from external to 127.0.0.1 and disables CORS.

Since LM Studio supports hot-reload of the HTTP server config (at least when changing cors and networkInterface), you can make the change without restarting (it will work once):

%h/.lmstudio/.internal/http-server-config.json
jq '.cors = true | .networkInterface = "0.0.0.0"' ~/.lmstudio/.internal/http-server-config.json | sponge ~/.lmstudio/.internal/http-server-config.json

remember to install sponge, which is included in the moreutils package.

To make the change automatic, modify the startup script:

$HOME/.config/systemd/user/lm-studio.service
[Unit]
Description=LM Studio Service
After=network.target

[Service]
Type=simple

ExecStart=/usr/bin/xvfb-run -a --server-args="-screen 0 1920x1080x24" %h/llm/lmstudio --run-as-service

# 1. Start the HTTP server
ExecStartPost=/bin/bash -c 'sleep 15 && exec lms server start'

# 2. Apply required settings (after the server has already started)
ExecStartPost=/bin/bash -c ' \
  sleep 2 && \
  jq ".cors = true | .networkInterface = \\\"0.0.0.0\\\"" \
     "%h/.lmstudio/.internal/http-server-config.json" \
     > "%h/.lmstudio/.internal/http-server-config.json.tmp" && \
  mv "%h/.lmstudio/.internal/http-server-config.json.tmp" \
     "%h/.lmstudio/.internal/http-server-config.json" \
'

Restart=always
RestartSec=10
Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin:%h/.lmstudio/bin
Environment=DISPLAY=:99
WorkingDirectory=%h/llm

[Install]
WantedBy=default.target

Apply the changes and restart the service (without sudo!):

systemctl --user daemon-reload
systemctl --user stop lm-studio.service
systemctl --user start lm-studio.service

Ten en cuenta el ajuste de parámetros del sistema operativo para un rendimiento óptimo.

Ejemplo

Aquí tienes un ejemplo de mi sistema (es un portátil con una tarjeta gráfica conectada mediante Oculink):

free -h
               total        used        free      shared  buff/cache   available
Mem:            37Gi       4.9Gi        29Gi       240Mi       3.5Gi        32Gi
Swap:          8.0Gi          0B       8.0Gi
cat /proc/meminfo | grep -E 'MemTotal|MemAvailable'

MemTotal:       39223064 kB
MemAvailable:   34073484 kB

La VRAM se muestra por separado — no está incluida en MemTotal.

nvidia-smi

Fri Nov 21 12:24:47 2025      	
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.82.09              Driver Version: 580.82.09      CUDA Version: 13.0     |
+-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 5060 Ti     Off |   00000000:01:00.0 Off |                  N/A |
|  0%   44C    P8              8W /  180W |      13MiB /  16311MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A            5314      G   /usr/bin/gnome-shell                      2MiB |
+-----------------------------------------------------------------------------------------+

Qué hacer

Revisa la política de overcommit y swappiness:

cat /proc/sys/vm/overcommit_memory
cat /proc/sys/vm/overcommit_ratio
cat /proc/sys/vm/swappiness

Valores recomendados:

2 # límite = (RAM * ratio/100) + Swap
80 # este es el ratio
10 # no más de 20 — indica cuánta RAM libre debe quedar para que comience a usar swap; el swap puede ser de 8 GiB si no se usa hibernación del sistema operativo, entonces el swap será igual al tamaño de la memoria RAM

Comandos para ejecutar:

sudo sysctl vm.overcommit_ratio=80
echo 'vm.overcommit_ratio=80' | sudo tee -a /etc/sysctl.d/99-ml-workstation.conf
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.d/99-ml-workstation.conf

IMPORTANTE: Para LLM (especialmente llama.cpp), THP = always puede causar pausas (retardos de un minuto después de iniciar sesión en el sistema operativo).

cat /sys/kernel/mm/transparent_hugepage/enabled
# Debe mostrar: [always] madvise never

Al trabajar con disco (por ejemplo, cuando los pesos del LLM se escriben en un SSD), es importante la configuración de writeback:

cat /proc/sys/vm/dirty_ratio
cat /proc/sys/vm/dirty_background_ratio
cat /proc/sys/vm/dirty_expire_centisecs

Valores óptimos para NVMe:

10
5
1000 (esto equivale a 10 segundos)
echo 'vm.dirty_ratio=10' | sudo tee -a /etc/sysctl.d/99-ml-workstation.conf
echo 'dirty_background_ratio=5' | sudo tee -a /etc/sysctl.d/99-ml-workstation.conf
echo 'vm.dirty_expire_centisecs=1000' | sudo tee -a /etc/sysctl.d/99-ml-workstation.conf

Entre ejecuciones de lmstudio, se recomienda limpiar el caché de memoria:

sync && echo 2 | sudo tee /proc/sys/vm/drop_caches