As far in advance as possible, especially in peak season. It is not unusual for tickets to be sold out weeks in advance, especially if you want exclusive access or tours to other areas that only take small groups, such as the gardens or the Papal summer residence.