Reading Time: 10 minutes
neo4j

In this tutorial, we will examine and discuss what the Neo4j graph database is, what it is used for, and how it is implemented. In the sections that follow, we will install Neo4j on an Ubuntu 20.04 server, and then we configure the database software appropriately. However, let's first review some important database concepts that apply to Neo4j.

Please be aware of the that fact the Neo4j database platform is classified as a NoSQL database management system (DBMS). See our related article, What is NoSQL and How is it Utilized? for a better understanding of how the NoSQL category of databases can be leveraged for your website.

What is a Graph Database?

Neo4j is a graph database management system (DBMS) used to create and search through data structures with complex data relationships. Other examples of graph databases (GDBs) include:

  • ArangoDB
  • Grakn Core
  • Microsoft SQL Server 2017
  • OrientDB
  • RedisGraph

Of all the existing graph databases in the market, the Neo4j graph database platform (often referred to as a GDB or graph DBMS platform) has the highest ranking and has been recognized as a leader in the cloud DBMS space by Gartner.

What is the Neo4j Graph Database Platform?

Neo4j connects data as stored, allowing us to run queries we never knew or thought about. To put it simply, Neo4j records the relationship between data nodes, whereas conventional relational databases use columns and rows to store structured data. Since each node stores references to all the other nodes to which it is connected, Neo4j can encode and query complex relationships with minimal overhead.

About the Creators of the Neo4j Graph Database — Neo Technology and Neo4j, Inc.

The original Neo Technology company is now part of Neo4j, Inc. — an umbrella company. They are the creators and developers of the open-source Neo4j graph database software. The company has been developing it since 2003. It is written in Java and Scala, and the source code is freely available on GitHub. As of 2023, it is considered the most utilized graph DBMS in use today. Neo4j employs its own query language called Cypher, but queries can also be written in other styles, for example, through the Java API.

Prerequisites

For this installation, the software requires the following baseline settings:

  • 8 GB RAM and a four-core server is preferred, but at a minimum, the recommended use is 1 GB RAM and a single-core server.
  • Ubuntu 20.04 operating system (OS)
  • All commands are run as root — aa regular user, commands must be prefaced by the sudo command.

8 Steps to Install the Neo4j Graph Database on Ubuntu 20.04

Step #1: Update the System and Add Repository

This tutorial will take you through the installation of Neo4j on Ubuntu 20.04.

1a. Update the System

Ubuntu does not officially contain Neo4j in the standard package repository. We will add the package source pointing to the location of the Neo4j repository, then add the GnuPG or Gnu Privacy Guard (GPG) key from Neo4j for verification, then install Neo4j itself.

We start by updating the list of packages and the packages themselves:

root@host:~# apt update && apt -y upgrade

1b. Add Additional Software

In this step, we will install an additional package needed for HTTPS connections. This application package may be already installed by default on the system, but it still needs to be updated:

root@host:~# apt install apt-transport-https ca-certificates curl software-properties-common -y

The apt-transport-https package enables the usage of HTTPS via the package manager using the libapt-pkg library. This keeps the installation secure.

1c. Verify Security Key

Now we add the official security key for the Neo4j package repository. This key check and verifies what you are installing is from the official repository:

 root@host:~# curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | apt-key add -
 OK
 root@host:~#  

1d. Add Repository

Add the official Neo4j repository to the Extra Packages for Enterprise Linux (EPEL) package manager list:

root@host:~# add-apt-repository "deb https://debian.neo4j.com stable 4.1"

Step #2: Install the Neo4j Graph Database

When installing Neo4j and all its dependencies, it is essential to note that the installation process will prompt you to install Java packages to work with Neo4j. During installation, press the Y key to accept the installing of Java software. If you already have the appropriate Java components installed, the installer will detect them and skip this step:

