Its been my experience that getting Ruby and by extension Rails setup on a Windows machine can be difficult and headache inducing. It is also my experience that upon getting it all setup, it runs just a bit slower than on Linux or OSX.
Because of the challenges surrounding Ruby on Windows the best solution often involves either Docker or Virtual Machines. Although there is nothing wrong with those solutions another one has arrived; Ubuntu on Windows 10.
Put simply Microsoft has developed "Windows Subsystem for Linux (WSL)" which Ubuntu is run on. It allows for files on a Windows machine to be interacted with using Linux command-line tools as well as providing a genuine Ubuntu Linux environment within Windows.
Ruby on Rails can now be installed and ran within a Ubuntu environment while accessing application code located on the Windows File System. This means we can use windows applications like Visual Studio Code to modify the application code but have it be served through the Ubuntu environment.
You must be running 64bit Windows 10 with the Anniversary Update or later. And follow the MSDN installation instructions
After the restart you should be able to press the start menu type in "ubuntu" and see the following new program. Clicking on it opens up a bash shell.
It is within Bash where we will be installing Ruby on Rails. Because this isn't a post about how to install Ruby on Rails on Ubuntu I will link to a great guide on how to do just that. Since we are now in a Ubuntu Bash Shell we can treat it as such and follow any Ubuntu resources.
Now that we have enabled Ubuntu on Windows and installing Ruby on Rails in that environment we are ready to generate a new project.
It's now when I would like to point out that using Windows apps and tools to change Linux files is a bad idea and can lead to data loss. Instead we will be using Linux to modify/execute Windows files which is completely fine and within the intended use of WSL.
Within our Bash shell the Windows File System can be found at
/mnt/. Below is a comparison of
/mnt/c and the C drive contents navigated to from the file explorer. Notice that they are identical; we are now navigating into the Windows File System from Linux.
So long as we generate the Rails project within the Windows File System (
/mnt) we are inline with intended use and at no risk of data loss. Not to mention we can then modify those files as we would for any other Windows files.
I have found that the best thing to do first is create a symlink to our Windows development folder. For this post I have created the following directory within windows
E:\documents\ruby_projects which from within the Bash shell is
$ ln -s TARGET LINK_NAME
- change directories into
~/ruby_projectsand see files that are actually located at
$ rails new blogto generate a new Rails app named "blog"
- CD into the new blog directory and do
$ rails serverwhich will start a server listening on port 3000
- Navigate to
localhost:3000within a Windows web browser.
Success! Note how that message is saying Ruby is running within Linux.
Now you can open up your favorite text editor/IDE in Windows and edit the files at
E:\documents\ruby_projects just like normal. The changes will be seen by the Rails server running on Ubuntu and reflected on localhost.