How do I get a version of Android with linux kernel >= 3.18 on a Nexus 5? According to this post Android 7.1 has kernel 4.1.1 and Android 6.0 has kernel 3.18.10.

I installed Android 6.0 on a Nexus 5 (using the latest google image) and ended up with kernel 3.4. Then I installed 7.1 on a Nexus 6 (also using the latest google image) and ended up with kernel 3.10.

I could go try on a newer version yet on a newer phone but I'm doing this to get dwarf-based callstacks on perf runs and I really want to do it on the oldest phone possible.

Has anyone  gone through this before?
Android devices generally don't receive major kernel upgrades, and Nexus devices certainly never did. Even when OS upgrades take place they only get minor-version kernel patches. The table in the other question is intended to represent what a new device would ship with given the first version of Android it supported, but even that is not a hard-and-fast rule.

For your specific example, Google has no code for either the N5 or N6 with a 3.18 kernel. Both remained on their original major versions (3.4.y and 3.10.y, respectively) for their entire lifetimes. You can check the "hammerhead" and "shamu" related tags in the kernel/msm repo to verify this. If you need a newer kernel on one of those devices you're likely going to have to port it yourself, unless you can find another 3rd party that already undertook this work.