In 1830 he traveled throughout Europe, finally returning to Italy to begin a seven-years cohabitation with Leopardi, until the latter's death. This cohabitation, which was also the subject of rumors, was the central moment in Ranieri's life and made him famous as the "guardian" of the last years of the genius of Recanati. Leopardi's deep affection for Ranieri transpires from certain letters he wrote to him during a brief moment of separation, for example: . After staying in Rome and Florence to follow Ranieri's mistress, actress Maddalena Pelzet, he and Leopardi met the noblewoman Fanny Targioni Tozzetti, whom both loved: Ranieri with success, Leopardi not. In 1833, Ranieri moved to Naples with Leopardi. Ranieri did not find a good welcome in his hometown, due to the controversial cohabitation with Leopardi and the unconventionality of the latter's thought. Nevertheless, he dedicated himself to historiography and literature, being favored by the association with Leopardi and close to his thought. His writings, marked by anti-clericalism, attacks on institutions and Italian patriotism, caused him troubles with censorship.
Leopardi's death and burial controversies
To defend himself from the cholera epidemic, in 1836 Ranieri moved with Leopardi to Torre del Greco, in a friend's country house. On 14 June 1837, when they returned to Naples, Leopardi died of heart dropsy, as Ranieri reported – even if there is a strong suspicion that the poet died of cholera, and that Ranieri lied in order to hide a less decorous death. Together with his sister Paolina, Ranieri assisted the poet to the end and, as he himself reported in his memoirs and his letters to Monaldo Leopardi, prevented the remains from being thrown into a mass grave, having him buried first, and clandestinely, in the crypt, then in the atrium of the Church of S. Vitale in Fuorigrotta, a quarter of Naples. Nevertheless, Ranieri's story had immediately appeared full of contradictions and many doubts arose about what he had declared, also because his versions were many and different depending on the interlocutor, making one suspect that the poet's body was ended up in the mass graves of the Fontanelle Cemetery, or in the Cemetery of the 366 Fossae in Naples, or even hidden in the Neapolitan house where the death had occurred, and that Ranieri had staged an empty coffin funeral, with the participation of his own brothers and a corrupt priest. Ranieri continued to state that the bones were in the atrium of the Church of S. Vitale and that the burial certificate was a forgery written by the priest in order to circumvent the law on burials in times of epidemic. However, it is proved that Ranieri lied in other cases: for example, he long maintained that he did not know where the 4,000 pages of Leopardi's Zibaldone di pensieri were: they were finally found in his house. On 21 July 1900 the official reconnaissance of Leopardi's remains was carried out and in the coffin – too small to contain the skeleton of a man with double hump – only fragments of bones, a heeled shoe and some rags, while there was no trace of the skull and the rest of the skeleton.
Later years
Forty years after Leopardi's death, Ranieri published the work that consecrated his fame to this day, Sette anni di sodalizio con Giacomo Leopardi, a memoir followed by bitter controversies over the author's self-congratulation, the overabundance of details unfavorable to the poet's memory and the absence of reflections on his intellectual greatness. However, though inaccurate and inappropriate in some descriptions, he offered a testimony of the biographical events of the two friends. In 1861, Ranieri was elected as a Deputy to the Parliament of the Kingdom of Italy, being repeatedly confirmed until 1881. On 16 November 1882, he became a Senator of the Kingdom of Italy. He died in Portici on 4 January 1888.