Running Livebook as a Systemd Service for Your User

Running Livebook as a Systemd Service for Your User

Table of Contents

Livebook is a powerful tool for creating and sharing interactive notebooks with Elixir. To make it even more convenient, you can set it up to run as a systemd service for your user. This ensures that Livebook starts automatically whenever you log in, and runs in the background without requiring root permissions. Here’s a step-by-step guide to help you get started.

Step 1: Install Livebook

First, ensure you have Livebook installed. You can install it via Elixir’s package manager, mix, by running:

mix escript.install hex livebook

Step 2: Create a User Systemd Service File

Systemd allows users to manage their own services. User-specific service files are stored in ~/.config/systemd/user/.

mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/livebook.service

Step 3: Define the Service Configuration

Add the following content to the livebook.service file (replace USER and PASSWORD):

[Unit]
Description=Livebook Service

[Service]
Type=simple
ExecStart=/home/__USER__/.asdf/shims/livebook server
Restart=on-failure
Environment=HOME=%h
Environment=MIX_ENV=prod
Environment=PATH=/home/USER/.asdf/shims:/usr/local/bin:/usr/bin:/bin
Environment=LIVEBOOK_HOME=%h/livemd
Environment=LIVEBOOK_PASSWORD=__PASSWORD__
Environment=LIVEBOOK_PORT=8090
Environment=LIVEBOOK_IFRAME_PORT=8091


[Install]
WantedBy=default.target

Explanation:

  • ExecStart: The command to start Livebook. Ensure the path to the Livebook binary is correct.
  • Environment: Sets necessary environment variables. %h will expand to the user’s home directory.

Step 4: Reload User Systemd Configuration

After creating the service file, reload the systemd user configuration.

systemctl --user daemon-reload

Step 5: Enable the Service for Your User

Enable the service to start on user login.

systemctl --user enable livebook.service

Step 6: Start the Service for Your User

Start the Livebook service.

systemctl --user start livebook.service

Step 7: Check the Service Status

Verify that the service is running.

systemctl --user status livebook.service

Step 8: Enable User Systemd Services on Boot

Ensure that user services are started automatically at boot. You can enable lingering for your user:

sudo loginctl enable-linger USER

Replace USER with your actual username.

Conclusion

By following these steps, you can set up Livebook to run as a systemd service for your user. This setup ensures that Livebook starts automatically when you log in and runs smoothly in the background. Enjoy your interactive notebooks with the convenience of a managed service!

comments powered by Disqus

Related Posts

Phoenix Live Dashboard Custom Page

Phoenix Live Dashboard Custom Page

Today I played with custom live dashboard pages, That’s a short documentation how to use it. We will implement a page that shows the currently running system processes on the box running our elixir system.

Read More
Livebook on Steam Deck

Livebook on Steam Deck

This post may be updated in the future.

Steam deck is a modern machine. I wanted to test how long it takes to generate images using stable diffusion on it. Thanks to livebook this becomes trivial. The biggest problem is to install the package. I did it using ssh but it can also be done directly on deck.

Read More
Machine Learning on Ryzen 7840HS

Machine Learning on Ryzen 7840HS

I bought an AMD laptop with 7840hs and 32GB RAM with the hope that I can play with machine learning models. At the beginning it was problematic, I could not get anything bigger to work but after some time spent reading and experimenting here are some of my findings.

Read More