In this tutorial, you will discover how to install and configure a Grafana instance for your Ubuntu server.

This new version presented a whole range of different features: a new panel editor, a new explore function as well as new plugins and tutorials for beginners.

Prerequisite

  • Linux servers running Ubuntu 20.04 or 20.10
  • root privileges
  • 3 GB or more of Ram
  • 2 CPUs or 4 CPUs
  • Docker engine running on target host – Can be local or remote
  • Linux, macOS or Windows docker host machine
  • Internet connection to download Portainer docker image

This article focuses on installing the latest version of Grafana on an Ubuntu 20.04 server.

Goals

Install a Grafana is a free and open source web console.

  • How to install Grafana 7.4.0
  • Access Grafana UI

Let's go to the code!

Step 1 - Volume for Grafana on Docker

Create a volume for Grafana with the following command:

#

docker volume create grafana_data

Step 2 - Run the command

After the volume has been created, run the command below on your terminal:

#

run -itd -p 3000:3000 --name=grafana --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v grafana_data:/data ubuntu:20.04

#

docker ps

CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS         PORTS                    NAMES
75a7b7075c49   ubuntu:20.04   "/bin/bash"   4 seconds ago   Up 3 seconds   0.0.0.0:3000->3000/tcp   grafana

Step 3 - Add the Grafana repositories to your server

First of all, you need to add the Grafana APT repositories in order to be able to install packages from them.

If you already have Grafana repositories, you can skip this section and go to the next one.

First, install packages that are needed for Grafana to run properly : apt-transport and software-properties-common.

#

sudo apt install -y apt-transport-https

#

sudo apt install -y software-properties-common wget

Step 4 - Add the GPG key to the trusted keys

In order to retrieve Grafana packages in a secure way, you need to add the GPG key to your trusted set of keys.

To achieve that, you need to use the apt-key command.

#

wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -

OK

A simple “OK” confirmation should be displayed right after the command.

Step 5 - Add the trusted Grafana repositories

Now that everything is configured, you can add the Grafana repositories to the custom APT repositories of your server.

#

echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

deb https://packages.grafana.com/oss/deb stable main

Awesome, now you can update your distribution packages and verify that it links to the Grafana repositories.

To install Grafana, use the “apt install” command.

#

sudo apt install grafana

Congratulations, you successfully installed the new Grafana v7.4.0 on your Ubuntu machine!

However, by default, your Grafana server is not started. You will have to configure it and start it manually, which is the purpose of the following sections.

Step 6 - Inspect your grafana-server systemd service

If you are using systemd, Grafana created for you a service called “grafana-server“.

To make sure of it, you can run the following command.

#

sudo ls /usr/lib/systemd/system/grafana-server.service

cat /usr/lib/systemd/system/grafana-server.service
[Unit]                                                                                          
Description=Grafana instance                                                                    
Documentation=http://docs.grafana.org                                                           
Wants=network-online.target                                                                     
After=network-online.target                                                                     
After=postgresql.service mariadb.service mysql.service                                          
                                                                                                
[Service]                                                                                       
EnvironmentFile=/etc/default/grafana-server                                                     
User=grafana                                                                                    
Group=grafana                                                                                   
Type=simple                                                                                     
Restart=on-failure                                                                              
WorkingDirectory=/usr/share/grafana                                                             
RuntimeDirectory=grafana                                                                        
RuntimeDirectoryMode=0750                                                                       
ExecStart=/usr/sbin/grafana-server                                                  \           
                            --config=${CONF_FILE}                                   \           
                            --pidfile=${PID_FILE_DIR}/grafana-server.pid            \           
                            --packaging=deb                                         \           
                            cfg:default.paths.logs=${LOG_DIR}                       \           
                            cfg:default.paths.data=${DATA_DIR}                      \           
                            cfg:default.paths.plugins=${PLUGINS_DIR}                \           
                            cfg:default.paths.provisioning=${PROVISIONING_CFG_DIR}              
                                                                                                
                                                                                                
