Upon arrival at the bus station, Guadix looked like any other small Andalucían town, with whitewashed townhouses and charming, shaded plazas. But as I climbed to the top of the hill, a strange Martian-like landscape appeared before me.

Russet-coloured craggy hills punctuated the skyline and vast desert-like plains were framed by the soaring Sierra Nevada mountain range. Upon closer inspection, I could see small white chimneys poking out of the hills and front doors cut into the side of the rock, like Hobbit homes.

I had arrived at the Barrio de Cuevas or the Neighbourhood of Caves, where much of the town’s population lives underground. Known as the trogloditas (troglodytes), locals have been living in these caves for hundreds of years.

“Would you like to see inside one of the caves?” an old man called out to me from a wicker chair on his patio, the words Cueva de José (José’s Cave) carved into a piece of wood next to his front door.

I had heard that the troglodytes were friendly and welcoming, but I hadn’t expected to be invited inside so soon after arriving. I quickly accepted.

“Many people think of caves as damp, dirty and dark places, but as you’ll see, this is not the case in Guadix at all,” José said, as he beckoned me to follow him into his quirky abode.

He proudly led me around his home, showing off his various rooms and possessions. The terracotta farm-style kitchen was modern yet rustic, with dried red peppers, onions and garlic hanging from the ceilings and walls. José then showed me into a large dining room, where a long wooden table was covered with bowls of dried fruit and vegetables, and the walls were decorated with religious icons and other cultural trinkets. I was surprised at how large and spacious it all was.

“Come, come,” José said as he motioned for me to go further inside. He showed me his bedroom, which held a large gold bed, religious paintings on the walls and a frilly red flamenco dress hanging in one corner.

“This dress belongs to my family. Flamenco is still a very important part of our culture here, like it is in many parts of Andalucía.”

The room he seemed most proud of though was his bathroom, which was small with a built-in shower. It seemed comfortable enough, but I couldn’t help but wonder what it would actually be like to live in a cave.

“I like it,” José exclaimed, “but I couldn’t imagine it any other way. My family has lived here for generations. I have everything I ever need here. I couldn’t picture exchanging it for a normal house. It gets very hot here in the summer, and we are protected from the heat down here. The caves act like a natural air-conditioner. Being underground also keeps it relatively warm during the cold winters, too.”

José said that he believed the cave was around 500 years old, but of course now it had been renovated and fitted with modern furnishings and appliances.

He then took me up to his terrace, which sat just above his house. From here, I had an incredible view over Guadix.

“Most of my friends live in caves and many of the public buildings are located in caves too. That’s the church over there,” José said, as he pointed to a small white chimney poking up from a round rocky façade. “And over there is the Cave Interpretation Centre, also housed underground. Look behind us and you’ll learn even more about the origins of Guadix.”

I turned and saw a turreted, Arabic-style castle, towering over the town.

“That was built by the Moors when they ruled our town around 1,000 years ago,” José said.

After thanking José for his hospitality, I headed towards the Cave Interpretation Centre, where I learned that the area where Guadix stands today is one of Europe’s oldest settlements, dating back to the Stone Age. The main town, however, was founded by the Romans to mine the silver found in the nearby hills. It was later taken over by the Moors when they ruled Andalucía from around the 8th to the 15th Centuries, building the Guadix Alcazaba, the magnificent Moorish fortress I had seen from José’s terrace, around the 11th Century. Some of the oldest caves are believed to have been built during the early period of their rule, and date back around 1,000 years, although most were built during the 15th and 16th Centuries.

During the Moorish time, Guadix was an important trade town, as it was midway between the city of Granada, the stronghold of the Alhambra Palace, and the sea. When the Catholic monarchs regained control of Andalucía and took Granada in 1492, many of the Moors were displaced and fled to the surrounding mountains and the town of Guadix. More people fled here from 1568 to 1571 during the Rebellion (or War of the Alpujarras). When they arrived and had nowhere to live, many of the Moriscos (Moors who had been forced to become Christian) decided to build their homes underground, primarily to escape the heat. Far from being natural caverns in rock like most caves, however, the ones in Guadix were actually chiselled out of the earth.

While some nearby towns such as Baza and Huescar also have cave districts, Guadix has more than 2,000 underground dwellings, making it home to the largest number of cave homes in all of Europe.

Until a few decades ago, Guadix’s cave homes were seen as houses for the poor, considered inferior to the town’s more modern houses. Today, however, they are becoming more popular, and many local residents are transforming these grotto-like dwellings into restaurants, hotels and even holiday rentals.

Upon my return to the bus station, I noticed a big sign scrawled across the exterior wall of one of the caves: Se Vende (For Sale). I couldn’t help but wonder how much it would cost to buy it.

If you liked this story, sign up for the weekly bbc.com features newsletter, called “If You Only Read 6 Things This Week”. A handpicked selection of stories from BBC Future, Earth, Culture, Capital, Travel and Autos, delivered to your inbox every Friday.