Install Apache Cassandra NoSQL DB on Ubuntu

By Anurag Singh

Updated on Aug 14, 2024

Install Apache Cassandra NoSQL DB on Ubuntu

In this tutorial, we'll explain how to install Cassandra NoSQL Database on Ubuntu 22.04. 

Apache Cassandra is a distributed NoSQL database designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. Apache Cassandra is a highly scalable, distributed NoSQL database designed to handle large amounts of data across multiple servers with no single point of failure. 

Known for its high availability, fault tolerance, and ability to scale horizontally, Cassandra is ideal for applications that require high performance, reliability, and seamless data distribution across many nodes. It uses a decentralized, peer-to-peer architecture, making it well-suited for handling massive, real-time data workloads across geographically distributed environments.

Prerequisites

Before you begin, ensure you have the following:

  • A dedicated server or KVM VPS running Ubuntu 22.04 or later.
  • A user with sudo privileges.
  • At least 2GB of RAM. For this tutorial, we have used 4GB RAM server.

Step 1: Update the System

Start by updating your package lists and upgrading your existing packages to the latest versions.

sudo apt update && sudo apt upgrade -y

Step 2: Install Java

Cassandra requires Java to run. Install OpenJDK, which is a free and open-source implementation of the Java Platform.

sudo apt install openjdk-17-jdk -y

Verify the Java installation:

java -version

You should see output similar to:

openjdk version "17.0.12" 2024-07-16
OpenJDK Runtime Environment (build 17.0.12+7-Ubuntu-1ubuntu224.04)
OpenJDK 64-Bit Server VM (build 17.0.12+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)

Step 3: Install Apache Cassandra

Apache Cassandra is not available in the default Ubuntu repositories, so you need to add the official Cassandra repository to your system. First visit official documentation page and get the latest stable version. Official download page

First, add the Cassandra repository to your sources list:

echo "deb [signed-by=/etc/apt/keyrings/apache-cassandra.asc] https://debian.cassandra.apache.org 50x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list

Next, add the Cassandra repository keys:

curl -o /etc/apt/keyrings/apache-cassandra.asc https://downloads.apache.org/cassandra/KEYS

Once the repository is added, update your package lists and install Cassandra.

sudo apt update
sudo apt install cassandra -y

After the installation is complete, start the Cassandra service and enable it to start on boot.

sudo systemctl start cassandra
sudo systemctl enable cassandra

Check the status of the Cassandra service to ensure it's running properly:

sudo systemctl status cassandra

Step 4: Verify Cassandra Installation

To verify that Cassandra is installed and running, use the nodetool utility, which is included with the Cassandra installation.

sudo nodetool status

You should see output indicating that your node is up and running.

Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load        Tokens  Owns (effective)  Host ID                               Rack 
UN  127.0.0.1  114.69 KiB  16      100.0%            8e1cf493-02b9-4a3a-a8cc-d5c498dbb577  rack1

Step 5: Connect to the Cassandra Database

Cassandra comes with a command-line tool called cqlsh (Cassandra Query Language Shell), which you can use to connect to your Cassandra database.

To connect to your Cassandra database, run:

cqlsh

You will be dropped into the cqlsh shell, where you can execute Cassandra Query Language (CQL) commands.

Step 6: Create a Keyspace

In Cassandra, a keyspace is a namespace that defines data replication on nodes. Let's create a simple keyspace:

CREATE KEYSPACE mykeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};

To verify that your keyspace was created, you can list all keyspaces:

DESCRIBE keyspaces;

Step 7: Create a Table

Now that you have a keyspace, you can create a table within it. Here’s an example of creating a simple users table:

USE mykeyspace;

CREATE TABLE users (
    user_id UUID PRIMARY KEY,
    name text,
    age int,
    email text
);

Step 8: Insert Data into the Table

You can now insert data into the table using the INSERT statement:

INSERT INTO users (user_id, name, age, email)
VALUES (uuid(), 'John Doe', 30, 'johndoe@example.com');

Step 9: Query the Data

Finally, you can query the data from the table to verify that everything is working correctly:

SELECT * FROM users;

You should see the data you inserted displayed in the output.

 user_id                              | age | email               | name
--------------------------------------+-----+---------------------+----------
 73dc58ac-766c-43b9-a434-02240b6b2837 |  30 | johndoe@example.com | John Doe

Conclusion

You have successfully seen how to install Apache Cassandra NoSQL DB on Ubuntu 22.04 server. You can now start building scalable, high-performance applications that leverage Cassandra’s distributed architecture.