We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

# How To Install Neo4J on an Ubuntu VPS

PostedJanuary 15, 2014 87.9k views

## Introduction

### What is a graph?

A graph is a set of vertices connected by edges. In the realm of databases, a graph is a set of items with each item having any type of relationship to another item in the dataset.

### What are vertices and edges?

• Vertices - Vertices are the datapoints in a graph. For those familiar with any form of SQL database, a vertex can be viewed as a row/record. For those not familiar with SQL, a vertex can be viewed as a piece of data.

• Edges - An edge is the relationship between two different vertices. An edge is hard to translate into SQL terms because of how flexible they are with graph databases, but an edge can be viewed as the way two pieces of data are connected.

### Example

A social network is one of the best examples of a graph that most people can relate to. In a social network, you have people and you have relationships between each person. The people are represented as vertices, and the relationships are represented as edges. There are many different types of relationships such as: married, friends with, in a relationship with, works with, etc. This is the same for graphs. There are endless possibilities for different types of edges and there are endless possibilities for different types of vertices.

### A pictorial example

In this picture, the graph vertices are just integers and the edges are not labeled. Despite the simplicity, this is still a graph.

### Weighted graphs

In the example of an airline company, when dealing with getting a plane from point A to point B, you want to choose the best possible path for the plane to take. Let the airports be visualized as vertices and the flight paths between them be edges.

Each edge is assigned a weight, or a cost, for utilizing it. Here, the weight represents the distance between two airports. So for example, in the graph above, the cost to get from LAX to ORD is 1749. Weighted graphs are especially useful in geographical data representations where distance is a factor.

## Graph Databases

A graph database is a NoSQL database that stores information as vertices and edges (nodes and relationships). Rather than having foreign keys and select statements, you use edges and graph traversals to query the data. This method of querying data is extremely powerful in many cases such as social networks, biology, chemistry, business analytics, and any situation where data is best represented as items that have relationships with other items in the dataset.

In this tutorial we will be installing Neo4J: an extremely popular graph database with many language bindings for pretty much any popular programming language.

## Installing Neo4J

Add the Neo4J key into the apt package manager:

``````wget -O - http://debian.neo4j.org/neotechnology.gpg.key | apt-key add -
``````

Add Neo4J to the Apt sources list:

``````echo 'deb http://debian.neo4j.org/repo stable/' > /etc/apt/sources.list.d/neo4j.list
``````

Update the package manager:

``````apt-get update
``````

Install Neo4J:

``````apt-get install neo4j
``````

Neo4J should be running. You can check this with the following command

``````service neo4j-service status
``````

### The Neo4J RESTful API and the Cypher Query Language

One of the things that makes Neo4J awesome is that it has a very easy to use RESTful API, which means that it can be used by virtually any programming language that can make web requests. Many of the operations performed on a Neo4J database are executed using a Cypher query. The Cypher query language is the query language used by Neo4J to manipulate and read data. Cypher is to Neo4J as SQL is to MySQL.

The structure of a web request to the Neo4J RESTful API is as follows:

``````curl -H "Accept: application/json; charset=UTF-8" -H "Content-Type: application/json" -X POST http://SERVERNAME:7474/db/data/cypher -d '{
"query" : "CYPHER QUERY GOES HERE",
"params" : {
QUERY PARAMETERS GO HERE
}
}'
``````

Neo4J is a database, and databases need data, so let's add some data!

Create a new node:

``````curl -H "Accept: application/json; charset=UTF-8" -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d '{
"query" : "CREATE (n:Person { name : {name} }) RETURN n",
"params" : {
"name" : "Foo"
}
}'
``````

I mentioned earlier that graph databases store data as nodes and relationships. A relationship requires two nodes, so let's create another node:

``````curl -H "Accept: application/json; charset=UTF-8" -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d '{
"query" : "CREATE (n:Person { name : {name} }) RETURN n",
"params" : {
"name" : "Bar"
}
}'

``````

Now we can create a relationship between these two nodes:

``````curl -H "Accept: application/json; charset=UTF-8" -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/node/0/relationships -d '{
"to" : "http://localhost:7474/db/data/node/1",
"type" : "Comes Before"
}
}'
``````

## Using Cypher to Query Neo4J

Below are some example cypher queries we can use to view the data we previously inserted.

We can start at the first node we created, and get all connected nodes and the corresponding relationships:

``````curl -H "Accept: application/json; charset=UTF-8" -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d '{
"query" : "MATCH (x {name: {startName}})-[r]->(n) RETURN type(r), n.name",
"params" : {
"startName" : "Foo"
}
}'
``````

Return the name property of all the nodes in the graph (Note: This should not be performed on large graphs):

``````curl -H "Accept: application/json; charset=UTF-8" -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d '{
"query" : "START n = node(*) return n.name",
"params" : {
}
}'
``````

Return all the relationship types in the graph (Note: This should not be performed on large graphs):

``````curl -H "Accept: application/json; charset=UTF-8" -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d '{
"query" : "START r=rel(*) return type(r) ",
"params" : {
}
}'
``````

A more complete description and list of methods provided by the Neo4J RESTful API can be found here, and information on the Cypher query language can be found here.