At least 3 hours before bedtime is usually a good practice, as others noticed though it may be possible for one to exercise just before and have a good nights sleep.
Exercising in the evening is optimal for muscle growth as power is maximal[0] and it's better for recovery, though there are other factors such as daily schedule and I've often heard people saying that working out at morning has a good psychological effect.
Whatever is best for ones consistency and goals.
0 - https://youtu.be/zOMR73FC4Qw?t=638
Further references: