Neo4j is a graph database that enables the creation of data relationships. Unlike traditional databases, which store data in a table-like structure, a graph database records relationships between data nodes, with each node storing references to connected nodes. This methodology allows Neo4j to efficiently encode and query complex relationships, sidestepping the expensive and time-consuming indexing processes required by traditional databases.
Developed by Neo Technology, Neo4j is built using Java and Scala, and offers both a free community edition and an enterprise edition. While Neo4j’s primary query language is Cypher, queries can also be crafted in other languages.
This tutorial will guide you through the installation and configuration of Neo4j on an Ubuntu 22.04 server.
Prerequisites
- A server running Ubuntu 22.04 with at least 1 CPU core and 2 GB of memory. Consider upgrading if your project demands more resources.
- A non-root user with sudo privileges.
- Ensure that your system is updated by executing:
$ sudo apt update
- Install basic utility packages, if they aren’t already present:
$ sudo apt install wget curl nano software-properties-common dirmngr apt-transport-https gnupg gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y
Step 1 – Install Neo4j
Begin by adding the Neo4j GPG key:
$ curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/neo4j.gpg
Add the Neo4j repository to your system’s APT sources:
$ echo "deb [signed-by=/usr/share/keyrings/neo4j.gpg] https://debian.neo4j.com stable latest" | sudo tee -a /etc/apt/sources.list.d/neo4j.list
To control version upgrades, specify a major.minor version in place of “latest”. For example, to stay within Neo4j version 5.x:
$ echo "deb [signed-by=/usr/share/keyrings/neo4j.gpg] https://debian.neo4j.com stable 5" | sudo tee -a /etc/apt/sources.list.d/neo4j.list
Update the package list:
$ sudo apt update
List available Neo4j versions:
$ apt list -a neo4j Listing... Done neo4j/stable 1:5.3.0 all neo4j/stable 1:5.2.0 all neo4j/stable 1:5.1.0 all
Install the Neo4j Community Edition:
$ sudo apt install neo4j
To install a specific version:
$ sudo apt install neo4j=1:5.3.0
Neo4j includes the necessary JDK version. Enable and start the Neo4j service:
$ sudo systemctl enable neo4j
$ sudo systemctl start neo4j
Check the status of Neo4j:
$ sudo systemctl status neo4j ? neo4j.service - Neo4j Graph Database Loaded: loaded (/lib/systemd/system/neo4j.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2023-01-21 20:50:52 UTC; 33s ago Main PID: 5241 (java) Tasks: 72 (limit: 1030) Memory: 399.3M CPU: 20.350s CGroup: /system.slice/neo4j.service
Step 2 – Test Connection
Neo4j utilizes the Cypher Shell for data interaction.
Connect to the Cypher Shell:
$ cypher-shell
Enter the default credentials (neo4j
/neo4j
). You’ll then be prompted to change the password:
username: neo4j password: Password change required new password: confirm password: Connected to Neo4j using Bolt protocol version 5.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>
Exit the shell using:
neo4j@neo4j> :exit Bye!
Step 3 – Configure Neo4j for Remote Access
To allow remote hosts to connect to Neo4j, configure the settings located in /etc/neo4j/neo4j.conf
. Open the file for editing:
$ sudo nano /etc/neo4j/neo4j.conf
Uncomment and set dbms.default_listen_address=0.0.0.0
by removing the preceding hash. It should look like this:
. . . #***************************************************************** # Network connector configuration #***************************************************************** # With default configuration Neo4j only accepts local connections. # To accept non-local connections, uncomment this line: server.default_listen_address=0.0.0.0 . . .
Save and exit the file. The address 0.0.0.0
binds Neo4j to all available IPv4 interfaces. Customize with a specific IP if needed. For IPv6 configurations, handle it similarly with the appropriate settings and DNS considerations.
Step 4 – Configure Firewall Access (UFW)
After enabling remote connections, restrict access using the firewall. Neo4j interacts over ports 7474 (HTTP) and 7687 (bolt protocol).
Use UFW (Ubuntu’s default firewall) to permit trusted systems:
$ sudo ufw allow from 203.0.113.1 to any port 7687 proto tcp
For a network range:
$ sudo ufw allow from 192.0.2.0/24 to any port 7687 proto tcp
IPv6 Example:
$ sudo ufw allow from 2001:DB8::1/128 to any port 7687 proto tcp
Reload the firewall:
$ sudo ufw reload
Verify firewall status:
$ sudo ufw status Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 22/tcp (v6) ALLOW Anywhere (v6) 7687/tcp ALLOW 203.0.113.1
Step 5 – Use Neo4j
Connect using cypher-shell
and provide credentials:
$ cypher-shell
If using remote access:
$ cypher-shell -a 'neo4j://203.0.113.1:7687'
Example IPv6 connection:
$ cypher-shell -a 'neo4j://your_hostname:7687'
To create nodes and relationships, try the following:
neo4j@neo4j> CREATE (:Slite {name: 'Navjot Singh'});
Sample output:
0 rows ready to start consuming query after 124 ms, results consumed after another 0 ms Added 1 nodes, Set 1 properties, Added 1 labels
Chain nodes using the COLLEAGUE
relation:
neo4j@neo4j> CREATE (:Slite {name: 'Sammy'})-[:COLLEAGUE]-> (:Slite {name: 'Peter Jack'})-[:COLLEAGUE]-> (:Slite {name: 'Chris Rock'});
Output:
0 rows ready to start consuming query after 72 ms, results consumed after another 0 ms Added 3 nodes, Created 2 relationships, Set 3 properties, Added 3 labels
Create departmental and project relationships:
neo4j@neo4j> MATCH (a:Slite),(b:Slite) WHERE a.name = 'Peter Jack' AND b.name = 'Chris Rock' CREATE (a)-[r:DEPARTMENT { name: 'Designers' }]->(b) RETURN type(r), r.name;
Check connections:
neo4j@neo4j> MATCH (a)-[r]->(b) RETURN a.name,r,b.name ORDER BY r;
Conclusion
This tutorial showed you how to install and configure Neo4j on an Ubuntu 22.04 server. For questions, feel free to comment below.
Frequently Asked Questions
What is Neo4j?
Neo4j is a graph database platform used to efficiently store, manage, and query connected data.
Which versions of Neo4j are available?
Neo4j offers a free community edition and an enterprise edition, with various version releases available.
How do I secure my Neo4j installation?
Secure your installation by configuring access restrictions, using firewalls, and applying best practices for user management.
Can Neo4j be configured for remote access?
Yes, you can configure Neo4j to accept remote connections by adjusting settings in the neo4j.conf
file and setting firewall rules.