Install asdf (ruby, nodejs and yarn) in WSL2
Tags: wsl2, asdf, ruby, node
Published: 2021-08-30 10:03:10

## What is WSL2
Read [Microsoft WSL2 Documentation](https://docs.microsoft.com/en-us/windows/wsl/install-win10)

## What is asdf
Read [asdf virtual manager Documentation](http://asdf-vm.com/)

## Install WSL2
Video Reference: [WSL 2: Getting started](https://www.youtube.com/watch?v=_fntjriRe48&t=704s) (Watch from Overview until PowerShell Commands (0:00 - 8.23))

## Before installing asdf
- In your Ubuntu, type `code .` to open up Visual Studio Code (VS Code)
- Open your terminal in VS Code
- In your terminal, it should be in already be Linux not Windows (You are in Windows if you have something like `PS C:\Users\>`), go to linux by typing `wsl`

```
$ PS C:\Users\>wsl

# Go to root directory
$ <user_name>: cd /
```

- Do the following to get dependencies for wsl2 (else you might get the error when install ruby: `BUILD FAILED (Ubuntu 20.04 using ruby-build 20210804)`)

For more information related to solving this problem: read [here](https://discuss.rubyonrails.org/t/installing-rails-on-ubuntu-20-04-showing-build-failed-error/75642/4)
```
$ sudo apt-get update
$ sudo apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline-dev zlib1g-dev libncurses-dev libffi-dev libgdbm-dev
```

---

## Install asdf
- Follow the documentation of in [asdf virtual manager Documentation](http://asdf-vm.com/)
```
$ sudo apt install curl git
$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.8.1
```
- Add `. $HOME/.asdf/asdf.sh` and `. $HOME/.asdf/completions/asdf.bash` in the end of `.bashrc` file
- Call `code ~/.bashrc` in your terminal

---

## Close and refresh terminal
- Close your terminal by clicking the rubbish bin icon
- Reopen the terminal

---

## Add plugins
```
$ asdf plugin add ruby
$ asdf plugin add nodejs
$ asdf plugin add yarn
```

---

## Install language
- Install ruby, nodejs and yarn latest version (or you can specify the version needed)
```
$ asdf install ruby latest
$ asdf install nodejs latest
$ asdf install yarn latest
```
- Check the version installed (the versions mentioned here is the latest version I downloaded)
```
$ asdf list
nodejs
  16.8.0
ruby
  3.0.2
yarn
  1.22.11
```
- Add to your shell
```
$ asdf shell ruby 3.0.2
$ asdf shell nodejs 16.8.0
$ asdf shell yarn 1.22.11
```
- Add to global
```
$ asdf global ruby 3.0.2
$ asdf global nodejs 16.8.0
$ asdf global yarn 1.22.11
```

---

## Install gems
- Check where is languages installed
```
$ type -a ruby
ruby is /home/<user>/.asdf/shims/ruby

$ type -a node
node is /home/<user>/.asdf/shims/node

$ type -a yarn
yarn is /home/<user>/.asdf/shims/yarn
yarn is /mnt/c/Program Files (x86)/Yarn/bin/yarn
```
- Check which ruby and gems your directory is referring to (in case you have more than 1 ruby version installed)
```
$ which ruby
/home/<user>/.asdf/shims/ruby

$ which gem
/home/<user>/.asdf/shims/gem
```
- If not in `.asdf/shims`, do `asdf reshim ruby 3.0.2` (specify the version yourself, mine here is 3.0.2)
- Install gems
```
$ gem install rails
```
- The rest can follow [Installing Ruby on Rails Using asdf](https://www.youtube.com/watch?v=Ji2jhj7t0bk&t=317s) (starting 5:24)

---

## References
1. [Microsoft WSL2 Documentation](https://docs.microsoft.com/en-us/windows/wsl/install-win10)
2. [asdf virtual manager Documentation](http://asdf-vm.com/)
3. [WSL 2: Getting started](https://www.youtube.com/watch?v=_fntjriRe48&t=704s)
4. [BUILD FAILED (Ubuntu 20.04 using ruby-build 20210804)](https://discuss.rubyonrails.org/t/installing-rails-on-ubuntu-20-04-showing-build-failed-error/75642/4)
5. [Installing asdf and using it to install Go, Python & Terraform](https://www.youtube.com/watch?v=njM3-eejlho)
6. [Installing Ruby on Rails Using asdf](https://www.youtube.com/watch?v=Ji2jhj7t0bk&t=317s)