LevelDB is a fast key-value storage library developed by Google. It provides an ordered mapping from string keys to string values, backed by log-structured merge trees (LSM trees). LevelDB powers components of Chrome, Bitcoin Core, and many embedded databases where you need fast writes without the overhead of a full RDBMS. This guide covers installing LevelDB on Ubuntu 24.04 and Debian 13, building from source, and using it from both C++ and Python.
What Is LevelDB
LevelDB is an embedded database – it runs inside your application process, not as a separate server. Data is stored on disk in sorted order, making sequential reads efficient. Key characteristics include:
- Keys and values are arbitrary byte arrays
- Data is sorted by key, so range scans are fast
- Supports batch writes for atomic updates
- Supports snappy compression by default
- Single-process access only (no concurrent access from multiple processes)
It is not a replacement for PostgreSQL or MySQL – it has no SQL layer, no network protocol, and no multi-user access. Think of it as a fast, persistent dictionary for a single application.
Install LevelDB from apt
Ubuntu 24.04 and Debian 13 ship LevelDB development packages in their default repositories. Install the library and headers:
sudo apt update
sudo apt install -y libleveldb-dev libleveldb1d
Verify the installation by checking the library is present:
dpkg -l | grep leveldb
You should see both the runtime library and the development headers listed. Check the header file exists:
ls /usr/include/leveldb/db.h
Build LevelDB from Source (CMake)
Building from source gives you the latest version and lets you customize build options. Install the build dependencies:
sudo apt install -y build-essential cmake libsnappy-dev
Clone the LevelDB repository:
git clone --recurse-submodules https://github.com/google/leveldb.git
cd leveldb
Create a build directory and compile:
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DLEVELDB_BUILD_TESTS=OFF -DLEVELDB_BUILD_BENCHMARKS=ON ..
make -j$(nproc)
Install the compiled library system-wide:
sudo make install
sudo ldconfig
Verify the installation by checking the shared library:
ldconfig -p | grep leveldb
You should see libleveldb.so listed with its full path.
Use LevelDB in C++
Create a simple C++ program that opens a database, writes a key-value pair, reads it back, and closes the database. Save this as leveldb_test.cpp:
#include <iostream>
#include <cassert>
#include "leveldb/db.h"
int main() {
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());
std::cout << "Database opened successfully" << std::endl;
// Write a key-value pair
status = db->Put(leveldb::WriteOptions(), "server", "debian13");
assert(status.ok());
std::cout << "Write successful" << std::endl;
// Read it back
std::string value;
status = db->Get(leveldb::ReadOptions(), "server", &value);
assert(status.ok());
std::cout << "Read: server = " << value << std::endl;
// Delete the key
status = db->Delete(leveldb::WriteOptions(), "server");
assert(status.ok());
std::cout << "Delete successful" << std::endl;
delete db;
return 0;
}
Compile and run:
g++ -o leveldb_test leveldb_test.cpp -lleveldb -lpthread
./leveldb_test
Expected output:
Database opened successfully
Write successful
Read: server = debian13
Delete successful
Use LevelDB in Python with plyvel
The plyvel library provides a Pythonic interface to LevelDB. Install it in a virtual environment:
sudo apt install -y python3-pip python3-venv
python3 -m venv ~/leveldb-env
source ~/leveldb-env/bin/activate
pip install plyvel
Verify plyvel installed correctly:
python3 -c "import plyvel; print(plyvel.__version__)"
Now use it for basic operations. Create a file called leveldb_demo.py:
import plyvel
# Open or create a database
db = plyvel.DB('/tmp/testdb_python', create_if_missing=True)
# Write key-value pairs
db.put(b'distro', b'ubuntu2404')
db.put(b'kernel', b'6.8')
db.put(b'desktop', b'gnome47')
print("Writes complete")
# Read a single key
value = db.get(b'distro')
print(f"distro = {value.decode()}")
# Iterate over all keys
print("\nAll entries:")
for key, value in db:
print(f" {key.decode()} = {value.decode()}")
# Batch write (atomic)
with db.write_batch() as batch:
batch.put(b'editor', b'vim')
batch.delete(b'desktop')
print("\nBatch write complete")
# Verify batch results
print(f"editor = {db.get(b'editor').decode()}")
print(f"desktop = {db.get(b'desktop')}") # Should be None
db.close()
Run the script:
python3 leveldb_demo.py
Benchmarking LevelDB
If you built from source with benchmarks enabled, the db_bench binary is available in the build directory. Run it to measure performance on your hardware:
./build/db_bench
This runs a series of tests including sequential writes, random writes, sequential reads, and random reads. Typical results on modern hardware show:
- Sequential writes: 400,000+ operations per second
- Random writes: 200,000+ operations per second
- Sequential reads: 500,000+ operations per second
- Random reads: 100,000+ operations per second
Actual numbers vary significantly based on disk type (NVMe vs SATA SSD vs HDD) and dataset size relative to available memory.
Wrapping Up
LevelDB is a solid choice when your application needs a fast, embedded key-value store without the complexity of running a database server. The apt package gets you started quickly on Ubuntu 24.04 and Debian 13, while building from source lets you pull in the latest optimizations. Combined with plyvel for Python or the native C++ API, it integrates cleanly into most application stacks.






















































