Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒

I've been trying on and off to enjoy Ruby on Rails development on Windows for many years. I was doing Ruby on Windows as long as 13 years ago. There's been many valiant efforts to make Rails on Windows a good experience. However, given that Windows 10 can run Linux with WSL (Windows Subsystem for Linux) and now Windows runs Linux at near-native speeds with an actual shipping Linux Kernel using WSL2, Ruby on Rails folks using Windows should do their work in WSL2.

我一直在试图和关闭,以享受Ruby在Windows上Rails开发了很多年。 早在13年前,我就在Windows上使用Ruby。 为了使Windows上的Rails成为良好的体验,已经进行了许多英勇的努力。 但是,鉴于Windows 10可以与WSL(Linux的Windows子系统)一起运行Linux,并且现在Windows以接近自然的速度运行Linux,并且实际使用的是使用WSL2的实际Linux内核,因此使用Windows的Ruby on Rails人员应该在WSL2中完成工作。

在Windows上运行Ruby on Rails (Running Ruby on Rails on Windows )

获取最新的Windows 10 (Get a recent Windows 10)

WSL2 will be released later this year but for now you can easily get it by signing up for Windows Insiders Fast and making sure your version of Windows is 18945 or greater. Just run "winver" to see your build number. Run Windows Update and get the latest.

WSL2将于今年晚些时候发布,但现在您可以通过注册Windows Insiders Fast并确保您的Windows版本为18945或更高版本来轻松获得它。 只需运行“ winver”即可查看您的内部版本号。 运行Windows Update并获取最新版本。

启用WSL2 (Enable WSL2)

You'll want the newest Windows Subsystem for Linux. From a PowerShell admin prompt run this:

您将需要适用于Linux的最新Windows子系统。 从PowerShell管理员提示符运行以下命令:

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

and head over to the Windows Store and search for "Linux" or get Ubuntu 18.04 LTS directly. Download it, run it, make your sudo user.

然后转到Windows应用商店并搜索“ Linux”,或者直接获取Ubuntu 18.04 LTS 。 下载它,运行它,使其成为sudo用户。

Make sure your distro is running at max speed with WSL2. That earlier PowerShell prompt run wsl --list -v to see your distros and their WSL versions.

确保您的发行版使用WSL2以最高速度运行。 先前的PowerShell提示符运行wsl --list -v以查看您的发行版及其WSL版本。

C:\Users\Scott\Desktop> wsl --list -v
NAME STATE VERSION
* Ubuntu-18.04 Running 2
Ubuntu Stopped 1
WLinux Stopped 1

You can upgrade any WSL1 distro like this, and once it's done, it's done.

您可以像这样升级任何WSL1发行版,完成后就可以完成。

wsl --set-version "Ubuntu-18.04" 2

And certainly feel free to get cool fonts and styles and make yourself a nice shiny Linux experience...maybe with the Windows Terminal.

当然,您也可以随时获得凉爽的字体和样式,并让自己拥有出色的闪亮Linux体验……也许可以使用Windows Terminal。

获取Windows终端 (Get the Windows Terminal)

Bonus points, get the new open source Windows Terminal for a better experience at the command line. Install it AFTER you've set up Ubuntu or a Linux and it'll auto-populate its menu for you. Otherwise, edit your profiles.json and make a profile with a commandLine like this:

奖励积分,获得新的开源Windows Terminal可以在命令行上获得更好的体验。 在设置Ubuntu或Linux之后安装它,它将自动为您填充菜单。 否则,编辑您的profiles.json并使用如下命令行创建一个配置文件:

"commandline" : "wsl.exe -d Ubuntu-18.04"

See how I'm calling wsl -d (for distro) with the short name of the distro?

看看我如何使用发行版的简称来调用wsl -d(发行版)?

Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒_第1张图片

Since I have a real Ubuntu environment on Windows I can just follow these instructions to set up Rails!

因为我在Windows上有一个真正的Ubuntu环境,所以我可以按照以下说明来设置Rails!

设置Ruby on Rails (Set up Ruby on Rails)

Ubuntu instructions work because it is Ubuntu! https://gorails.com/setup/ubuntu/18.04

Ubuntu指令之所以有效,是因为它是Ubuntu! https://gorails.com/setup/ubuntu/18.04

Additionally, I can install as as many Linuxes as I want, even a Dev vs. Prod environment if I like. WSL2 is much lighter weight than a full Virtual Machine.

此外,我可以根据需要安装尽可能多Linux,如果愿意,甚至可以安装Dev vs. Prod环境。 WSL2的重量比完整的虚拟机轻得多。

Once Rails is set up, I'll try making a new hello world:

一旦设置了Rails,我将尝试创建一个新的世界:

rails new myapp

and here's the result!

结果就是这里!

Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒_第2张图片

