wsl使Windows上的软件开发再次变酷

I felt in love with Ubuntu a long time ago, when I was able to run World of Warcraft on my machine that had 256MB and no graphics card at all, and I’ve been coding there ever since. The bad thing about Linux though is that it’s great for coding but not so good at everything else. I think most of the software development community has moved to Linux or MacOS because setting up development environments there is just easy.

很久以前,当我能够在拥有256MB且完全没有图形卡的机器上运行《魔兽世界》时,我就爱上了Ubuntu,从那时起我一直在这里进行编码。 但是,关于Linux的坏处是,它对编码非常有用,但在其他所有方面却不那么出色。 我认为大多数软件开发社区已经迁移到Linux或MacOS,因为在那里建立开发环境非常容易。

Well, that has changed now that Windows added native support for Linux with WSL. In this guide, I’ll share with you my process to set up a backend development environment on Windows using:

好了,既然Windows在WSL中添加了对Linux的本机支持,那情况就发生了变化。 在本指南中,我将与您分享使用以下方法在Windows上建立后端开发环境的过程:

  • Ubuntu 20.04 LTS

    Ubuntu 20.04 LTS
  • Docker

    码头工人
  • Gradle + Java

    Gradle + Java
  • IntelliJ

    IntelliJ
  • GitKraken

    吉特·克拉肯

It might be a bit long but trust me, this will save you a few google searches.

可能会有点长,但是请相信我,这将为您节省一些Google搜索。

设置WSL (Setting Up WSL)

The first step is to enable Windows Subsystem for Linux (WSL) feature. To do this, you’ll need to open a PowerShell console as administrator (Start -> PowerShell -> Right Click -> Run as Administrator) and execute the following commands:

第一步是启用Windows Linux子系统(WSL)功能。 为此,您需要以管理员身份打开PowerShell控制台(开始-> PowerShell->右键单击->以管理员身份运行)并执行以下命令:

# Enable WSL
dism.exe /onl
ine /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart# Enable Windows Virtual Machine Platform
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

After this, you should restart your machine (it’s Windows, what did you expect). Then execute the last command:

此后,您应该重新启动计算机(这是Windows,是您期望的)。 然后执行最后一条命令:

# Set WSL version 2 as default
wsl --set-default-version 2

Note: Please make sure your version of Windows is higher than 1903 Build 18362. Check here for more details.

注意:请确保您的Windows版本高于1903 Build18362。请在此处查看更多详细信息。

安装Ubuntu (Install Ubuntu)

Once WSL is enabled, it’s time to install the Linux distribution. I used Ubuntu 20.04 because is my favorite and it’ll be the base of this guide but I’d think the steps shouldn’t differ much with other distros.

启用WSL后,就该安装Linux发行版了。 我使用Ubuntu 20.04是因为它是我的最爱,它将成为本指南的基础,但我认为步骤与其他发行版应该没有太大不同。

To install it, just open the Microsoft Store and search for Ubuntu 20.04 LTS. After the installation, you’ll be required to enter a password and voila! you have an actual Linux machine up and running:

要安装它,只需打开Microsoft Store并搜索Ubuntu 20.04 LTS即可。 安装完成后,您将需要输入密码,瞧! 您已经安装并正在运行一台实际Linux机器:

码头工人 (Docker)

When using WSL, you don’t need to install Docker on each distro (actually it won’t work). Instead, what you need to do is install Docker Desktop for Windows. Once installed, go to Settings -> Resources -> WSL Integration and enable it:

使用WSL时,您不需要在每个发行版上都安装Docker(实际上它不起作用)。 相反,您需要做的是安装Docker Desktop for Windows 。 安装后,转到设置->资源-> WSL集成并启用它:

wsl使Windows上的软件开发再次变酷_第1张图片
Docker Desktop WSL Integration Docker桌面WSL集成

After this, you’ll be able to execute the usual docker ps command from Ubuntu.

之后,您将能够从Ubuntu执行通常的docker ps命令。

Java + Gradle (Java + Gradle)

