Neo4j is a powerful graph database that allows you to create and manage complex data relationships. Unlike traditional databases that store data in tables, Neo4j uses a graphical structure that represents relationships between data nodes. This enables efficient and fast querying of complex relationships without the need for expensive indexing.
In this tutorial, we will guide you through the process of installing and configuring Neo4j on a Ubuntu 22.04 server. By following these steps, you’ll be able to harness the power of Neo4j and leverage its capabilities for your data management needs.
Prerequisites
Before we begin, make sure you have the following:
- A server running Ubuntu 22.04 with at least 1 CPU core and 2 GB of memory. If you need to upgrade your server, Shape.host offers reliable Linux SSD VPS services that can meet your requirements.
- A non-root user account with sudo privileges.
Step 1 – Install Neo4j
To start the installation process, we need to add the Neo4j GPG key to the system.
$ curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/neo4j.gpg
Next, we’ll add the Neo4j repository to the system APT’s sources directory.
$ 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 avoid unexpected upgrades to newer major versions, you can specify a specific version instead of “latest” in the above command. For example, to install 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
Now, update the system repositories list.
$ sudo apt update
You can list the available Neo4j versions for installation.
$ apt list -a neo4j
Choose the version you want to install and run the following command. For example, to install Neo4j Community edition:
$ sudo apt install neo4j
By default, Neo4j will automatically install the required JDK version along with it. Once the installation is complete, enable and start the Neo4j service.
$ sudo systemctl enable neo4j $ sudo systemctl start neo4j
To check the status of the Neo4j service, use the following command.
$ sudo systemctl status neo4j
Step 2 – Test Connection
To ensure that Neo4j is installed and running correctly, we’ll test the connection using the Cypher Shell.
$ cypher-shell
You will be prompted for a username and password. The default username is “neo4j”, and the password will be the one you set during the installation process. Once connected, you should see a message confirming the successful connection.
Connected to Neo4j using Bolt protocol version5.0 at neo4j://localhost:7687as user neo4j.
To exit the Cypher Shell, type “:exit” and press Enter.
Step 3 – Configure Neo4j for Remote Access
By default, Neo4j only accepts connections from the localhost. However, for production environments, you may need to allow remote connections. To configure Neo4j for remote access, we’ll edit its configuration file.
$ sudo nano /etc/neo4j/neo4j.conf
Find the line that starts with “#dbms.defaultlistenaddress=0.0.0.0″ and remove the leading hash to uncomment it. This will allow Neo4j to accept connections from remote hosts.
Save the file and exit the text editor. By default, Neo4j will bind to all available IPv4 interfaces, including localhost. If you want to restrict Neo4j to a specific IP address, you can replace “0.0.0.0” with the desired IP address.
For IPv6, you can set the “server.defaultlistenaddress” value to a specific IPv6 address. If you configure Neo4j with an IPv6 address, make sure to add an entry in the remote system’s “/etc/hosts” file or configure a DNS name that resolves to the IPv6 address.
Step 4 – Configure Firewall Access (UFW)
To secure your Neo4j installation, it’s recommended to configure the firewall to only allow connections from trusted systems. We’ll use Uncomplicated Firewall (UFW) to manage the firewall rules.
To allow access to the Neo4j bolt interface from a trusted remote host using IPv4, use the following command:
$ sudo ufw allow from <trusted_remote_host_ip> to any port 7687 proto tcp
Replace “” with the IP address of the trusted remote system. You can also allow an entire network range by specifying the CIDR notation.
For IPv6, use the following command to allow access to the bolt interface:
$ sudo ufw allow from <trusted_remote_host_ipv6> to any port 7687 proto tcp
Replace “” with the IPv6 address of the trusted remote system.
After adding the firewall rules, reload the firewall to apply the changes.
$ sudo ufw reload
You can check the status of the firewall to verify that the rules have been applied.
$ sudo ufw status
Step 5 – Use Neo4j
Now that Neo4j is installed, configured, and accessible, let’s explore some of its features.
To connect to Neo4j using the Cypher Shell, run the following command:
$ cypher-shell
If you have configured Neo4j for remote access, use the following command to connect from a remote system:
$ cypher-shell -a 'neo4j://<neo4j_server_ip>:7687'
Replace “” with the IP address or hostname of the Neo4j server.
Once connected, you can start working with the Neo4j database. Let’s create a node called “Slite” and add the names of authors to Neo4j.
neo4j >CREATE (:Slite {name: 'Navjot Singh'});
This command creates a node of type “Slite” with the name “Navjot Singh”. You can add more employees and relate them using the “COLLEAGUE” relationship.
neo4j> CREATE (:Slite {name: 'Sammy'})-[:COLLEAGUE]-> (:Slite {name: 'Peter Jack'})-[:COLLEAGUE]-> (:Slite {name: 'Chris Rock'});
In this example, we create three nodes and establish a “COLLEAGUE” relationship between them.
To display the data, you can use Cypher queries. For example, to retrieve all the relationships in the database, you can use the following query:
neo4j> MATCH (a)-[r]->(b) RETURN a.name, type(r), b.name;
This query returns the names of the connected nodes and the type of relationship between them.
Conclusion
Congratulations! You have successfully installed and configured Neo4j on your Ubuntu 22.04 server. Now you can leverage the power of this graph database to manage complex relationships in your data.
In this tutorial, we covered the installation process, testing the connection, configuring Neo4j for remote access, and using the database. With Neo4j, you can easily represent and query complex relationships, making it a valuable tool for various applications.
If you have any further questions or need assistance, feel free to reach out to Shape.host, a trusted provider of Linux SSD VPS services. They offer reliable hosting solutions that can support your Neo4j deployment and ensure optimal performance and security.
Now go ahead and explore the capabilities of Neo4j, unlock the potential of your data, and bring your applications to the next level of efficiency and scalability.