Use of the castle area goes back to Roman times as evidenced by the existence of two Roman cisterns, tombstones, coins and other artifacts. Primitive fortifications date back to the time of the Goths. Its first walls were constructed by North Africans during the time of Muslim control of Portugal. These walls were destroyed during the Christian reconquest when the castle was seized in 1148 by Afonso I of Portugal, but were immediately rebuilt. In 1288 King Denis ordered that the defences be strengthened and extended and Ferdinand I of Aragon ordered further improvements in 1373. The Castle of Torres Vedras has served as a temporary residence for several Portuguese kings. It was here that King John I gathered the Council that decided on the conquest in 1415 of Ceuta, a Spanish enclave in North Africa, that marked the beginning of the outward-looking policies of Portugal. In 1510, Torres Vedras received a municipal charter, and six years later King Manuel I, ordered reconstruction of the castle. Around 1520, the Palace of the Alcaides was constructed by the Chief Magistrate, João Soares de Alarcão. It remains the dominant feature of the castle, despite being in ruins. In 1589, during the Philippine dynasty, forces of António, Prior of Crato commanded by Sir Francis Drake disembarked in Peniche and briefly took the Castle of Torres Vedras. The castle suffered heavy damage from the 1755 earthquake, particularly the Palace of the Alcaides and some of the walls. In 1809, it was integrated into the Lines of Torres Vedras, which were defensive lines established by the Duke of Wellington to protect against French invasion during the Peninsular War. Together with the Fort of São Vicente on a hill immediately to its north, the castle, with about 500 soldiers, guarded the main road between Coimbra and Lisbon. As part of the adaptation by the British Royal Engineers for this purpose the castle door was demolished. During the Liberal Wars, sections of the walls facing east and some turrets on the north side were rebuilt. During the so-called Little Civil War in 1846, it served as barracks for the troops of the Count of Bonfim and was bombarded by the forces of the Duke of Saldanha, which caused the almost total ruin of the Palace. Despite its poor condition, the castle continued to function as a barracks until the end of the 19th century. In 1929, the Ministry of War transferred the property to the municipality of Torres Vedras. In 1955 there was a collapse of part of the wall to the northwest. In 1957 the castle was classified as a Property of Public Interest and this was followed by several repairs being carried out but there remained the danger of collapse to part of the walls, which happened in 1963, leading to additional repairs.