The asymptotic you want does not hold just because the quantity"last-term fluctuation" $$ g(n)-g(n-1) = 2\sum_{a=1}^n \gcd(a,n)-n $$ is too large. DenotingIndeed, denoting the sum in the right-hand side by $\sigma(n)$, we have $$ \sigma(n) =\sum_{d\mid n} d\varphi(n/d) = n \sum_{d\mid n} \prod_{p\mid(n/d)} \Big(1-\frac1p\Big) = n \sum_{d\mid n} \prod_{p\mid d} \Big(1-\frac1p\Big). $$ If
If now $n=p_1\dotsb p_k$ is the product of the first $k$ primes, then $$ \sigma(n) = \Big(2-\frac1{p_1}\Big)\dotsb\Big(2-\frac1{p_k}\Big)\,n \ge (3/2)^k n. $$$$ \sigma(n) = \Big(2-\frac1{p_1}\Big)\dotsb\Big(2-\frac1{p_k}\Big)\,n \ge (3/2)^k n $$ On the other handwhile, using the prime number theorem, it is not difficult to see that $k\ge c\ln n/\ln\ln n$ with an absolute constant $c>0$. It follows that for $n=p_1\dotsb p_k$ we have; hence, $$ \sigma(n) \ge \exp(c'\ln n/\ln\ln n)\,n $$$$ \sigma(n) \ge \exp(c'\ln n/\ln\ln n)\,n,\ n=p_1\dotsb p_k. $$ whileOn the other hand, for $n$ prime, $\sigma(n)=2n-1$. Hence, we have $$ \sigma(n) = 2n-1. $$ It follows that one cannot get aan asymptotic for $g(n)$ with the remainder term better than $O(\exp(c'\ln n/\ln\ln n)\,n)$.