Installing and using Ruby in Rails natively in Windows is a very difficult and painful process. So we will be taking the help of WSL(Windows Subsystem for Linux) for this process. WSL allows developers to run a Linux environment without the need for a separate virtual machine or dual booting. So before we get on with the installation of Ruby on Rails lets install WSL.
IMPORTANT NOTE: For installing WSL you need to have a minimum Windows version of Windows 10 version 2004 and higher (Build 18362 and higher)
Note: You can skip this step if you have already installed WSL2 on your machine.
Installing WSL
To start, enable WSL feature in windows if not already done. You can do this by opening a new PowerShell window as an administrator.
Tip: To open Powershell as an administrator, search and find PowerShell
on the start menu, right click on the Microsoft PowerShell
app and click on Run as Administrator
.
Now execute the following command:
After this step, PowerShell might ask you to restart your machine. Let's restart out machine and continue with the installation by typing Y
.
Once the system has been restarted, open a new PowerShell window and execute the command:
Note that we will be using WSL2 which is the default version of WSL in Windows machines now. If you have an older version of Windows, you might be defaulted to WSL 1 instead. To avoid this, let's specifically set the default version of WSL to be WSL2.
Once this step is done, you should reboot your system. Most of these features require a system reboot for activation since these are changes made in the system level.
If WSL has already been installed on your system, then you should see a similar message when you execute the above command:
Now you can choose your preferred distro to use along with WSL. To see a list of all available distros you can execute:
This will give you a result similar to this:
You can choose any distro you like from the list. For the rest of the guide we will be using the commands for Ubuntu. If you like to follow along with the guide, we recommend you choose the Ubuntu distro as well.
To install Ubuntu execute the command:
Some of you may face the following error:
In this case, you need to update the WSL kernal to the latest machine. For this find the architecture of your CPU. It can be found by executing the following:
For x64 machines: download and install
this update.
For ARM64 machines: download and install
this update
After installing these updates, try re-installing Ubuntu with the steps mentioned above.
After the installation, let's ensure that the distro was installed with WSL2 by executing this command:
You should get an output similar to this:
Instead, if you see that the version is one, we need to change it to version 2.
IMPORTANT NOTE: You can skip to the next step if the output you received in the line above is 2.
To change the WSL from 1 to 2 for Ubuntu, execute the following command:
If you see an error message like Please enable Virtual Machine Platform Windows feature and ensure virtualization is enabled in the BIOS
when you open the WSL terminal, follow the below steps to fix it:
Step 1: Go to control panel > programs.
Step 2: Under the section Programs and Features click Turn windows features on or off.
Step 3: In the popup window check the box for Virtual Machine Platform and click ok.
Step 4: Restart your system if prompted.
IMPORTANT NOTE: For older systems (Windows 10 build < 19041) these steps won't work. For these versions use the following commands to install WSL. If you were able to successfully installs WSL using the steps mentioned above, you can skip these steps and start with the next section.
Step 1: Open a new PowerShell window in Administrator mode (Start menu > PowerShell > right-click > Run as Administrator)
Step 2: Enable Windows-Subsystem-Linux
Restart the machine if prompted.
Step 3: Enable the Virtual machine feature:
Step 4: Download the latest Linux kernal update package by visiting this link.
Step 5: Download the image for Ubuntu by visiting this link. Note: This link downloads the LTS version of Ubuntu. Installing this package might throw an error for some Windows users. This can be due to any of the following reasons:
- Your system doesn't support the LTS version of Ubuntu. In these cases download the version 20.04 by visiting this link instead.
- Your system has an ARM chip. In that case download the ARM version of Ubuntu by visiting this link.
- If none of these help its possible that WSL cannot be installed on your machine. Learn more by visiting this link.
Step 6: Once download is complete, open PowerShell and navigate to the directory in which the package was downloaded
Step 7: Now install the package:
Step 8: Now add the Linux distribution path to your Windows Environment PATH variables
Replace UserName with your user name.
Step 9: Once done open the Ubuntu app from the start menu
Creating a new Username and Password for UNIX
When you open the Ubuntu terminal for the first time, you will be prompted to create a new UNIX username and password. Add your desired username and password. and continue.
IMPORTANT NOTE: Make sure to note down these credentials since you will have to reinstall WSL completely if you forget them.
Install Windows Terminal (Optional)
The development experience using the Command Prompt shell, PowerShell and the WSL native terminal is subpar. To overcome this limitation, let's install the Windows Terminal.
- Go to the Windows Terminal download page and download the app.
- Install Terminal via Microsoft store.
Note: If you were able to install Windows terminal using the steps mentioned above please skip to the next section.
If your system doesn't have Microsoft store or you were not able to install the terminal using the steps mentioned above, follow these steps:
Step 1: Visit the Windows Terminal GitHub page.
Step 2: Find the latest release and click the msixbundle file to download it
Step 3: Navigate to the folder where the file was downloaded.
Step 4: Install the package
IMPORTANT NOTE: You need atleast Windows 10 2004 (build >= 10.0.19041.0) for installing Windows Terminal.
Install VSCode on your machine (Optional but recommended)
When working with WSL in Windows, we recommend using VSCode as you default text editor. This is because of the officially available Microsoft extensions in VSCode that can help you work with WSL much easier. To install VSCode follow these steps:
Step 1: Go to the VSCode page and download the latest version.
Step 2: Double click on the downloaded EXE file and install VSCode
Step 3: Now open VSCode and click to the extensions tab. There search for WSL and install the WSL extension.
Step 4: Now click on the search bar on top of your VSCode screen and type the greater than (>
) symbol your keyboard to open the command palette.
Step 5: Now search and find the option WSL: Connect to WSL
and click on it.
Step 6: Your VSCode window will be refreshed and you will see a WSL tag on the bottom left corner of your VSCode screen indicating you are connected to WSL.
Now you will be able to access WSL directly from your VSCode.