root@host:~# apt install neo4j -y
 Reading package lists... Done
 Building dependency tree       
 Reading state information... Done
 The following package was automatically installed and is no longer required:
   libfprint-2-tod1
 Use 'sudo apt autoremove' to remove it.
 The following additional packages will be installed:
   cypher-shell
 The following NEW packages will be installed:
   cypher-shell neo4j
 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
 Need to get 116 MB of archives.
 After this operation, 131 MB of additional disk space will be used.
 Get:1 https://debian.neo4j.com stable/4.1 amd64 cypher-shell all 4.1.3 [27,1 MB]
 Get:2 https://debian.neo4j.com stable/4.1 amd64 neo4j all 1:4.1.5 [88,4 MB]
 Fetched 116 MB in 10s (11,1 MB/s)                                              
 Preconfiguring packages ...
 Selecting previously unselected package cypher-shell.
 (Reading database ... 163626 files and directories currently installed.)
 Preparing to unpack .../cypher-shell_4.1.3_all.deb ...
 Unpacking cypher-shell (4.1.3) ...
 Selecting previously unselected package neo4j.
 Preparing to unpack .../neo4j_1%3a4.1.5_all.deb ...
 Unpacking neo4j (1:4.1.5) ...
 Setting up cypher-shell (4.1.3) ...
 Setting up neo4j (1:4.1.5) ...
 Processing triggers for man-db (2.9.1-1) ...
 Processing triggers for systemd (245.4-4ubuntu3.3) ...
 root@host:~#  

Step #3: Start the Neo4j Service

Once we have it installed, we need to enable it as a neo4j.service service:

 root@host:~# systemctl enable neo4j.service
 Synchronizing state of neo4j.service with SysV service script with /lib/systemd/systemd-sysv-install.
 Executing: /lib/systemd/systemd-sysv-install enable neo4j
 Created symlink /etc/systemd/system/multi-user.target.wants/neo4j.service → /lib/systemd/system/neo4j.service.
 root@host:~#  

Step #4: Check Neo4j Status

