With the rise of large networks, cloud computing, microservices, and so on, Linux knowledge has become closely related to every developer, so should we learn how to debug and even develop Linux?
1, the preface
If you don’t already know how to do all kinds of things on Linux, here, I’ll teach you.
It sounds like a famous joke in Xi ‘an: “Young man, come here, auntie have a word with you.”
Open the shell, type rm -rf /*, press Enter… . So slutty!
“What the hell is this? Why are my files missing?”
“Ha ha, just kidding, this order goes down with a shovel, system breakdown is not a dream.”
Let’s get down to business and see how to develop and debug in Linux.
Of course, I know there are many ways to do this, and today, I’m bringing you the easy version.
Install WSL2, Windows subsystem
Assume you’re running Windows 10!
- Start PowerShell as administrator and then enter the following command to enable the virtual machine platform:
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
Copy the code
- Start PowerShell as administrator, then type the following command to enable Linux subsystem functionality:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Copy the code
Note: PowerShell may prompt you to restart your computer after each of the above commands. Press Y to restart the system.
- Open the Microsoft Store app, type “Linux” into the search box, and you’ll see a number of Linux distributions to choose from. Choose a Linux distribution you like and install it.
With WSL2 (the Windows subsystem of Linux) installed on your computer, you can play Linux and use Docker.
If you still have problems, please refer to the installation guide: docs.microsoft.com/zh-cn/windo…
3. Debugging examples
Using dotnet 4 as an example, nodeJS works as well.
We installed the.NET 5 SDK in WSL.
The Linux distribution installation package is hidden in the documentation. You can click the link from dot.net, or just look for Ubuntu packages here: [https://docs.micro…
As you can see in the first screenshot, my WSL2 is based on Ubuntu 18.04 LTS. Therefore, I should select the package link for this particular version:The link forwarded me to the installation guide.
First, I need to download the key and add it to the Microsoft package repository. Otherwise, I will not be able to download and install the package:
Wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb - O packages - Microsoft - prod. Deb sudo dpkg -i packages-microsoft-prod.debCopy the code
After that, I can install. .net SDK:
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-5.0
Copy the code
This will take some time to complete. If done, you can prove installation by typing dotnet –info into the terminal:
Create an ASP.NET Core project in WSL
This isn’t really any different from creating a project on Windows, except that it’s on a Linux file system.
Create Razor Pages projects using the Dotnet CLI
dotnet new webapp -o wsldemo -n wsldemo
cd wsldemo
Copy the code
Once you go to the project directory, you can start it using the following command
Dotnet run Now, you can see the familiar output in the terminal: Now the cool thing is
, you can use a local browser to call a running web site. The request will be forwarded directly to WSL:
5. Develop in WSL using Visual Studio Code
To use VSCode for Remote development in WSL, you need to install the remote-wsl extension
This extension name is visible in VS Code’s Remote Explorer. It directly shows you existing WSL targets on your computer:Right click on the Ubuntu-18.08 item, or click the small connection icon to the right of the WSL item to connect to the WSL.
This will open a new instance of VSCode without the folder open. If you now open a folder, you can select the project folder directly from within the WSL:If the correct folder is selected, click OK or press Enter. When first connected, it will install VSCode Server in the WSL, which is the actual VSCode instance that does the actual work.
You’re actually working, coding, and debugging in WSL. Your local VSCode instance is a terminal session into the WSL. IntelliSense, code analysis, and all the cool stuff that goes into WSL. This also means that even if you already have VSCode Extensions installed on your computer, you may need to install it again in WSL. Even VSCode terminals are connected to WSL:
Explorer shows you the current project:To see if remote encoding is working, open _layout.cshtml in Pages/Shared/ and open and change the application title to make it more readable.
I changed all wsldemo to WSL Demo:Running debugging starts, the browser opens and displays my changes.
6, summary
It’s really easy, and Microsoft does a lot of things to make remote development as easy as possible. Now, I can also test my applications on Linux or develop for Linux.
In fact, I didn’t realize THAT I could call WSL’s internal running Web directly from a Windows browser. This makes testing and front-end debugging very easy.
In order not to mess up the WSL, I will avoid doing too many different things on it. Installing the.NET 5 runtime is not a big deal, but if I also wanted to test Nginx integration or something else, I would choose Docker Containers. You can also do remote development inside the Docker container, which may be covered in another future article.
Casually say our slogan: pay attention to the building Lord not get lost, a key three even love you!