While you technically can buy a ticket at the visitor center upon arrival, it is highly recommended to book your visit to Petra in advance, especially if you want to experience specific events like "Petra by Night." The most important "booking" factor is actually your Jordan Pass. If you purchase the Jordan Pass online before you arrive in the country, it includes the cost of your Petra entrance fee (which is quite expensive at 50-60 JOD) and waives your entry visa fee, saving you significant money and time. If you do not have a Jordan Pass, you will have to wait in the ticket queue at the entrance in Wadi Musa. Furthermore, if you plan to stay in a specific hotel near the entrance or want a guided tour in a specific language, those should be booked weeks in advance as they fill up quickly during the peak seasons of Spring and Autumn. Petra is a massive archaeological site that requires at least one full day (if not two) to explore, so having your logistics pre-arranged allows you to start your hike through the Siq as early as possible (usually 6:00 AM) to beat the heat and the large tour bus crowds.