Settlements on the castle hill have existed since the Bronze Age. The location was probably occupied in Roman times due to its strategic location, and the discovery of several artefacts from this period.
The first written reference to the castle is from 672, when the fort was on the northern Visigoth border and known as Castrum Libyae, and had joined the rebellion of Hilderic, the Duke of Narbonne, and General Paulus against King Wamba.
From Llívia, still perched above the castle, Catalonia was beginning to form, and it was the county town of Cerdanya and home of some of the counts. With the death of the last count in Cerdanya, Bernat Guillem in 1117, sovereignty passed to the House of Barcelona. Soon after Puigcerdà was founded, and the town lost administrative, but not strategic, importance. For this reason, Jaume I gave royal approval to the people of Llívia to inhabit the lower slopes of the hill, on the condition that they would never abandon the fortress. The new castle was built during the 13th century, and during the 14th and 15th centuries it was the venue for many confrontations: the reunification of the Crown of Aragon and also after the Catalan Civil War, when Joan II taxed the counties.