DEV Community

Alexander Alemayhu
Alexander Alemayhu

Posted on

Run Deno apps via Systemd on Linux

Last year around summertime I got to play around with Deno and it was lots of fun πŸ˜€ I mostly made simple server scripts to serve up static sites. The most Deno code I wrote was rewriting my dotfiles from Ruby to Typescript.

It was interesting and soon I would like to try out more things so this post is for myself and anyone else out there who wants to deploy Deno on a traditional Linux distribution like Ubuntu or some other Linux distro.

Heads up as of this writing, the current version of Deno is v0.40.0. The project is not yet considered production-ready so keep that in mind when you want to expose your app to the internet. If you are receiving traffic on the internet there might be bugs that people could exploit.

What is Deno?

Deno is a new Typescript / Javascript runtime developed by Ryan Dahl and others. There are several interesting things with the project but a few that stick out are; the security model is different where you have to explicitly allow things like file system access, networking, etc. Deno scripts are written in Typescript. You can read more about Deno on their website. YouTube has several talks on the subject when you search for deno js. Searching for just deno might not work since a rapper uses that name 😜

In this setup, I will just assume you are using some proxy server and will only focus on the systemd specific bits. Depending on what kind of application you are running you might want to consult the full systemd documentation. I also recommend running the app as a dedicated user this way you can restrict it even more (please don't use root πŸ˜…).

Installing Deno

Before we actually move on to installing Deno, let's get curl and unzip. curl is used to get the install script and unzip is used when deno is downloading dependencies (I think πŸ™ƒ).

sudo apt-get update
sudo apt-get install -y curl unzip
Enter fullscreen mode Exit fullscreen mode

The easiest way to get Deno on your server is the official install script

curl -fsSL https://deno.land/x/install/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

Adding the Service file

Great, now that you have Deno in place, you need to tell systemd about the application. So first create the service file we will be using, just replace my-cool-thing with your app name.

sudo touch /etc/systemd/system/my-cool-thing.service
Enter fullscreen mode Exit fullscreen mode

Use this template to set the values (change out the relevant fields or delete)

[Unit]
Description=my-cool-thing.io - doing cool things
Documentation=https://my-cool-thing.io/docs
After=network.target

[Service]
Type=simple
User=my-cool-thing-user
WorkingDirectory=/home/my-cool-thing-user/code
ExecStart=/home/my-cool-thing-user/.deno/bin/deno run --allow-read --allow-net my-cool-thing-main.ts
Restart=on-failure

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Then enable and start it

sudo systemctl enable my-cool-thing
sudo systemctl start my-cool-thing
Enter fullscreen mode Exit fullscreen mode

Bam, now you have your service all wired up and ready.

Troubleshooting

If you end up with issues, check the logs

systemctl status
Enter fullscreen mode Exit fullscreen mode

One common mistake I have seen is forgetting to pass the right permissions, but those are easy to spot by manually running the start command

deno run my-cool-thing-main.ts
Enter fullscreen mode Exit fullscreen mode

I’m Alexander and work as a Web Developer at Scrimba - The mind-blowing way to learn how to code. This post is part of my personal weekly challenge of writing 256 words per week to improve my writing on technical topics.

I hope you found it useful. Thank you for reading.

Discussion (0)