Next, we verify that everything is working as expected:

 root@host:~# systemctl status neo4j.service
  neo4j.service - Neo4j Graph Database
      Loaded: loaded (/lib/systemd/system/neo4j.service; enabled; vendor preset:>
      Active: active (running) since Wed 2020-12-23 20:04:44 +03; 2min 4s ago
    Main PID: 4827 (java)
       Tasks: 52 (limit: 9489)
      Memory: 447.9M
      CGroup: /system.slice/neo4j.service
              └─4827 /usr/bin/java -cp /var/lib/neo4j/plugins:/etc/neo4j:/usr/sh>
 сне 23 20:04:46 host neo4j[4827]: 2020-12-23 17:04:46.101+0000 INFO  ======== N>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.073+0000 INFO  Initializi>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.077+0000 INFO  Setting up>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.077+0000 INFO  Creating n>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.083+0000 INFO  Setting ve>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.085+0000 INFO  After init>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.088+0000 INFO  Performing>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.197+0000 INFO  Bolt enabl>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.843+0000 INFO  Remote int>
 сне 23 20:04:47 host neo4j[4827]: 2020-12-23 17:04:47.844+0000 INFO  Started.
 lines 1-19/19 (END)
 To exit this screen, press Ctrl + C.
 It is important to have the following parameters:
 Loaded: loaded (/lib/systemd/system/neo4j.service; enabled; vendor preset:>
  Active: active (running) since Wed 2020-12-23 20:04:44 +03; 2min 4s ago 

Step #5: Test the Database Connection

Please remember to review the principles involved with Neo4j as an open source project.

Since we installed Neo4j and started it as a service, we will test the database connection and configure the admin user:

Note:
We are using the free, Neo4j Community Edition version. It supports simultaneous work with the same database, but does not include assigning roles and permissions to users.

Step #6: Connecting to Neo4j

6a. Working with Neo4j the Neo4j Graph Database

To interact with the database, we will launch the internal utility called cypher-shell to work with Neo4j. When we first run it, we will be asked to enter a user and password. By default, the username is neo4j, and the password is neo4j. After the first login, you will be prompted to change the password to one of your choosing.

 oot@host:~# cypher-shell
 username: neo4j
 password: *****
 Password change required
 new password: ********
 Connected to Neo4j 4.1.0 at neo4j://localhost:7687 as user neo4j.
 Type :help for a list of available commands or :exit to exit the shell.
 Note that Cypher queries must end with a semicolon.
 neo4j@neo4j>  

To exit, use the exit command.

 neo4j@neo4j> exit
 Bye!
 root@host:~#  

6b. Adding Nodes

Let's set up some sample nodes and define relationships between them. Connect using Cypher:

 root@host:~# cypher-shell
 username: neo4j
 password: ********
 Connected to Neo4j 4.1.0 at neo4j://localhost:7687 as user neo4j.
 Type :help for a list of available commands or :exit to exit the shell.
 Note that Cypher queries must end with a semicolon.
 neo4j@neo4j>  

Next, let's add a node called Liquidweb and the names of colleagues who work for this company under the name Margaret. We can do this using the CREATE command, and the syntax will be as follows:

 neo4j@neo4j> CREATE (:Liquidweb {name: 'Margaret'});
 0 rows available after 36 ms, consumed after another 0 ms
 Added 1 nodes, Set 1 properties, Added 1 labels
 neo4j@neo4j>  

6c. Adding Users

Let's add a few more colleagues and link them to the company they work for, Liquidweb. We will connect using the FRIEND command:

 neo4j@neo4j> CREATE (:Liquidweb {name: 'John'})-[:FRIEND]->
              (:Liquidweb {name: 'Peter'})-[:FRIEND]->
              (:Liquidweb {name: 'Chris'});
 0 rows available after 38 ms, consumed after another 0 ms
 Added 3 nodes, Created 2 relationships, Set 3 properties, Added 3 labels
 neo4j@neo4j>

6d. Creating Relationships

Since Peter and Chris work in the same department and have the same properties as nodes, we will create a relationship with the name column:

 neo4j@neo4j> MATCH (a:Liquidweb),(b:Liquidweb)
              WHERE a.name = 'Peter' AND b.name = 'Chris'
              CREATE (a)-[r:DEPARTMENT { name: 'Developers' }]->(b)
              RETURN type(r), r.name;
 +------------------------+
 | type(r) | r.name       |
 +------------------------+
 | "DEPARTMENT" | "Developers" |
 +------------------------+
 1 row available after 105 ms, consumed after another 10 ms
 Created 1 relationships, Set 1 properties
 neo4j@neo4j> 
  • MATCH — This indicates the correspondence of the nodes. In this case, within one company Liquidweb.
  • WHERE — between values.
  • CREATE — create and add.
  • RETURN — return to the base.

Now create a connection between John and Chris, although they are in different departments, but they are working on the same project.neo4j@neo4j> MATCH (a:Liquidweb),(b:Liquidweb):

              WHERE a.name = 'John' AND b.name = 'Chris'
              CREATE (a)-[r:PROJECT { name: 'Cool Project' }]->(b)
              RETURN type(r), r.name;
 +----------------------------+
 | type(r)   | r.name         |
 +----------------------------+
 | "PROJECT" | "Cool Project" |
 +----------------------------+
 1 row available after 48 ms, consumed after another 5 ms
 Created 1 relationships, Set 1 properties
 neo4j@neo4j>  

6e. Displaying All Information

We will display all this data and their relationships using the following query:

 neo4j@neo4j> Match (n)-[r]->(m)
              Return n,r,m;
 +--------------------------------------------------------------------------------------------------+
 | n                            | r                                  | m                            |
 +--------------------------------------------------------------------------------------------------+
 | (:Liquidweb {name: "John"})  | [:FRIEND]                          | (:Liquidweb {name: "Peter"}) |
 | (:Liquidweb {name: "John"})  | [:PROJECT {name: "Cool Project"}]  | (:Liquidweb {name: "Chris"})  |
 | (:Liquidweb {name: "Peter"}) | [:DEPARTMENT {name: "Developers"}] | (:Liquidweb {name: "Chris"})  |
 | (:Liquidweb {name: "Peter"}) | [:FRIEND]                          | (:Liquidweb {name: "Chris"})  |
 +--------------------------------------------------------------------------------------------------+
 4 rows available after 17 ms, consumed after another 2 ms
 neo4j@neo4j>  

6f. Exiting the Cypher Shell

We received the output data with the following FRIEND relationships, which show the relationship and the following data relationships between DEPARTMENT and PROJECT.

To exit the Cypher shell, run the exit command:

 neo4j@neo4j> :exit
 Bye!
 root@host:~#  

6g. Setting Up a Secure Remote Connection to the Neo4j Graph Database

We will not always be able to connect to the database from the server itself. If we want to configure the application to use Neo4j, we will need to configure it to securely connectto other servers. Also, we should configure the firewall to restrict which servers can attach to Neo4j.

By default, Neo4j connects via localhost (127.0.0.1 - localhost) intended for testing applications without working with other servers). Also, the work of Neo4j from localhost will not be open for public access to the internet. Only users with access to the local network can connect to Neo4j.

Step #7: Configure the Neo4j Graph Database

For Neo4j to connect to other servers, we need to change the settings of the configuration file /etc/neo4j/neo4j.conf. We will use the Nano Editorfor this task. Remember, if you are not root, use the sudo command:

 root@host:~# nano /etc/neo4j/neo4j.conf
 root@host:~#  

Find the line in the Network Connector section:

#dbms.default_listen_address=0.0.0.0

Uncomment this line by removing the # symbol and then press Ctrl + S and Ctrl + X keys to save and exit the editor.

The 0.0.0.0 value will bind Neo4j to all available IPv4 network interfaces. You can put a specific IP address or network that your servers use as a data path. You can also configure it to use IPv6 interfaces, but there are many nuances to that setting. We recommend that you read the documentation on the official website.

Step #8: Configure the Firewall for Remote Connections

To configure the Neo4j software for remote connections, we must configure the firewall. We restrict access so that only trusted systems can connect to it. In this case, we will use the default Ubuntu firewall, UFW. 

8a. Enabling the Ubuntu Firewall (UFW)

Next, we must check to see if the firewall is activated. If it is not active, we have to enable it:

 root@host:~# ufw enable
 Firewall is active and enabled on system startup
 root@host:~#  

Neo4j creates two network sockets when installing the software. One on port 7474 for the HTTP interface and the one for the primary protocol on port 7687. Neo4j recommends using port 7687. The command to open a port will be similar to the following command used for allowing an IPv4 address:

 ufw allow from YOUR_IP to any port 7687 proto tcp
 YOUR_IP - Use an IP here to provide access permission. If you want to allow access to the entire local network, use the following rule:
 ufw allow from 192.168.50.0/16 to any port 7687 proto tcp 

8b. Opening Your Port

Enter your specific network range to open the port. For an IPv6 address, the command will look like this:

ufw allow from fe80::1006:f7a3:b9cc:b0cb to any port 7687 proto tcp

The above IPs are used as an example. Substitute your values and add a rule:

 root@host:~# ufw allow from 192.168.50.189 to any port 7687 proto tcp
 Rule added
 root@host:~#  

8c. Restarting Your Firewall

Be sure to restart your firewall:

 root@host:~# ufw reload
 Firewall reloaded
 root@host:~#  

8d. Verifying the Connection

Now let’s check to see if it is working correctly:

 root@host:~# ufw status
 Status: active
 To                         Action      From
 --                         ------      ----
 7687/tcp                   ALLOW       192.168.50.189            
 root@host:~#  

And with that, we have a working Neo4j server running the Neo4j graph database that's ready to go and configured to allow access on port 7687.

Conclusion — Neo4j Graph Database

We got acquainted with the Neo4j graph database, learned how it works and why it is needed. As well, we set up a package manager and then installed Neo4j. Next, we checked the functionality, went into it, and changed the password. We tried basic commands related to creating a table, making relationships, and setting up nodes. Ultimately, we configured the connection to the IPs we needed and configured a firewall for security. Good work!

Let our team of helpful humans assist you as you continue building stellar solutions on the web. He have the hosting and web applications for the job.

Original Publication Date

This article was originally published in January 2021. It has since been updated for accuracy and comprehensiveness.

Avatar for Amritha Varshini

About the Author: Amritha Varshini

With a Bachelor of Engineering in Information Science, Amritha Varshini has been working as a Linux Admin for Liquid Web since 2018. Varshini has worked on multiple operating systems, such as CentOS, AlmaLinux, and Ubuntu and on servers, like VPS, cloud, shared, reseller, and dedicated.

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article