Simple Steps to Run Go Application in Ubuntu

Praditya Adhitama
The Startup
Published in
3 min readJun 2, 2020
Photo by Yomex Owo on Unsplash

Fellas, this post is about how to run Go application in the background with Ubuntu server. To do so, we could use the built in service in Ubuntu called systemd.

Let’s try to take a look at it briefly. System Daemon or so called systemd is a suite of basic building blocks for a Linux system. As described by Linode,

systemd is a Linux initialization system and service manager that includes features like on-demand starting of daemons, mount and automount point maintenance, snapshot support, and processes tracking using Linux control groups. systemd provides a logging daemon and other tools and utilities to help with common system administration tasks.

In a nutshell, systemd provides us a tool to manage services that are ran within the linux system. It gives us a control point to start, monitor, restart, delete and log services. With this simplification, we can implement it to Go application.

Now, let’s think of a Go application as an individual service that is running within a Linux System. Following this perspective in mind, we can implement it with these 3 simple steps:

1. Write / Clone and Build our Go program

First we need to have our Go program ready. We could write or clone it from Git repository. For example, it could be a Go package or Go module. The most important part in this step is to create the binary file to be ready for execution by systemd. We could do this by first change the directory to where we have our main.go file (or any directory where we have the go program that we want to run with systemd) and running this command on the terminal

go build

If it succeed a new binary file will be created and it will be named the same as the directory name.

2. Create System Service file

Next, let’s create a service file for the systemd. This service file serves as a guidance that tells systemd about the location of the execution file, user, and other required configuration. To do this, using nano (or vim whichever you prefer) we create a goapp.service through the command below.

nano /lib/systemd/system/goapp.service

and within the goapp.service, lets write the configuration that we want to use for our go program. we could use the code below for it.

[Unit]
Description=simple go application

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/path/to/binary/file

[Install]
WantedBy=multi-user.target

The code snippet above contains basic information about: the description of our service; how to handle for the server restart; and the most-important-part, the execution path (written as “ExecStart”). This is the path to locate our built binary file. The path should end with the name of the binary file and not only the directory where we put it. For example, supposed we have the binary file named goapp in the goapp directory within the home folder. In that case the path should be “/home/goapp/goapp”.

3. Start the service with system service command

Finally, we could start or go app service with this command

service goapp start

we could restart, enable the service to be run when the user logged in, and see the status of the service with these commands sequentially

service goapp restart
service goapp enable
service goapp status

Notice that the name of the service that we call is the same with the service file name that we created before. So that is how we call our service in the terminal.

Conclusion

That’s it. 3 easy steps to have our go program running on the background in Ubuntu. This is the simplest way to deploy our go program in a regular server without docker or hosting service. This steps can also be used in other UNIX-based OS. Cheers!

--

--