How to use WSL with Ruby, Ruby on Rails, MySQL And PostgreSQL Databases ,GIT .

Billo Diallo
9 min readMay 25, 2021

AS Microverse Students after ruby module, The next module is ruby on rails Microverse curriculum Version 1.0, a few days later I started ruby on rails it’s was very complex to run a complex rails app, and use A specific database such as PostgreSQL. After cloning the Social media app I spent at least 3 days fixing issues relating to the database, incompatibility with some gems with windows.

TO solve the issue and work comfortably on windows I decided to install WSL ON Windows 10.

Windows Subsystem for Linux is a compatibility layer for running Linux binary executable natively on Windows 10 and Windows Server 2019. In May 2019, WSL 2 was announced, introducing important changes such as a real Linux kernel, through a subset of Hyper-V features.

The Windows Subsystem for Linux lets developers run a GNU/Linux environment — including most command-line tools, utilities, and applications — directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual boot setup.

For those who are using only Linux System you jump to SECTION 2 .

Section 1

You can:

Choose your favorite GNU/Linux distributions from the Microsoft Store.

Run common command-line tools such as grep, sed, awk, or other ELF-64 binaries.

Run Bash shell scripts and GNU/Linux command-line applications including:

Tools: vim, emacs, tmux

Languages: NodeJS, Javascript, Python, Ruby, C/C++, C# & F#, Rust, Go, etc.

Services: SSHD, MySQL, Apache, Lighttpd, MongoDB, PostgreSQL.

Install additional software using your own GNU/Linux distribution package manager.

Invoke Windows applications using a Unix-like command-line shell.

Invoke GNU/Linux applications on Windows.

This is a short video from Microsoft on WSL https://youtu.be/48k317kOxqg .

IN this article we are going to :

- install Linux sub system step by step and create

- Create user account and password

- Set up Databases: MYSQL and PostgreSQL.

- Install Ruby

- Ruby on Rails

- Configuring GIT.

- Create rails new app

1. System requirements:

Before installing WSL, make sure your computer meets the minimum system requirements to run WSL:

- You must be running Windows 10 version 1607 (the Anniversary update) or above.

- WSL only runs on 64-bit versions of Windows 10. 32-bit versions are not supported.

To check that the computer meet the requirements, follow these steps:

1. Open your Settings. You can do this by clicking the gear icon on the Start menu, or by opening the Power User Tasks menu and choosing Settings.

2. In the Settings window, choose System.

3.On the left side of the System window, choose About.

4. On the right side of the window, system information is displayed. Make sure the Version is at least 1607, and the System type is a 64-bit operating system.

  • Let’s check the settings

Click on system and About.

If the Version number is less than 1607, you need to perform a Windows Update before installing WSL.

If the System type is not a 64-bit operating system, you cannot run WSL.

Enable Windows Subsystem for Linux 1

Command line

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

virtualization:

Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform.

If it doesn’t work you can do manually:

Manually:

- Open Start on Windows 10.

  • Search for Turn Windows features on or off and click the top result to open the experience.

And then select these options below:

- Windows PowerShell 2.0

  • Window Subsystem for Linux ( WSL)
- Windows PowerShell 2.0
  • Window Subsystem for Linux ( WSL)

OS build 20262 or higher

2 . Installing WSL

1. Open a new PowerShell window as Administrator. To do this, open your Start menu, scroll down to W, and expand the Windows PowerShell folder. Right-click Windows PowerShell, choose More, then Run as administrator.

2. At the PowerShell prompt, run the following command:

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

4. Some necessary software downloads, and the WSL subsystem will be enabled after you reboot. Linux itself is not installed yet, however (until you choose a Linux distribution, in step 5.)

5. When the download is complete, PowerShell ask if you’re ready to reboot the computer. Before rebooting, make sure any documents are saved, and any open applications are closed. Type Y, or if you’re going to reboot later, type N instead.

Second method to install WSL

Open the Microsoft Store and select your favorite Linux distribution.

As of this writing, choices are Ubuntu, OpenSUSE, SUSE Enterprise Server, Debian, and Kali. If you’re not sure what to pick, I recommend Ubuntu.

Create an Account and Password:

After installation you will have to configure a Linux user account and password every time you add a distribution, reinstall, or reset.

- Enter your username and password.

After the creation of a user account and password, now you are ready to go, you can update the Ubuntu system.

Set your version to wsl 2 run command below on windows PowerShell :

wsl — set-default-version 2

One of the requirements to switich the version is to update your windows system and Linux kernel.

Follow this documentation below.

https://winaero.com/how-to-install-linux-kernel-update-for-wsl-2-in-windows-10/

wsl — set-version Ubuntu-20.04 2

To check your WSL version

SECTION II

II . Ruby Installation

Step 1

Run this command below :

sudo apt install gcc make libssl-dev libreadline-dev zlib1g-dev libsqlite3-dev

sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev

Step 2: Install rbenv

