The \(S\)-wave \(\pi K\) scattering lengths are calculated for both the isospin 1/2 and 3/2 channels in the lattice QCD by using the finite size formula. We perform the calculation with \(N_f=2+1\) gauge configurations generated on \(32^3 \times 64\) lattice using the Iwasaki gauge action and nonperturbatively \(O(a)\)-improved Wilson action at \(1/a = 2.17\) GeV. The quark masses correspond to \(m_\pi = 0.30 - 0.70\) GeV. For \(I=1/2\), to separate the contamination from excited states, we construct a \(2 \times 2\) matrix of the time correlation function and diagonalize it. Here, we adopt the two kinds of operators, \(\bar{s}u\) and \(\pi K\). It is found that the signs of the scattering lengths are in agreement with experiment, namely attraction in \(I=1/2\) and repulsion in \(I=3/2\). We investigate the quark-mass dependence of the scattering lengths and also discuss the limitation of chiral perturbation theory.