Introduction
Apache Cassandra is a popular, open-source NoSQL database software. It offers high scalability and availability while handling large data amounts, making it a great choice for apps that require fault tolerance and seamless scalability.
Regular relational databases cannot handle linear scaling, seamless data distribution, and other big data requirements as efficiently as Cassandra.
In this tutorial, you will learn to install and configure Apache Cassandra on Ubuntu.
Prerequisites
- A system running Ubuntu.
- Access to the terminal.
- A user account with root access.
Step 1: Install Dependencies for Apache Cassandra
Cassandra requires Java 11 to work and the apt-transport-https package to connect to HTTPS repositories. This section shows how to install these dependencies.
If you already have these packages installed, you can skip to Step 2 of the guide.
Note: This tutorial uses Ubuntu 24.04 to provide the examples, but the instructions apply to other Ubuntu versions as well.
Install Java OpenJDK
Apache Cassandra requires OpenJDK 8 or 11 to run on an Ubuntu system. For this tutorial, we will use Java 11. Follow the steps below to install it:
1. Update the package index:
sudo apt update
2. Install OpenJDK 11 using the following command:
sudo apt install openjdk-11-jdk -y
3. When the installation completes, check the Java version to test if Java was installed successfully:
java -version
If everything installed correctly, the output should print the Java version:
Install the apt-transport-https Package
Next, install the apt-transport-https package. You need to add this package to your system to enable access to the repositories using HTTPS.
Run the command:
sudo apt install apt-transport-https
Wait for the installation process to complete.
Step 2: Add Apache Cassandra Repository and Import Repository Key
Since Apache Cassandra is not available in the official Ubuntu repository, you need to add the Cassandra repository and pull the repository key before installing the database.
Follow the steps below:
1. Use the wget command to pull the public repository key from the URL below:
wget -qO- https://downloads.apache.org/cassandra/KEYS | sudo apt-key add -
2. Run the command below to add the Cassandra repository to the sources list:
echo "deb https://debian.cassandra.apache.org 41x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
Replace 41x
with the appropriate version series you want to use (e.g., 40x
for version 4.0 or 39x
for version 3.9).
Step 3: Install Apache Cassandra
You are now ready to install Cassandra on Ubuntu. Follow the steps below:
1. Update the package index:
sudo apt update
2. Run the install command:
sudo apt install cassandra -y
Wait for the process to complete. Once the installation finishes, the Cassandra service starts automatically. Also, a user named cassandra is created during the process. That user is used to run the service.
Verify Apache Cassandra Installation
Finally, check the cluster status to make sure that the Cassandra installation process completed properly. Run the following command:
nodetool status
The UN
letters in the output signal that the cluster is working.
Commands to Start, Stop, and Restart Cassandra Service
Check the Cassandra service status by running:
sudo systemctl status cassandra
The output should display active (running)
in green.
If, for any reason, the service shows inactive
after the installation, you can start it manually with:
sudo systemctl start cassandra
To restart the service, use the restart
command:
sudo systemctl restart cassandra
To stop the Cassandra service, run:
sudo systemctl stop cassandra
The status shows inactive
after using the stop
command.
Optional: Start Apache Cassandra Service Automatically on Boot
When you turn off or reboot your system, the Cassandra service switches to inactive. To start Cassandra automatically after booting up, run the following command:
sudo systemctl enable cassandra
Now, when the system reboots, the Cassandra service starts automatically.
Step 4: Configure Apache Cassandra
After installation, change the Cassandra configuration settings depending on your requirements. The default configuration is sufficient if you intend to use Cassandra on a single node. If you plan to use Cassandra in a cluster, customize the main settings using the cassandra.yaml file.
Important: We strongly advise to create a backup of your cassandra.yaml file if you intend to edit it. Create a backup with the cp command:
sudo cp /etc/cassandra/cassandra.yaml /etc/cassandra/cassandra.yaml.backup
We used the /etc/cassandra directory as a destination for the backup, but you can change the path as you see fit.
Rename Apache Cassandra Cluster
Use a text editor of your choice to open the cassandra.yaml file (we use nano):
sudo nano /etc/cassandra/cassandra.yaml
Find the line that reads cluster_name:
The default name is Test Cluster. That is the first change you want to make when you start working with Cassandra.
If you do not want to make more changes, exit and save the file.
Add IP Addresses of Cassandra Nodes
Another thing to add to the cassandra.yaml, if you are running a cluster, is the IP address of every node.
Open the configuration file, and under the seed_provider
section, find the seeds
entry:
Add the IP address of every node in your cluster. Divide the entries with a comma after every IP address.
Step 5: Test Cassandra Command-Line Shell
The Cassandra software package comes with its command-line tool (CLI). The tool uses Cassandra Query Language (CQL) for communication.
To start a new shell, open the terminal and type:
cqlsh
A shell loads showing the connection to the default cluster. If you changed the cluster_name
parameter, it will show the one you defined in the configuration file. The example above is the default connection to the localhost.
Important: If cqlsh
fails to start and throws a ModuleNotFoundError, check your Python version. This is a known error for Python 3.12, and a workaround is to run cqlsh
in a virtualenv
Python virtual environment with a Python 3.11 installation.
Conclusion
This tutorial showed how to install Cassandra on an Ubuntu system and configure the most important parameters in the Cassandra configuration file.
Next, learn more about Cassandra in our guide on how to create, drop, alter, and truncate Cassandra tables, or see how Cassandra compares to MongoDB.