git clone https://github.com/rbenv/rbenv.git ~/.rbenv

Step 2.1.1

Be sure to copy and paste these command :

echo ‘export PATH=”$HOME/.rbenv/bin:$PATH”’ >> ~/.bashrc

echo ‘eval “$(rbenv init -)”’ >> ~/.bashrc

exit

Next, you need to install ruby-build to help compile the Ruby binaries. Run these commands in the terminal to create a directory for the ruby-build plugin and then download it to the proper directory.

mkdir -p “$(rbenv root)”/plugins

git clone https://github.com/rbenv/ruby-build.git “$(rbenv root)”/plugins/ruby-build

run the command   rbenv - v

to check rbenv version .

we installed rbenv successfully and checked the version , let’s install now .

Step 2.2: Install Ruby

If you’re using rbenv, you’ll need to run the following command to make the rails executable available:

You can use any ruby version you want, in this case I decided to install Ruby version 3.0.0

rbenv install 3.0.0 — — verbose

rbenv global 3.0.0

rbenv rehash

It will take some few minutes , be patient .you can select any ruby version you want .

Step 3: Install Rails

We’ve previously installed Ruby, and now it’s time to install Ruby on Rails. It’s as simple as running one command!

gem install rails

Once the installation finishes, you can check if everything went well by restarting your terminal and running the following command:

Now you are good to go your Ubuntu terminal is ready , you can create rails new app.

rbenv rehash

Once the installation finishes, you can check if everything went well by restarting your terminal and running the following command:

rails –v

Setting Up MySQL

Rails ships with sqlite3 as the default database. Chances are you won’t want to use it because it’s stored as a simple file on disk. You’ll probably want something more robust like MySQL or PostgreSQL.

sudo apt-get install mysql-server mysql-client libmysqlclient-dev

Setting Up PostgreSQL

The easiest way to setup PostgreSQL is to install it on Windows using one of the Windows installers. You’ll be able to run it in Windows and connect to it through Linux.

Pay attention to the username and password you setup during installation of Postgres as you will use this to configure your Rails applications later to login to Postgres when your Rails app runs.

You’ll also need to install the development headers on Ubuntu so you can install the pg gem.

sudo apt install libpq-dev

sudo apt install postgresql postgresql-contrib

Then run these command below:

-bundle update

  • bundle install

Now it’s time to start Postgres Server

Run the command :

sudo service postgresql start

to stop the service :

Run sudo service postgresql stop

rails db:setup

rails db:setup

You will receive a message error like this: the username doesn’t exist that’s you have to create an account in postgres server and link it your app, let’s do that .

- connect to postgres, command: sudo -u postgres psql

- Create a User with admin Role:

ALTER USER billo CREATEDB; in my case am using this username billo , the one I created after ubunter terminal installation , the first account .

sudo -u postgres psql

CREATE USER billo;

ALTER USER billo CREATEDB;

To quit postgres terminal run: \ q

To quit postgres terminal run: \ q

And run the magic command rails db:setup and you are good to go

Well done, you installed postgres, pg gem and set up your local database on Ubuntu.

And start your service
  • rails server
  • Or rails s

Final Steps

cd /mnt/c

cd /mnt/c

If you want to use Postgres

# Note that this will expect a postgres user with the same username

# as your app, you may need to edit config/database.yml to match the

# user you created earlier

#### or if you want to use SQLite (not recommended)# rails new myapp

Install Rails

gem install rails

rails -v

Step 1.2: Install Yarn

With Rails 6 came Webpacker, a Ruby Gem that integrates Webpack, the Javascript module bundler, with Rails.

Run the command : sudo apt-get update -y

Since Rails ships with so many dependencies these days, we’re going to need to install a Javascript runtime like NodeJS and a package manager called Yarn.

To install NodeJS and Yarn, we’re going to add it using the official repository:

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

echo “deb https://dl.yarnpkg.com/debian/ stable main” | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt update

sudo apt-get install -y nodejs yarn

sudo apt-get update && sudo apt-get install yarn

1. curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

2. echo “deb https://dl.yarnpkg.com/debian/ stable main” | sudo tee /etc/apt/sources.list.d/yarn.list

3. sudo apt update && sudo apt install yarn

4. rails webpacker:install

gem install rails -v 6.1.3.2

NB: please don’t forget to run this command below after rails installation .

rbenv rehash

Configuring Git

git config — global color.ui true

git config — global user.name “YOUR NAME”

git config — global user.email “YOUR@EMAIL.com”

ssh-keygen -t rsa -b 4096 -C YOUR@EMAIL.com

Thank you for your time I hope you enjoyed it , feel free to contact me at any time to discuss about the article .

Bibliotheque:

https://docs.microsoft.com/en-us/windows/wsl/user-support

--

--

Billo Diallo

Full-Stack Web Developer. JavaScript | Ruby on Rails | React | Redux. Improving open-source projects, one commit at a time.