Ten others were wounded, one seriously, in the attack at a flag-raising ceremony at the Bouchoucha barracks.
The man had family and psychological issues and was “forbidden from carrying arms”, said ministry spokesman Belhassen Oueslati.
He described the incident as an “isolated act, not a terrorist act”.
There would be an investigation to try to determine the killer’s motives, he added.
Mr Ouselati said the soldier attacked one man with a knife before taking the victim’s gun and “shooting at his comrades”.
Tunisian security forces have been on alert since Islamist gunmen attacked the Bardo Museum in March, killing 21 tourists.
The shooting at the barracks, which is close to the museum and the parliament building, prompted the evacuation of a nearby school.
Police reinforcements were sent to the area to comb nearby streets, while a helicopter hovered overhead.
Interior Ministry spokesman Mohamed Ali Aroui said on national radio that there was no gunfire outside the barracks.