Do Berkley DB versions need to be the same? If I build using --with-incompatible-bdb, will the resulting wallet be incompatible with the bitcoin core client from bitcoin.org? What if I'm using a newer version?
To be safe, you should use BDB 4.8 as that is the version that is used in releases. However using --with-incompatible-bdb should still produce wallet.dat files that can be used with the officially built clients.

You may find that people say that the BDB database files are incompatible from version to version. Furthermore Oracle, in their own documentation, say that there are no guarantees that database or environment files are compatible from version to version, even from minor version to minor version. But from researching and testing this out myself, this is not true in practice.

BDB rarely changes the database file format. Rather what does tend to change are the database transaction logs. These log files are used to ensure Durability (the D in ACID). Oracle has changed the log file format in minor versions so they are not backwards compatible (they are forwards compatible, so you can always move to a newer version of BDB). When you use --with-incompatible-bdb, you will probably be using a newer version of BDB that has incompatible transaction logs. So this would be a problem.

But Bitcoin Core actually mostly takes care of that problem for you. The transaction logs are removed if the wallet is able to be closed cleanly. So if nothing crashes and the wallet file is closed normally, you will be able to use a version of Bitcoin Core compiled with a different BDB, and then have the official release be able to use the same datadir and open the same wallet files.

In the event that Core crashes or the wallet files are otherwise uncleanly closed (e.g. power loss), then you should use a build of Bitcoin Core using the same BDB version that you had been using prior to the unclean shutdown. This will ensure that the transaction log can be read correctly and any information replayed and written to the wallet.dat file.

So what versions are compatible? From my own testing, I have found the following versions to work with no modification to Core necessary (Note these versions are what Oracle has available for download, versions with the same major and minor number of different patch number should still work):


4.8.30 (Note this is the version that Core ships with)
5.0.32
5.1.29
5.2.42
5.3.29


6.0 and above are not backwards compatible as they database file format has changed for these versions. You would be able to use 6.0+ but any wallet.dat files created by them cannot be used in the official binaries. However existing wallets created with the official binaries or one of the above versions of BDB could be used with a build that uses 6.0+ and be subsequently used again in the builds with older BDB versions. I.e. you could go from 4.8 to 6.0 and back to 4.8 with the same wallet.dat file.

4.7.25 also works but requires you to modify configure.ac to be able to build with a version less than 4.8.

4.6.21 requires modifying some code as API calls changed, but the database format still works. 4.5.x and below have significantly more API differences so I wasn't able to test them.