Starting from here, you can follow whatever setup ritual you have for your fresh development machines. In my case, I use SDKMAN which is this great tool for managing versions, kind like an nvm but for Java, Gradle, Maven among others. You can install it following the instructions here.

从这里开始,您可以按照新的开发机器的设置习惯进行操作。 就我而言,我使用SDKMAN ,它是用于管理版本的出色工具,类似于nvm,但适用于Java,Gradle,Maven等。 您可以按照此处的说明进行安装。

Once installed you just need to execute the following commands to install the latest Gradle and Java versions:

安装后,您只需执行以下命令即可安装最新的Gradle和Java版本:

sdk install gradlesdk install java

To install other versions and learn more about how to use SDKMAN to manage your environments, please take a look here.

要安装其他版本并了解有关如何使用SDKMAN来管理您的环境的更多信息,请在此处查看。

IntelliJ (IntelliJ)

Ok, everything is set up from the command line but, I need my IDE as much as my coffee to be productive. Some IDEs like Visual Studio already have native support for WSL so if this is your favorite you’re lucky.

好的,一切都从命令行进行了设置,但是,我需要我的IDE和咖啡一样多的东西来提高生产力。 某些Visual Studio之类的IDE已经对WSL提供了本机支持,因此,如果您最喜欢它,那将会很幸运。

In my case, I’m a JetBrains fanboy so I need my IntelliJ. They still don’t offer native support for WSL but the good news is that the steps here will allow you to run other tools such as GitKraken or Postman so, here is how you do it.

就我而言,我是JetBrains的狂热粉丝,所以我需要IntelliJ。 他们仍然不提供对WSL的本地支持,但是好消息是,这里的步骤将使您可以运行其他工具,例如GitKraken或Postman,因此,这是您的操作方法。

On Windows

在Windows上

  1. Install Chocolatey following these instructions

    按照以下说明安装Chocolatey

  2. choco install VcXsrv

    choco install VcXsrv

Note: It is not absolutely necessary to install Chocolatey, there’re some other guides to install VcXsrv directly but it’s a bit complicated compared to just executing a command.

注意:并非绝对必须安装Chocolatey,还有其他一些指南可以直接安装VcXsrv ,但是与仅执行命令相比,它有点复杂。

Once installed, select these options in the dialog:

安装后,在对话框中选择以下选项:

Display Settings:

显示设置:

  • Check the option “Multiple Windows”

    选中“多个窗口”选项
  • Set Display Number to -1

    显示编号设置为 -1

Client Startup -> Select “Start no client”

客户端启动->选择“不启动客户端”

Extra Settings -> Activate all (including “Disable access control”)

额外设置->全部激活(包括“禁用访问控制”)

Finish Configuration -> Save the configuration as a desktop icon so you don’t have to do this every time.

完成配置->将配置另存为桌面图标,因此您不必每次都这样做。

On Ubuntu

在Ubuntu上

Add these lines to your .bashrc file:

将这些行添加到您的.bashrc文件中:

# Get the WSL host IP
export WSL_ip_line=$(ipconfig.exe | grep "WSL" -n | awk -F ":" '{print $1+4}')# Export it as "DISPLAY" which is used to connect to
export DISPLAY=$(ipconfig.exe | awk -v a=$WSL_ip_line '{if (NR==a) print $NF":0.0"}' | tr -d "\r")
export LIBGL_ALWAYS_INDIRECT=1

Install the following dependencies:

安装以下依赖项:

sudo apt install libcups2 libpangocairo-1.0-0 libatk-adaptor libxss1 libnss3 libxcb-keysyms1 x11-apps libgbm1

Execute the JetBrains Toolbox

执行JetBrains工具箱

wget https://download.jetbrains.com/toolbox/jetbrains-toolbox-1.18.7455.tar.gztar -zxf jetbrains-toolbox-*.tar.gz

Go inside the extracted directory and execute:

进入提取的目录并执行:

./jetbrains-toolbox

Before installing IntelliJ, go to settings and enable the “Shell Scripts” option. This will generate scripts to easily execute the installed tools in the folder you specify.

