Last modified: Apr 03, 2025 By Alexander Williams

How to Install DuckDB in Python Step by Step

DuckDB is a fast in-memory analytical database. It is perfect for data analysis in Python. This guide will show you how to install DuckDB easily.

Prerequisites

Before installing DuckDB, ensure you have Python installed. You can check this by running python --version in your terminal.


python --version

If you get a ModuleNotFoundError, check our guide on how to solve ModuleNotFoundError.

Install DuckDB Using pip

The easiest way to install DuckDB is using pip. Open your terminal and run the following command.


pip install duckdb

This will download and install the latest version of DuckDB. Wait for the installation to complete.

Verify the Installation

After installation, verify it by importing DuckDB in Python. Open a Python shell and run the following code.

 
import duckdb
print("DuckDB installed successfully!")

If you see the message, DuckDB is installed correctly. If not, check for errors.

Basic Usage of DuckDB

Now that DuckDB is installed, let's use it. Here’s a simple example to create a table and query data.

 
# Connect to DuckDB
conn = duckdb.connect()

# Create a table
conn.execute("CREATE TABLE test (id INTEGER, name VARCHAR)")

# Insert data
conn.execute("INSERT INTO test VALUES (1, 'Alice'), (2, 'Bob')")

# Query data
result = conn.execute("SELECT * FROM test").fetchall()
print(result)


[(1, 'Alice'), (2, 'Bob')]

This shows DuckDB is working. You can now use it for more complex queries.

Common Issues and Fixes

Sometimes, you may face issues. Here are common ones and their fixes.

Issue 1: Installation fails due to pip version. Update pip using pip install --upgrade pip.

Issue 2: DuckDB not found after installation. Reinstall it or check your Python environment.

Conclusion

Installing DuckDB in Python is simple. Just use pip install duckdb and verify the installation. Now you can use DuckDB for fast data analysis.