Installing Dgraph on Ubuntu — The Simple Way
It was a year back that I started using Dgraph and now I would like to share my experiences with you. At first, coming from a phase where I used GraphQL a lot, Dgraph’s GraphQL+- felt quite similar.
But as I tried to bring it to production, that’s where things started messing up. To be honest Dgraph does have a learning curve, and trust me every effort you put in is worth it.
The database itself if very scalable and robust even if the data size is large and realtime. The community is becoming stronger each day and the members are very helpful. Thanks to .
Dgraph is an open source, scalable, distributed, highly available and fast graph database, designed from ground up to be run in production.- docs.dgraph.io
But first things first!! How to get started?
Let’s walk you through a simple tutorial to get Dgraph up and running on your Ubuntu 16.04|18.04 LTS machine. Please follow along as it will only take a few minutes.
Setting Up
Before starting let’s get the basic requirements installed on your machine. Run the following commands:
sudo apt update
sudo apt install curl apt-transport-httpsNow that we have everything required under the belt. Let’s get started!!
Installing Dgraph
To install Dgraph we need to fetch a script that has already been drafted by the Dgraph team. We do that by running the below command:
curl https://get.dgraph.io -sSf | bash**When prompted agree to the Licence and Agreements.
On success you should see a similar message:
Download complete.
Inflating binaries (password may be required).
Dgraph binaries v1.0.11 have been installed successfully in /usr/local/bin.
Please visit https://docs.dgraph.io/get-started for further instructions on usage.The process installs Dgraph binaries at the location /usr/local/bin/. Make sure you have it on your $PATH.
Now we have Dgraph installed on your machine and is ready to run too. But wait, there’s a catch..
There is now a way to run it as a service so once you opt out of the process everything stops.
Don’t worry we’ve got your back. Let create some services that will help run Dgraph more efficiently and will also help to monitor it.
Okay before creating the services we need to create a system user to specifically manage our Dgraph database services.
groupadd --system dgraph
sudo useradd --system -d /var/lib/dgraph -s /bin/false -g dgraph dgraphWe also need to create a few directories where all our data and logs will be stored. So let create them.
**Please note that we need to create these directories in place that is a little isolated as the files that they hold are very important and if mishandled may lead to the corruption of the complete database.
mkdir -p /var/log/dgraph
mkdir -p /var/lib/dgraph/{p,w,zw}
chown -R dgraph:dgraph /var/{lib,log}/dgraphNow that all the directories are created, we need to create the services one by one. There are three services in total.
Here’s the first one — dgraph-alpha.service
To create its first run:
sudo nano /etc/systemd/system/dgraph-alpha.serviceAnd paste the following:
[Unit]
Description=dgraph.io data server
Wants=network.target
After=network.target dgraph-zero.service
Requires=dgraph-zero.service
[Service]
Type=simple
ExecStart=/usr/local/bin/dgraph alpha --lru_mb 2048 -p /var/lib/dgraph/p -w /var/lib/dgraph/w
StandardOutput=journal
StandardError=journal
User=dgraph
Group=dgraph
[Install]
WantedBy=multi-user.target
EOFThe second — dgraph-zero.service
Similarly, to create its first run:
sudo nano /etc/systemd/system/dgraph-zero.serviceAnd paste in the following:
[Unit]
Description=dgraph.io zero server
Wants=network.target
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/dgraph zero --wal /var/lib/dgraph/zw
StandardOutput=journal
StandardError=journal
User=dgraph
Group=dgraph
[Install]
WantedBy=multi-user.target
RequiredBy=dgraph.service
EOFAnd finally the third one — dgraph-ratel.service
Again run:
sudo nano /etc/systemd/system/dgraph-ratel.serviceAnd paste the following:
[Unit]
Description=dgraph.io UI server
Wants=network.target
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/dgraph-ratel
StandardOutput=journal
StandardError=journal
User=dgraph
Group=dgraph
[Install]
WantedBy=multi-user.target
EOFWoah!! that was a lot to take in. Really?
Nah!! Just three simple services and that’s all we need to fire up the Dgraph database.
Why wait? Let do it... Run the following commands:
sudo systemctl daemon-reload
sudo systemctl enable --now dgraph-alpha
sudo systemctl enable --now dgraph-ratelThat should do the trick! If not try running the below command:
sudo systemctl start dgraph-alpha dgraph-zero dgraph-ratelTo check the status run:
sudo systemctl status dgraph-ratel dgraph-zero dgraph-alphaYou should see the status of the services as:
● dgraph-ratel.service - dgraph.io UI server
Loaded: loaded (/etc/systemd/system/dgraph-ratel.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2020-01-30 17:56:37 UTC; 3s ago
Main PID: 18591 (dgraph-ratel)
Tasks: 4
Memory: 2.3M
CPU: 7ms
CGroup: /system.slice/dgraph-ratel.service
└─18591 /usr/local/bin/dgraph-ratelJan 30 17:56:37 instance-1 systemd[1]: Started dgraph.io UI server.
Jan 30 17:56:38 instance-1 dgraph-ratel[18591]: 2020/01/30 17:56:38 Listening on :8000...an 30 17:56:38 instance-1 dgraph-ratel[18591]: 2020/01/30 17:56:38 Listening on :8000...● dgraph-zero.service - dgraph.io zero server
Loaded: loaded (/etc/systemd/system/dgraph-zero.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2020-01-30 17:56:37 UTC; 3s ago
Main PID: 18600 (dgraph)
Tasks: 46
Memory: 14.4M
CPU: 440ms
CGroup: /system.slice/dgraph-zero.service
└─18600 /usr/local/bin/dgraph zero --wal /var/www/dgraphData/run/dgraph/zw● dgraph-alpha.service - dgraph.io data server
Loaded: loaded (/etc/systemd/system/dgraph-alpha.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2020-01-30 17:56:37 UTC; 3s ago
Main PID: 18601 (dgraph)
Tasks: 35
Memory: 27.1M
CPU: 437ms
CGroup: /system.slice/dgraph-alpha.service
└─18601 /usr/local/bin/dgraph alpha --lru_mb 1024 -p /var/www/dgraphData/run/dgraph/p -w /var/www/dgraphData/run/dgraph/w
Accessing the web interface
Let’s confirm by accessing the web interface:
Open this URL on the browser:
You should see something like:
Conclusion
Congratulations! You’re all set to start working with Dgraph.
For more please visit the Dgraph homepage.
Find the official installation docs here.
You can also check out an amazing JavaScript ORM by here.
Not everything can fit in rows and columns — Dgraph.io
If you liked what you read do clap and please comment if you have any questions.