I can also run "explorer.exe ." and launch Windows Explorer and see and manage my Linux files. That's allowed now in WSL2 because it's running a Plan9 server for file access.

我也可以运行“ explorer.exe”。 并启动Windows资源管理器并查看和管理我Linux文件。 WSL2现在允许这样做,因为它正在运行用于文件访问的Plan9服务器。

Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒_第3张图片

安装VS Code和VS Code远程扩展包 (Install VS Code and the VS Code Remote Extension Pack)

I'm going to install the VSCode Remote Extension pack so I can develop from Windows on remote machines OR in WSL or  Container directly. I can click the lower level corner of VS Code or check the Command Palette for this list of menu items. Here I can "Reopen Folder in WSL" and pick the distro I want to use.

我将安装VSCode Remote Extension包,以便可以从Windows在远程计算机上进行开发,或者直接在WSL或Container中进行开发。 我可以单击VS Code的下角,或在Command Palette中查看此菜单项列表。 在这里,我可以“在WSL中重新打开文件夹”并选择我要使用的发行版。

Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒_第4张图片

Now that I've opened the folder for development WSL look closely at the lower left corner. You can see I'm in a WSL development mode AND Visual Studio Code is recommending I install a Ruby VS Code extension...inside WSL! I don't even have Ruby and Rails on Windows. I'm going to have the Ruby language servers and VS Code headless parts live in WSL - in Linux - where they'll be the most useful.

现在,我已经打开了用于开发WSL的文件夹,在左下角仔细查看。 您可以看到我处于WSL开发模式,并且Visual Studio Code建议我在WSL内安装Ruby VS Code扩展! 我什至在Windows上都没有Ruby和Rails。 我将在WSL(在Linux中)中使用Ruby语言服务器和VS Code无头部件,它们将是最有用的。

Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒_第5张图片

This synergy, this balance between Windows (which I enjoy) and Linux (whose command line I enjoy) has turned out to be super productive. I'm able to do all the work I want - Go, Rust, Python, .NET, Ruby - and move smoothly between environments. There's not a clear separation like there is with the "run it in a VM" solution. I can access my Windows files from /mnt/c from within Linux, and I can always get to my Linux files at \\wsl$ from within Windows.

这种协同作用以及Windows(我喜欢的)和Linux(我喜欢的命令行)之间的平衡已证明是非常有效的。 我能够完成我想要的所有工作-Go,Rust,Python,.NET,Ruby-并在环境之间平稳地移动。 与“在VM中运行”解决方案没有明显的区别。 我可以在Linux中从/ mnt / c访问Windows文件,并且始终可以从Windows中以\\ wsl $进入我Linux文件。

Note that I'm running rails server -b=0.0.0.0 to bind on all available IPs, and this makes Rails available to "localhost" so I can hit the Rails site from Windows! It's my machine, so it's my localhost (the networking complexities are handled by WSL2).

请注意,我正在运行Rails服务器-b = 0.0.0.0来绑定所有可用IP,这使Rails可用于“ localhost”,因此我可以从Windows访问Rails站点! 这是我的机器,所以是我的本地主机(网络复杂性由WSL2处理)。

$ rails server -b=0.0.0.0
=> Booting Puma
=> Rails 6.0.0.rc2 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.2-p47), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Here it is in new Edge (chromium). So this is Ruby on Rails running in WSL, as browsed to from Windows, using the new Edge with Chromium at its heart. Cats and dogs, living together, mass hysteria.

它在新的Edge(Chrome)中。 因此,这是运行在WSL中的Ruby on Rails,从Windows浏览到Windows,并使用了以Chromium为核心的新Edge。 猫和狗在一起生活,歇斯底里。

Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒_第6张图片

Even better, I can install the ruby-debug-ide gem inside WSL and now I'm doing interactive debugging from VS Code, but again, note that the "work" is happening inside WSL.

更好的是,我可以在WSL内安装ruby-debug-ide gem,现在我正在从VS Code进行交互式调试,但是再次注意,“工作”正在WSL内进行。

Windows上的Ruby on Rails不仅可行,而且使用WSL2和VS Code很棒_第7张图片

Enjoy!

请享用!

Sponsor: Get the latest JetBrains Rider with WinForms designer, Edit & Continue, and an IL (Intermediate Language) viewer. Preliminary C# 8.0 support, rename refactoring for F#-defined symbols across your entire solution, and Custom Themes are all included.

赞助商:使用WinForms设计器,“编辑并继续”和IL(中间语言)查看器,获取最新的JetBrains Rider 。 初步的C#8.0支持,整个解决方案中F#定义符号的重命名重构以及自定义主题都包括在内。

翻译自: https://www.hanselman.com/blog/ruby-on-rails-on-windows-is-not-just-possible-its-fabulous-using-wsl2-and-vs-code

你可能感兴趣的:(网络,内核,linux,java,python)