Sono qui da trentuno anni.
Sono qui da trentun anni.
Sono qui da trentun'anni.
Yes, the first is correct, but which one between the second and the third is correct?
The correct form is the middle one. The first is not incorrect, but not used except in particular cases. The third form is wrong (at least under the current orthography rules).
Trentuno follows the same rule as uno (indeterminate article) discussed in Article comparison in "un francese e due svizzeri" and "uno svizzero e due francesi". The truncation is not really compulsory, but very common.
It's even commoner with the indeterminate adjectives ending in uno, nessuno being the most commonly used. Nobody would say nessuno amico, but trentuno amici can be heard when the emphasis is on the number, say in aveva ben trentuno amici.
Speaking of apocope, ben is a peculiar word, but this would take too far.
The second is correct. It's apocope (omission of the final sound of a word), which doesn't need an apostrophe. The rule is: anything ending with -uno, including uno as an indeterminate article, as well as alcuno, nessuno, ciascuno, etc., before the masculine words, starting with a vowel or with a consonant other than s impura/z/gn/ps, drops the final letter.
Examples: un uomo, alcun pensiero, nessun dubbio, ciascun anno, etc.