Bradley Manning, the 23-year-old army private from Oklahoma alleged to have been behind the biggest US government leak of all time, is now in Fort Leavenworth military jail, Kansas. He faces 34 charges, and if convicted could face a prison sentence of up to 52 years.

So why did the US army ignore warnings from officers that Manning was unstable? Why did it send him – a 5ft 2in gay man with a history of being bullied in the military – to one of the most isolated and desolate bases in Iraq? Why was security so lax on the base that passwords for secret military computers were posted on sticky notes nearby?

A year after his arrest, a Guardian investigation reveals a trail of ignored warnings, beatings and failed personal relationships that led to Manning's arrest on 29 May 2010.

Manning, the son of a former naval intelligence analyst, Brian Manning, and his wife Susan, was brought up in the small town of Crescent, Oklahoma. Neighbours watched the family disintegrate as Susan Manning turned to drink to ease the final years of the marriage.

"I never saw her plastered, but … I'd go by there at two o'clock in the morning and the lights would be on. I think she did her drinking when he'd go to bed," one neighbour, Bill Cooper, said.

In 2001, when Manning was 13, his parents divorced and he moved with his mother to her home town of Haverfordwest, in west Wales, where he joined the local school, Tasker Milward comprehensive. Small, geeky and speaking with an Oklahoma accent, Manning was an obvious target for teasing, and he reacted furiously to it, friends recalled.

"Bradley's sense of humour was different to the rest of ours, whereby the rest of the school kind of goad and tease each other," said schoolfriend Tom Dyer. "He was far too literal for that, and so would often snap back if he thought the joke had gone too far, which would cause laughter for everyone else."

When he was 17, by which time he was openly gay, Manning returned to Crescent to live with his father, who had remarried. The software job his father promised in Tulsa didn't work out – and neither did his relationship with his stepmother and her son. "I am nobody now, Mom," he wrote to his mother.

In March 2006 his stepmother called the police, saying that he was "out of control". Manning left home, and for the next year slept on friends' couches or in his pickup truck in other people's driveways, earning money in a series of casual jobs in restaurants and coffee shops.

Manning was keen to work with computers but quickly realised there would be no job for him without a degree. Joining the US army, he decided, would be his best chance of getting one as they would help pay for it through the GI bill's provision for soldiers' education.

"He joined the army because he wanted to go to university," said Keith Rose, a friend of Manning's from time he spent in Boston.He said the army's attitude towards gay people did not put Manning off. "I asked him that night how he could join, given the army's attitude towards gays. He told me he was a patriot but there were benefits too. He wanted to go to university."

In October 2007, Manning joined up. He was far from typical soldier material. He was smart, gay, physically weak and politically astute. "He knew all kinds of things," said Rose. "He was heavily educated in science. He knew math. He knew what was going on in the world."

He enlisted in October 2007 and was sent to Fort Leonard Wood, Missouri, for basic training, but in just over a month he was moved to a discharge unit and on the verge of expulsion.

One man who befriended Manning in the unit, but who wishes to remain anonymous, explained what being in the discharge unit meant. "He was not bouncing back. He's going home. You don't just accidentally end up in a discharge unit one day. You just have somebody one day saying, 'You know what, he is no good – let's get him out of here'. There are a lot of steps to go to before you even hit a discharge unit."

Manning was picked on, the friend said, and used to wet himself. "[Once] there were three guys that had him up front and cornered. And they were picking on him and he was yelling and screaming.

"We got up there – it was me and a couple of the guys – and we started breaking it up. We were saying, "Get the hell out of there, back off," and everything – and started pulling Manning off. The other guys were taking care of the ones that were picking on him and stuff. I got Manning off to the side there and yeah, he pissed himself. It wasn't the only time he did that, but that was the only time that I remember. It happened a few other times, the other guys will probably tell you the same story. Just a different circumstance."

Despite the concerns of his immediate superiors, Manning was "recycled" instead of being discharged. The war in Iraq was in its fourth year and the army was short of recruits.

In August 2008, after training as an intelligence analyst, he was stationed at Fort Drum in upstate New York while he awaited deployment to Iraq. Here he was considered a "liability" by superior officers.

His weekends at Fort Drum were occupied by visiting his first serious boyfriend, Tyler Watkins, a student at Brandeis University, near Boston. Watkins began taking Bradley to events at his university's gay, lesbian, bisexual and transgender society, Triskelion, and introduced his computer-loving boyfriend to Danny Clark, a student at MIT. Through Clark, the Boston "hacktivist" scene – consisting of some of the world's most prominent and smartest hackers – opened up to Manning.

