Clion with wsl2

I will follow the official docs

If you have not installed WSL2, please go to this blog

After installing WSL2 and CLion, open windows PowerShell as administrator and run

1
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Then restart your computer
open ubuntu and run this command to install c++ tools

1
sudo apt-get install cmake gcc clang gdb build-essential

You will wait a long time.(here I use powershell, I suggest you use ubuntu tu run this command)

Then run this command to open the script

1
wget https://raw.githubusercontent.com/JetBrains/clion-wsl/master/ubuntu_setup_env.sh && bash ubuntu_setup_env.sh

If you are in China, you may can’t download the script by this command

But don’t worry, this is the content of the script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
set -e

SSHD_LISTEN_ADDRESS=127.0.0.1
if [ -e "/dev/vsock" ]; then # in case of WSL2
SSHD_LISTEN_ADDRESS=0.0.0.0
fi

SSHD_PORT=2222
SSHD_FILE=/etc/ssh/sshd_config
SUDOERS_FILE=/etc/sudoers

# 0. update package lists
sudo apt-get update

# 0.1. reinstall sshd (workaround for initial version of WSL)
sudo apt remove -y --purge openssh-server
sudo apt install -y openssh-server

# 0.2. install basic dependencies
sudo apt install -y cmake gcc clang gdb valgrind build-essential

# 1.1. configure sshd
sudo cp $SSHD_FILE ${SSHD_FILE}.`date '+%Y-%m-%d_%H-%M-%S'`.back
sudo sed -i '/^Port/ d' $SSHD_FILE
sudo sed -i '/^ListenAddress/ d' $SSHD_FILE
sudo sed -i '/^UsePrivilegeSeparation/ d' $SSHD_FILE
sudo sed -i '/^PasswordAuthentication/ d' $SSHD_FILE
echo "# configured by CLion" | sudo tee -a $SSHD_FILE
echo "ListenAddress ${SSHD_LISTEN_ADDRESS}" | sudo tee -a $SSHD_FILE
echo "Port ${SSHD_PORT}" | sudo tee -a $SSHD_FILE
echo "UsePrivilegeSeparation no" | sudo tee -a $SSHD_FILE
echo "PasswordAuthentication yes" | sudo tee -a $SSHD_FILE
# 1.2. apply new settings
sudo service ssh --full-restart

# 2. autostart: run sshd
sed -i '/^sudo service ssh --full-restart/ d' ~/.bashrc
echo "%sudo ALL=(ALL) NOPASSWD: /usr/sbin/service ssh --full-restart" | sudo tee -a $SUDOERS_FILE
cat << 'EOF' >> ~/.bashrc
sshd_status=$(service ssh status)
if [[ $sshd_status = *"is not running"* ]]; then
sudo service ssh --full-restart
fi
EOF


# summary: SSHD config info
echo
echo "SSH server parameters ($SSHD_FILE):"
echo "ListenAddress ${SSHD_LISTEN_ADDRESS}"
echo "Port ${SSHD_PORT}"
echo "UsePrivilegeSeparation no"
echo "PasswordAuthentication yes"

copy this content, and follow me
go to the linux file system

go to this path and create a txt named ubuntu_setup_env.sh, paste the content to this file.

then run this command.
Notice: you should get into the path where you save your bash ubuntu_setup_env.sh file.

1
bash ubuntu_setup_env.sh

It will help us config the ssh

Then run this command, notice the username should be your username

1
ssh username@localhost -p2222

!NOTICE: before you connect to the WSL, you must turn off your fast startup function, or the localhostForward will not work!

Now open your Clion and go to Settings / Preferences | Build, Execution, Deployment | Toolchains and click plus icon to create a new toolchain. Select WSL from the Environment list.

Then click Credentials setting

Fill your information of your WSL, then test connection and OK

If you meet a error, that mean you need to config a file, let me tell you how to solve it.
go to your path like me

create a file named .wslconfig and its content is

1
2
[wsl2]
localhostForwarding=true

save it and restart your WSL2 on powershell

1
wsl --shutdown

then open WSL2 again

Finally, open Clion you will find it work successfully

Have a good time!