In the mid-18th century, the region was colonized by the Spanish and became part of the Viceroyalty of Peru. Following the Peruvian and Bolivian wars of independence, which ended in 1826, the region and large portions around it became part of Bolivia, but independent of Spain. It was a territory of the short-lived Peru–Bolivian Confederation (1836–1839), until the two countries separated and the region returned to Bolivian control.