Here Manning appeared to have found his place. He appears in photographs looking tanned and happy in Pika House, a clapboard communal student residence in the suburbs of Cambridge, Massachusetts.

The pictures appeared in a Facebook album Bradley captioned: "Randomly hung out with some pikans." Bradley's snaps were mostly of the tie-dyed T-shirt wearing Clark. The rest of the pictures are of the jumble of gadgets, electronics and posters that cover the house.

The day Manning was posted to Iraq in October 2009, Watkins went to a gay march wearing a placard that read "Army wife". Manning was deployed to Forward Operating Base Hammer, one of the most isolated US posts in Iraq, in the desert close to the Iranian border. Veterans recalled a desolate place built mainly from freight containers.

"There was a fog that would come in almost every morning that was pollution from nearby that smelled sour and nasty, and would just wave through and linger," said Jacob Sullivan, who served alongside Manning at Hammer.

Hammer's overriding culture was one of boredom and casual bullying, where bored non-commissioned officers picked on juniors. "They had a saying, 'Shit rolls downhill,' " said Jimmy Rodriguez, 29, an infantry soldier who was stationed at the base with Manning.

For entertainment, soldiers would download porn to workstations or access footage from Apache attack helicopters showing civilians being shot at, often through SIPRNet, the classified intelligence network used by the state department and department of defence.

It was data downloaded from this network that would later find its way to the whistleblowing website WikiLeaks.

According to Sullivan, security was extremely lax. "If you saw a laptop with a red network wire going into it, you knew it was on SIPRNet. If you had the password you could access SIPRNet. Everybody would write their password on sticky notes and set it by their computer. There is no wonder something like this transpired."

According to Peter van Buren, a civilian reconstruction team leader on the base, there was a sense of a security free-for-all about SIPRNet.

"Soldiers would call it 'war porn' or 'the war channel' or just 'war TV'. It was hypnotic to watch, even when not much was happening, just this lazy overhead view of the world around you. For many soldiers, it was all they ever saw of Iraq," Van Buren said.

"I saw them using the SIPRNet for entertainment. That's what most of the people did most of the time," said Rodriguez. "They would watch these videos of different things and some of them were videos of helicopters attacking people or drones or whatever the case, or maybe fighter jets. But they were watching military footage.

"We were pretty much bored all the time," he recalled. "When you got to Iraq, we got to Baghdad and ended up in Forward Operating Base Hammer. They would [say to] us: 'Here's the videos; here's the internet; here's all the interesting games.' "

In January 2010 Manning went on leave and visited his friends in Boston, including Watkins. It became clear their relationship – one of the most significant in his life – was near its end.

That January, Rose recalled, "Bradley was really down. Tyler was like an anchor for Bradley and the one constant for that entire year. He gave me a two-hour earful about all the things in the relationship that he didn't understand. He had gone in the military and come back and he didn't have his relationship anymore."

While his relationship with Watkins was souring, Manning socialised with Clark at the launch party for Builds – a hackers' playground in Boston University's computer science faculty where they would simulate unlocking codes and bypassing online security.

Film footage shows him leaning against a table – a soldier in his collared shirt, he looks very different from the grungy student hackers at a top university. Nevertheless, he appears comfortable inside this elite circle. Less than a week later, Manning was back at his intelligence analyst's computer in Iraq.

"I live in a very real world, where deaths and detainments are just statistics; where idealistic calls for 'liberation' and 'freedom' are utterly meaningless," Manning wrote in a final message to Watkins on Facebook. "I don't have a real place to call home, except for a trailer with a bunk, a laptop, and an alarm clock. Please don't let the LAST PERSON that I trust and care about go away. I haven't given up."

On 5 May he wrote of being "beyond frustrated with people and society at large", and a day later, on 6 May, he wrote: "Bradley Manning is not a piece of equipment." On 7 May Manning was found in a foetal position in a storeroom after stabbing a chair with a knife as he tried to carve the words "I want" into the seat. He had punched his commanding officer, a woman, in the face.

He was disciplined and demoted and told he was to be finally discharged from the army on grounds of "adjustment disorder". In the space of a few weeks, he had lost his job, his boyfriend and his chance of a university education.

During the following fortnight, Manning turned back to his computer and his hacker friends. He began chatting with someone who didn't even know him, hacker Adrian Lamo.

In the early hours of 25 May, Manning had his last conversation with Lamo. The following day Lamo reported him to the authorities and he was escorted from his computer room. After three days of questioning he was charged in relation to the biggest intelligence leak in US military history.

The US military has refused to make any comment on Manning's mental health record other than to confirm it is being investigated. He is due to be courtmartialled in December.