Allan Peach detonated the home-made bomb at a tenement in Dundee, blowing a number of doors off their hinges.
Peach, 24, set off another device earlier on the same evening but no-one was hurt during either incident.
At the High Court in Glasgow yesterday, judge Lady Stacey told Peach: "What you did was culpable and reckless. It could have caused a great deal more damage than it actually did. I have to take a serious view of this, however you are previously of good character and it is a great pity to see you in this situation because of drugs which you misused."
The court heard Peach had depression and had been taking a legal high which resulted in a change of personality.
The blasts occurred in Dundee on June 7 this year.
Prosecutor Paul Kearney told how Peach had bought several 1kg bags of explosive substances online, including potassium nitrate and aluminium and sulphur powders.
Around 9pm that evening, he threw the first of his bombs into the garden area of a block of flats in Cullen Place, Dundee. He then built another improvised device with a hair gel tub.