Police received information that drugs might be at the scene but instead found the weapon, magazines and live rounds during a search.
At the High Court in Edinburgh, Judge Lord Boyd of Duncansby told Stuart Campbell: "You have pleaded guilty to serious offences under the Firearms Act. You had in your possession a Sten Mark II sub machine gun together with 12 bullets that could be fired from the gun."
Lord Boyd said he considered it a particularly serious offence because of the nature of the weapon and that it was found with ammunition.
But the judge said he was also prepared to accept that the crime was "out of character" for Campbell and added he was prepared to limit the jail term to the minimum period set by Parliament.
Campbell, 27, of Bryson Street, Clydebank, earlier admitted possessing the prohibited weapon and ammunition on May 1 last year.
During searches, keys for a garage in Abbeylands Road, Clydebank, were found and inside it black bags were discovered containing the gun and components.
Campbell told police that only he and a friend used the garage for their bikes.
The former loft insulator later told officers that he had got into debt of about £5000 and was struggling to pay it back.
He was asked to hold the items found in the garage to clear his debt, but declined to name the person who approached him to store them.