# libtmux.Runtime:with_server

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Lua
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-lua/blob/ec2c4d710bcec2bd54c1ded405e6652e873b0994/lua/libtmux/_internal/runtime.lua#L1061
- **Page:** https://libtmux.org/en/lua/latest/reference/libtmux-runtime-with_server/

```
with_server: fun(
  self: libtmux.Runtime,
  options: libtmux.StartServerOptions,
  body: fun(
    server: libtmux.Server,
    owned: libtmux.OwnedServer,
  ):<T>?, libtmux.Error?,
):libtmux.Request<T>
```

Starts a private server, runs `body(server, owned)` and stops the server
 however the body ends. The request resolves to what the body returns.

## Parameters

- `options` (`libtmux.StartServerOptions`):  Options for `libtmux.Runtime:start_server`.

 `binary` is the only required field. The server lives in a fresh directory
 under `root`, with its socket inside, so `root` plus about 45 bytes must fit
 a socket path (`sun_path`: 107 bytes on Linux, 103 on macOS and the BSDs);
 a longer root fails with `invalid_endpoint` before anything is created. The
 directory holds the daemon's `TMUX_TMPDIR`, so a tmux client that names no
 socket cannot reach another server from inside it.

 The daemon inherits the caller's environment less `TMUX`, `TMUX_PANE` and
 every `LIBTMUX_SOCKET*` variable, plus `environment`. `environment` cannot
 set those names or `TMUX_TMPDIR`. The daemon runs with `exit-empty off`, so
 it stays up with no session.
- `body` (`fun(server: libtmux.Server, owned: libtmux.OwnedServer):<T>?, libtmux.Error?`)
  Default: `nil`.

## Examples

Run a body on a private tmux server that stops when the body ends. Use Lua 5.1 or newer with LuaRocks, tmux 3.3a or newer, and a C compiler and CMake for luv on Linux. In a new directory, install the library and its standalone runtime dependency from LuaRocks:

```console
$ luarocks --tree ./rocks install luv &&
  luarocks --tree ./rocks install libtmux &&
  eval "$(luarocks --tree ./rocks path)"
```

Save the complete program as `with_server.lua`. It starts its own private tmux server, uses it and stops it, so no existing session is needed.

[Source example](https://github.com/libtmux/libtmux-lua/blob/ec2c4d710bcec2bd54c1ded405e6652e873b0994/examples/api/with_server.lua).

```lua
local adapter = require("libtmux.runtime.luv")

local function must(value, err)
    if err ~= nil then
        error(tostring(err), 0)
    end
    return value
end

-- tmux is /usr/bin/tmux unless TMUX_BIN names another absolute path.
local binary = os.getenv("TMUX_BIN") or "/usr/bin/tmux"

must(adapter.run(function(runtime)
    -- The server stops when the body returns, fails or raises.
    local count = must(runtime
        :with_server({ binary = binary }, function(server)
            local session = { name = "demo", argv = { "/bin/cat" } }
            must(server:new_session(session):await())
            local snapshot = must(server:snapshot():await())
            return #snapshot.sessions
        end)
        :await())
    print("sessions: " .. count)
    return true
end))
```

Run the saved program. It runs `/usr/bin/tmux`; set `TMUX_BIN` to another absolute path to use a different binary. Assertions and operation errors produce a failing exit status:

```console
$ eval "$(luarocks --tree ./rocks path)" &&
  lua with_server.lua
```

## Guides

Pages that discuss `with_server`.

- [Getting started](/en/lua/latest/guides/overview/)
- [Runtime ownership](/en/lua/latest/guides/runtime/)
