How to make SQLite work with Diesel ORM for Rust on Windows

Windows is generally difficult to develop on, unless you are using the Windows Subsystem for Linux (WSL). I had to set up SQLite with Diesel ORM for a Rust project on Windows 10 and it was a bit tricky.

Step 1: Install Rust and Microsoft C++ Build Tools

  1. Rust: Download the installer from https://www.rust-lang.org/tools/install and run it.
  2. Microsoft C++ Build Tools: follow the steps listed on the Tauri guide.
How to check if Rust is installed: Run this in Powershell or Command Prompt – rustc --version. It should output something like rustc 1.76.0 (07dca489a 2024-02-04).
How to check if Microsoft C++ Build Tools are installed: In the Start menu, you should have an app called Developer Command Prompt for VS 2022. If you don't have it, the Build Tools are not installed. Follow the steps listed on the Tauri guide again.

Step 2: Download and Compile SQLite

  1. Go to SQLite’s download page and look for the heading Precompiled Binaries for Windows. Download the zip file for the 64-bit DLL. As of March 2024, you can use this direct link. But it’s better to check the download page for the latest version.

  2. In the C:\ drive, create a new folder called sqlite64. Extract the zip file’s contents into that folder. The folder should look like this –

C:\sqlite64\
  ├── sqlite3.def
  ├── sqlite3.dll
  ├── sqlite3.exp
  1. Next, open the Developer Command Prompt for VS 2022 from the Start menu. Run the following commands –
cd C:\sqlite64
lib /def:sqlite3.def /out:sqlite3.lib /machine:x64

This should add a new file called sqlite3.lib to the C:\sqlite64\ folder.

Step 3: Add SQLite to the system PATH

  1. Type “environment variables” in the Start menu and click on Edit the system environment variables.
  2. A System Properties window should open up. Under the Advanced tab, click on the Environment Variables button (at the bottom of the window).
  3. Under User variables, click on Path and then click on Edit. Click on New and type – C:\sqlite64. Press OK.
  4. Under User variables, click the New... button. In the Variable name field, type SQLITE3_LIB_DIR and in the Variable value field, type C:\sqlite64. Press OK.
  5. Press OK. Press OK again.

Step 4: Add Diesel to the project

Add Diesel to your Rust project by running –

1
cargo install diesel_cli --no-default-features --features "sqlite-bundled"

Fin.

That should do it! I figured this out by parsing the angry comments on this GitHub issue.

Note: If you are coming from a UNIX environment, there is something you should know about sqlite paths on Windows. They look like this:

sqlite:///C:\Users\Username\Documents\database.db
  1. The beginning should have sqlite:///. Yes, that’s THREE slashes. And yes, they are slashes, not backslashes.
  2. The path in your Rust code should have double backslashes: C:\\Users\\Username\\Documents\\database.db.

Here’s why.