How to Install LevelDB: Step-by-Step Tutorial
LevelDB is an open-source key-value storage library developed by Google. It is widely used for applications requiring fast read and write performance. This tutorial will walk you through how to install LevelDB on different operating systems, including Linux, Windows, and macOS, along with helpful tips and troubleshooting advice.
Prerequisites
- A computer running Linux, Windows, or macOS.
- Basic familiarity with command line or terminal usage.
- Development tools like
gcc,g++, orclanginstalled (for compiling from source). - CMake installed for building the LevelDB project.
- Git installed (optional, for fetching the latest source).
Step 1: Download LevelDB Source Code
You can download the latest LevelDB source from its official GitHub repository. Open your terminal or command prompt and run:
git clone https://github.com/google/leveldb.git
cd leveldb
If you don’t have git, download the zip archive from the LevelDB GitHub page (Official site) and extract it.
Step 2: Build LevelDB
LevelDB uses C++11 and CMake for its build system. Follow the instructions specific to your OS:
On Linux and macOS
- Ensure CMake, g++, and make are installed. You can install them via your package manager, e.g.,
sudo apt install build-essential cmakeon Ubuntu orbrew install cmakeon macOS with Homebrew. - Create a build directory inside the cloned repo and run CMake:
mkdir -p build && cd build
cmake ..
make -j$(nproc)
This will compile LevelDB and generate the library files inside the build directory.
On Windows
- Install CMake (Official site) and Visual Studio with C++ tools.
- Open the Developer Command Prompt from Visual Studio.
- Navigate to the LevelDB directory you cloned or extracted.
- Create a build folder and configure the project with CMake:
mkdir build
cd build
cmake -G "Visual Studio 16 2019" ..
Replace the generator name with your installed Visual Studio version if needed. Then open the generated LevelDB.sln file in Visual Studio and build the solution.
Step 3: Installing the Library (Optional)
On Linux/macOS, you can install LevelDB system-wide:
sudo make install
This step installs the headers and libraries so other programs can easily link LevelDB.
Step 4: Verify Installation
To verify LevelDB is correctly installed, write a simple test program that opens a LevelDB database, puts a key-value pair, and retrieves it.
#include <iostream>
#include "leveldb/db.h"
int main() {
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "./testdb", &db);
if (!status.ok()) {
std::cerr << "Unable to open/create test database './testdb'" << std::endl;
return -1;
}
// Put key-value
status = db->Put(leveldb::WriteOptions(), "key1", "value1");
if (!status.ok()) {
std::cerr << "Write failed" << std::endl;
}
// Get key
std::string value;
status = db->Get(leveldb::ReadOptions(), "key1", &value);
if (status.ok()) {
std::cout << "Got value: " << value << std::endl;
} else {
std::cerr << "Read failed" << std::endl;
}
delete db;
return 0;
}
Compile the above program linking with LevelDB. For example, on Linux/macOS:
g++ -std=c++11 -o test_leveldb test_leveldb.cpp -lleveldb -lpthread
Run it with ./test_leveldb and check output.
Troubleshooting
- Compiler errors: Confirm your compiler supports C++11. GCC 4.8+ and Visual Studio 2015+ are recommended.
- CMake not found: Install CMake from the official site.
- Missing dependencies: On Linux, ensure you have the build-essential or equivalent packages installed.
- Windows build issues: Use the Developer Command Prompt and ensure CMake config uses the correct Visual Studio generator.
Summary Checklist
- Download LevelDB source code
- Install prerequisites (CMake, compiler, Git)
- Build LevelDB using CMake and make (or Visual Studio on Windows)
- Optionally install the library system-wide
- Test the installation with a sample program
For more database installation tutorials, check out our guide on How to Install FoundationDB.