LimitNOFILE=10000                                                                               
TimeoutStopSec=20                                                                               
UMask=0027   
[Install]
WantedBy=multi-user.target 

It is quite important for you to inspect this file, as it provides many information about the server that you just installed.

From the file, you understand that :

  • The Grafana server binary is located at /usr/sbin/grafana-server.
  • The file that defines all the environment variables is located at /etc/default/grafana-server
  • The configuration file is given via the CONF_FILE environment variable.
  • The PID of the file is also determined by the PID_FILE_DIR environment variable.
  • Logging, data, plugins and provisioning paths are given by environment variables.

The content of the environment file is the following one :

GRAFANA_USER=grafana

GRAFANA_GROUP=grafana

GRAFANA_HOME=/usr/share/grafana

LOG_DIR=/var/log/grafana

DATA_DIR=/var/lib/grafana

MAX_OPEN_FILES=10000

CONF_DIR=/etc/grafana

CONF_FILE=/etc/grafana/grafana.ini

RESTART_ON_UPGRADE=true

PLUGINS_DIR=/var/lib/grafana/plugins

PROVISIONING_CFG_DIR=/etc/grafana/provisioning

# Only used on systemd systems
PID_FILE_DIR=/var/run/grafana

Step 7 - Start your grafana-server service

Now that you have learnt about the Grafana configuration variables and how you can arrange your Grafana server, it is time to launch your service.

In order to start your service, you need to execute the “systemctl start” command on the “grafana-server” service.

#

sudo systemctl start grafana-server

#

sudo systemctl status grafana-server

If your service is set as “Active” and “Loaded” (meaning that it will be started on launch), you should be good to go!

Now that everything is configured, we are going to head over to the Web UI in order to create your first dashboard.

Step 8 - Launch Grafana v7 Web UI

To open Grafana, you need to open a web browser and navigate to http://your_server_ip:3000

As a reminder, Grafana works on port 3000 by default, but it may be different if you are already using this port.

You will be presented with this screen when launching the application for the first time.

By default, the login for Grafana is “admin” and the default password is also “admin“.

You will be asked to change your password via a custom chosen password or a strong generated one.

Choose a password and click on “Submit“.

You should now be presented with the default screen for Grafana v7.0, well different from the v6.2 one!

If you are new to Grafana, you should probably follow the tutorial showcased on the “Welcome page“.

Step 9 - Disable new user registrations

The account that you just created will be used as an administrator account for your server.

However, in some cases, you want to be the only one responsible for new user registrations on your server.

That’s why you should think about disabling new user registrations.

By default, user registration is available at http://your_server_ip:3000/signup.

To disable user registration, head back to the configuration file and disable the following section.

#

sudo vi /etc/grafana/grafana.ini

### Content of grafana configuration file

[users]
# disable user signup / registration
;allow_sign_up = true

# Allow non admin users to create organizations
;allow_org_create = true

# Set to true to automatically assign new users to the default organization (id 1)
;auto_assign_org = true

Change the allow_sign_up setting to false, and restart your Grafana server.

;allow_sign_up = false
#

sudo systemctl restart grafana-server

Now you can test that user registration is correctly disabled on your server.

Note that the page will still be accessible but you will be prompted with an error message when you try to create a new user.

Awesome, you successfully disabled user registration on your server!

Step 10 - Start monitoring targets with Grafana

Using Grafana alone is not very useful but you can connect it to many different data sources.

In Grafana, data sources are defined as plugins that one can install in order to connect to it.

On modern architectures, one can connect to cloud datasources like Cloudwatch or Amazon Timestream.

Step 11 - Create Your First Dashboard

In order to create all those monitoring dashboards, you will need to create a simple dashboard first.

See more Dashboards

Conclusion

In this article, we have explained the installation of the Grafana on Ubuntu 20.04.

If you have any questions about installing and configuring Grafana on Ubuntu, please contact us in the comments.

Read more about: Data ManagementUbuntu