To show that group G is abelian if $(ab)^3 = a^3 b^3$ and the order of $G$ is not divisible by 3 Let $G$ be a finite group whose order is not divisible by $3$.
  suppose $(ab)^3 = a^3 b^3$ for all $a,b \in G$. Prove that $G$ must be abelian.


Let$ $G be a finite group of order $n$. As $n$ is not divisible by $3$ ,$3$ does not divide $n$ thus $n$ should be relatively prime to $n$. that is gcd of an $n$ should be $1$.
  $n =  1 ,2 ,4 ,5 ,7 ,8 ,10 ,11, 13 ,14 ,17,...$
 further I know that all groups upto order $5$ are abelian and every group of prime order is cyclic. when it remains to prove the numbers which are greater than $5$ and not prime are abelian.

Am I going the right way?
 please suggest me the proper way to prove this.
First note that given condition says that $ f: G \to G$ defined as $x \to x^3$ is an injective homomorphism of $G$.

Further Note that
$$
\forall a,b \in G: \quad ababab = (ab)^{3} = a^{3} b^{3} = aaabbb.
$$
Hence,
$$
\forall a,b \in G: \quad baba = aabb, \quad \text{or equivalently}, \quad (ba)^{2} = a^{2} b^{2}.
$$
Using this fact, we obtain
\begin{align}
\forall a,b \in G: \quad (ab)^{4} &amp;= [(ab)^{2}]^{2} \\
                                  &amp;= [b^{2} a^{2}]^{2} \\
                                  &amp;= (a^{2})^{2} (b^{2})^{2} \\
                                  &amp;= a^{4} b^{4} \\
                                  &amp;= aaaabbbb.
\end{align}


On the other hand,
\begin{align}
\forall a,b \in G: \quad (ab)^{4} &amp;= abababab \\
                                  &amp;= a (ba)^{3} b \\
                                  &amp;= a b^{3} a^{3} b \\
                                  &amp;= abbbaaab.
\end{align}
Hence, for all $ a,b \in G $, we have $ aaaabbbb = abbbaaab $, which yields
$$
f(ab) = a^{3} b^{3} = b^{3} a^{3} = f(ba).
$$
As $ f $ is injective, we conclude that $ ab = ba $ for all $ a,b \in G $.Hence $G$ is an abelian group




Added: I think it's worth mentioning that  there exist nonabelian group $G$ for which $x \to x^3$ is a group homomorphism.Smallest such example is Heisenberg group of order $27$ which can be thought of as all $3 \times 3$ upper diagonal matrices with $1's$ on the diagonal and other entries in the field of order $3$.As $G$ is  of exponent 3 ( i.e. $x^3=1$ for all $x \in G$) hence $f$ is a homomorphism and $G$ is clearly  non abelian because for example following two matrices don't commute:
$$\left(\begin{array}{ccc}
1 &amp; 1 &amp; 0\\
0 &amp; 1 &amp; 2\\
0 &amp; 0 &amp; 1
\end{array}\right),$$ $$\left(\begin{array}{ccc}
1 &amp; 1 &amp; 0\\
0 &amp; 1 &amp; 1\\
0 &amp; 0 &amp; 1
\end{array}\right)$$

In particular this also shows that the condition that $3$ does not divide order($G$) is necessary.
I have followed the similar method as previous answer with a few changes(without defining the f, thought it would be easier:) )
As $(ab)^3=a^3b^3$ for all $a,b\in G$
We have $$ababab=aaabbb$$
$$\Rightarrow baba=aabb$$
$$\Rightarrow (ba)^2=a^2b^2$$
Consider,
$$(ab)^4=((ab)^2)^2$$
$$=(b^2a^2)^2$$
$$=a^4b^4$$
$$=aaaabbbb$$
Also $$(ab)^4=abababab$$
$$=a(ba)^3b$$
Therefore, we get $aaaabbbb=a(ba)^3b$
$$\Rightarrow (ba)^3=(ab)^3$$
$$\Rightarrow (ab)^{-3}(ba)^3=e$$
Where e is identity in G
$$\Rightarrow [(ab)^{-1}(ba)]^3=e$$
Now for $x=(ab)^{-1}(ba)$ ,$|x|$ divides 3 by which $|x|$ can be 3 or 1.
Now $|x|$ can not be 3 (as by Lagrange's theorem if $|x|$=3 then 3 divides |G| which is not true.)
Thus, $|x|$=1 $$\Rightarrow x=e$$
$$\Rightarrow (ab)^{-1}(ba)=e$$
Multiplying by ab from left,
$$\Rightarrow ba=ab$$ for all $a,b\in G$
Thus, G is abelian