It is recommended to use Berkeley DB 4.8. If you have to build it yourself, you can use the installation script included in contrib/may be used for UPnP port mapping. It can be downloaded from here. like so:
from the root of the repository.
Otherwise, you can build Bitcoin Core from self-compiled depends.
Note: You only need Berkeley DB if the wallet is enabled (see Disable-wallet mode).