在安装IntelliJ之前,请转到设置并启用“ Shell脚本”选项。 这将生成脚本,以轻松执行您指定文件夹中的已安装工具。

Once IntelliJ is installed, just open it either from the toolbox or from the generated script, in my case:

一旦安装了IntelliJ,就可以从工具箱或生成的脚本中打开它,在我的情况下:

~/Programs/jetbrains/idea &

And there you go, now you have IntelliJ running over Ubuntu but looking like any other Windows program:

到此为止,现在您已经在Ubuntu上运行了IntelliJ,但看起来像其他Windows程序一样:

wsl使Windows上的软件开发再次变酷_第2张图片
IntelliJ Running On Windows Over WSL 通过WSL在Windows上运行的IntelliJ

Pretty cool huh?

太酷了吧?

吉特·克拉肯 (GitKraken)

Last but not least, I use GitKraken as my Git client because I consider it to be easy to use, fast and it has a lot of nice features and integrations. Anyway, if you happen to use it to, this is what you need to do from Ubuntu shell:

最后但并非最不重要的一点是,我将GitKraken用作我的Git客户端,因为我认为它易于使用,快速且具有许多不错的功能和集成。 无论如何,如果您碰巧使用了它,这就是在Ubuntu shell中需要执行的操作:

Install The Dependencies:

安装依赖项:

sudo apt install chromium-browser
sudo apt install fonts-noto-color-emoji
sudo apt install fonts-noto

Install GitKraken

安装GitKraken

wget https://release.gitkraken.com/linux/gitkraken-amd64.deb
sudo dpkg -i ./gitkraken-amd64.deb
sudo apt-get install -f

If you have a high resolution screen, add this to your .bashrc file in order to get a proper font size:

如果您有高分辨率的屏幕,请将其添加到您的.bashrc文件中,以获得适当的字体大小:

export GDK_SCALE=2

After all this, you can just start it by executing

完成所有这些之后,您只需执行以下命令即可启动它

gitkraken &

And that’s it. It took me a while to get my development environment completed but I think it was worth it. Now I don’t have to reboot to play Mortal Shell =).

就是这样。 我花了一些时间才能完成开发环境,但我认为这是值得的。 现在,我不必重新启动即可玩Mortal Shell =)。

I tried using Win-Kex with Kali Linux so you might wanna take a look, however, it looks like it’s still under development, and in my experience, it didn’t work properly. Every morning I started my days praying to the WSL gods to let it work but those prayers were not answered most of the time. That’s why I decided to go with the approach I share here.

我尝试将Win-Kex与Kali Linux一起使用,因此您可能想看看,但是它似乎仍在开发中,以我的经验,它不能正常工作。 每天早晨,我开始向WSL众神祈祷,让它运转,但大部分时间都没有答复。 这就是为什么我决定采用我在这里分享的方法的原因。

I hope this can help you and if I missed something or you have any suggestions, please don’t hesitate on commenting. Thank you for reading.

希望对您有所帮助,如果我错过了什么或您有任何建议,请随时评论。 感谢您的阅读。

资料来源 (Sources)

  • https://medium.com/@ragin/development-under-windows-under-linux-with-wsl2-intellij-860daf601b61

    https://medium.com/@ragin/development-under-windows-under-linux-with-wsl2-intellij-860daf601b61

  • https://medium.com/@chuckdries/installing-gitkraken-in-wsl-2-15bf6459f823

    https://medium.com/@chuckdries/installing-gitkraken-in-wsl-2-15bf6459f823

  • https://docs.docker.com/docker-for-windows/wsl/

    https://docs.docker.com/docker-for-windows/wsl/

  • https://docs.microsoft.com/en-us/windows/wsl/install-win10

    https://docs.microsoft.com/zh-cn/windows/wsl/install-win10

翻译自: https://medium.com/@jhoanmanuelms/wsl-making-software-development-onwindows-cool-again-d3de5ffdfdd3

你可能感兴趣的:(python,windows,java,linux,leetcode)