Install Neo4j in Ubuntu and Use Gremlin
Install Neo4j in Ubuntu and Use Gremlin
1. Reason why installing Neo4j in Ubuntu
In previous article, we say that we will use Gremlin to Access Neo4j.
However, there is a problem, that Gremlin cannot support Neo4j whose version is too high, while Neo4j Desktop in Windows can only install Neo4j database whose lowest version is 4.1.0
.
So, we will install Neo4j in Ubuntu. Of course, we can install Neo4j of any version from GitHub, but it is more easy to use apt
to install it.
All things following will be done by root
credential!!!
2. Install Neo4j in Ubuntu
We will use Neo4j whose version is 3.3.9
.
2.1 Install JDK-1.8
Neo4j 3.3.9
only support JDK-1.8, but we use JDK-17 or higher now, so we need install JDK-1.8 before installing Neo4j.
JDK-1.8 is too old and it is only used to run Neo4j, so we need to manage JDK versions in our computer.
We will use JENV to do this.
First we clone this repository:
git clone https://github.com/jenv/jenv.git ~/.jenv
Then add jenv
to PATH
environment:
# ~/.bashrc
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
Finally, we restart terminal or use a new terminal or run command following:
source ~/.bashrc
We can use command following to check whether it has been installed correctly:
jenv --version
After that, we can install JDK-1.8:
sudo apt install openjdk-8-jdk # JDK-1.8
Then add JDKs in our computer to jenv
:
# JDK installed by `apt` is commonly here
jenv add /usr/lib/jvm/java-8-openjdk-amd64
# And other JDKs...
After adding them, we can run command following to view them:
jenv versions
But Neo4j uses environment variable JAVA_HOME
to find where JDK is, we need to enable plugin export
in jenv
to automatically manage it. Run command following:
jenv enable-plugin export
This plugin can automatically set JAVA_HOME
according to the version of JDK chosen by us.
The usage of jenv
is as follows:
# View Java version added
jenv versions
# Add new Java version to `jenv`
jenv add <java-path>
# Set global Java version
jenv global <version>
# Set Java version used in current directory or project
jenv local <version>
# Set Java version used in current shell
jenv shell <version>
Finally, we change to JDK-1.8 to do following things:
jenv global 1.8
2.2 Install Neo4j
Run commands following:
wget -O - https://debian.neo4j.com/neotechnology.gpg.key | sudo apt-key add -
echo 'deb https://debian.neo4j.com stable legacy' | sudo tee /etc/apt/sources.list.d/neo4j.list
apt update
apt list -a neo4j
apt install neo4j=1:3.3.9
We can use command following to check whether it can run:
neo4j start
Then we can see information following if successful:
Active database: graph.db
Directories in use:
home: /var/lib/neo4j
config: /etc/neo4j
logs: /var/log/neo4j
plugins: /var/lib/neo4j/plugins
import: /var/lib/neo4j/import
data: /var/lib/neo4j/data
certificates: /var/lib/neo4j/certificates
run: /var/run/neo4j
Starting Neo4j.
It will run at 7474
port and we can visit it from our browser. We can run command to check the status of Neo4j:
neo4j status
If we want to connect it from other computer, we can change its configuration:
dbms.connectors.default_listen_address=0.0.0.0
Then restart Neo4j:
neo4j restart
3. Install Gremlin and Connect
Gremlin can support many versions of JDK, so we can discard JDK-1.8 now!!!
3.1 Install Gremlin
Download from this web page: Apache TinkerPop.
Or download with wget
command:
wget https://dlcdn.apache.org/tinkerpop/3.7.2/apache-tinkerpop-gremlin-server-3.7.2-bin.zip
Then unzip it.
Change to this directory and change its configuration:
File conf/gremlin-server-neo4j.yaml
:
# Allow all address to connect
host: 0.0.0.0
File neo4j-empty.properties
:
gremlin.neo4j.directory=/var/lib/neo4j/data/databases/graph.db
Before running Gremlin, we need to install some dependencies:
# version of dependencies is according to the version of gremlin
bin/gremlin-server.sh install org.apache.tinkerpop neo4j-gremlin 3.7.2
In addition, we need to stop Neo4j server, because they two cannot run in the same time:
neo4j stop
Then we run command following to check whether Gremlin server can run successfully:
bin/gremlin-server.sh conf/gremlin-server-neo4j.yaml
If it can run successfully, stop it and change file bin/gremlin-server.sh
:
# Other codes
if [[ -z "$GREMLIN_YAML" ]]; then
GREMLIN_YAML=$GREMLIN_HOME/conf/gremlin-server-neo4j.yaml
fi
# Other codes
Then start Gremlin server:
bin/gremlin-server.sh start
Now Gremlin server is running in the background.
3.2 Connect to Gremlin Server with Gremlin Console
We can use Gremlin console to connect to Gremlin server.
Download from this web page: Apache TinkerPop.
Or download with wget
command:
wget https://dlcdn.apache.org/tinkerpop/3.7.2/apache-tinkerpop-gremlin-console-3.7.2-bin.zip
Then unzip it and change to this directory.
Make file remote.yaml
consistent with where Gremlin server is running or add a new file:
hosts: [localhost]
port: 8182
Then run Gremlin console:
bin/gremlin.sh
If successful, we can see information following:
Oct 12, 2024 8:07:13 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
\,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin>
Then run command to connect to Gremlin server:
:remote connect tinkerpop.server conf/remote.yaml
If successful, we can run command following to change to remote mode:
:remote console
3.3 Connect to Gremlin Server in WSL from Windows
First we need to find the IP of WSL.
If localhost
of WSL is the same as that of Windows, just use localhost
is OK.
Or, we can run command in WSL to get it:
ip addr show eth0
We can see output like this:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:c7:ff:e6 brd ff:ff:ff:ff:ff:ff
inet 172.18.50.12/20 brd 172.18.63.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fec7:ffe6/64 scope link
valid_lft forever preferred_lft forever
The output indicates that 172.18.50.12
is the IP of WSL.
Then we can connect to the Gremlin server running in WSL from Windows.
Just like the previous subsection, download and unzip file, and change configuration file. Then run the console:
.\bin\gremlin.bat
Other things is the same as those in Ubuntu.
4. Problem
Neo4j and Gremlin server cannot run at the same time, just say that we cannot use Neo4j to view the graph in a visual way.
How can we resolve this problem? Just look at the next article!