CSE 121e: Week 01

Installation and Purpose

The Why of Erlang

Some Background

The Ericsson corporation in northern Europe had a need. Their old telephone control system was going to be replaced with new hardware. They had created the new hardware, but the design and development of the controlling software collapsed. They needed to replace that development with something else, or lose a lot of money. Thankfully they had been financing Joe Armstrong and others to explore "a better way of programming telephony applications" as part of Ericsson's research lab expenditures since the late 1980's.

In the lab, Armstrong and a few others had been creating and experimenting with a new language they called Erlang. Initially conceived as an internal Ericsson language, those responsible, including Armstrong, found that the language worked very well for any system that needed massively concurrent, distributed applications with nearly 100% uptimes. In 1998, the Ericsson corporation open sourced Erlang. Since then, many companies including Google, Amazon, WhatsApp, Digital Ocean, MasterCard, Nintendo, and many others have adopted Erlang as a major part of their design and development core set of tools.

If you want more information on the history of Erlang, Joe Armstrong wrote an article for the ACM outlining how and why Erlang became what it is.

Python and Erlang are Cousins

Take a look at Figure 1. It shows the ancestry of both Erlang and Python. Since Modula is an ancestor of both languages you can expect to see some things in Erlang that remind you of what you learned about coding when you studied Python. An example of this is the concept of a module.

place holder
Figure 1. - The antecedent languages for Python and Erlang (Grady Booch, Joe Armstrong).

Installations

Installing the Editor

The editor selected for this course is Visual Studio Code. If you don't already have it, go to the download site for Visual Studio Code to download the installer.

After you have installed VS Code, install the following extensions:

  1. Everyone: erlang by Pierrick Gourlain. This extension will provide syntax highlighting, color coding, and help docs for built-in Erlang functions (BIFs).
  2. Everyone: Erlang LS by erlang-ls. Provides code completion for function names, variable names, and more.
  3. Windows users only: WSL by Microsoft. This extension will let you connect to your Ubuntu WSL installation from VS Code, which you'll be installing in the next steps.

Please realize that it's OK if you don't understand everything about VS Code and these plugins right off the bat. You'll gradually learn as you run into a need. The reason for adding the Erlang plugins is that they enable color coding for Erlang in VS Code and show help docs for built-in Erlang functions (BIFs) when you move your mouse over a function and type Command (MacOS) or Ctrl (Windows). The WSL extension allows Windows users to connect to the Windows Subsystem for Linux from VS Code.

Installing Erlang

Windows Users

It is possible to do Erlang development directly on Windows. However, to make your life easier, we will install Windows Subsystem for Linux (WSL) and use Ubuntu Linux.

Follow the steps below. If you get stuck, check out the videos at the bottom of the page.

First, check your version of Windows.

Press Win+R and type winver.

In the window that pops up, check your version of Windows.

If your OS is Windows 10 version 2004 and higher or Windows 11, open a Windows Command Prompt or Powershell window and type the following command:

C:\> wsl --install -d Ubuntu

Otherwise, if you are on earlier versions of Windows, follow the instructions on the manual install page.

After WSL installation completes, you may be prompted to reboot your laptop.

After you finish rebooting, open a Command Prompt and type wsl to enter an Ubuntu Linux command line.

C:\> wsl

Notice your command prompt changes to a Linux-style bash prompt, ending in a $-sign. This lets you know you are in an Ubuntu Linux shell.

<username>@<computername>:~$

Type the following command to update the Ubuntu package manager with the latest list of packages from its software repositories:

$ sudo apt update

After this command completes, type the following to install Erlang and the rebar3 Erlang build tool:

$ sudo apt install rebar3

Finally, open VS Code in WSL mode by typing the following command.

$ code .

Note the single period "." at the end of the command. The single period is a symbol used on the command line to mean the "current working directory", i.e., the directory that you are in right now.

You'll know if VS Code has opened in WSL mode by looking at the bottom-left corner of the VS Code window. You should see "WSL: Ubuntu" like in this screenshot:

WSL: Ubuntu

If you don't see "WSL: Ubuntu", make sure you have installed the WSL extension in VS Code, then close VS Code completely and type code . in the terminal again.

After you have opened VS Code in WSL mode, install these two extensions:

  1. erlang by Pierrick Gourlain.
  2. Erlang LS by erlang-ls.

MacOS Users

There are many ways to install an Erlang development environment on your device. The easiest way is to use homebrew. If you don't have homebrew installed, do it now.

After completing the installation of homebrew, enter brew install rebar3 in the terminal.

Linux Users

Open a terminal and type the following command to update the Ubuntu package manager with the latest list of packages from its software repositories:

$ sudo apt update

After this command completes, type the following to install Erlang and the rebar3 Erlang build tool:

$ sudo apt install rebar3

Test Your Tools

Download and unzip this file, which contains a project you can use to test your rebar3 and Erlang installation, VS Code, and the plugins you installed in VS Code. After going to where you downloaded the file, select it and unzip it. (Note for WSL users about where to store your files.)

How to Unzip

Testing VS Code

After the folder is unzipped, run VS Code and open to the folder you have unzipped.

Windows users: To open VS Code to the correct folder, open a command line to the folder you have unzipped, enter a WSL command prompt by typing wsl, then type code . to open VS Code in WSL mode to that folder. (See the video below if you get stuck.)

C:\> wsl

$ code .

Mac and Linux users: Open VS Code and select File->Open Folder from the pulldown menu at the top of your screen. Go to the folder you unzipped, not the zipped file, highlight it, and then click Open.

After you have opened VS Code to the correctly location, you should now see this:

a view of the IDE window

Select "Yes, I trust the authors."

Next, select the Explorer icon in the upper-left of the VS Code window. You will be shown a list of what is in the project. Select math.erl from this list.

You will now see the source code in the math.erl file. The code is colored to make it easier to read. You may see two messages pop up in the lower right corner of the screen. Both of these can be safely closed and ignored. VS Code now looks like this.

a view of the IDE window with the Erlang code colored and ready to edit if needed (Hint: it is not needed)

While this is a complicated VS Code installation process, you only have to do it once to get what you need for this course.

Your editor is now all set up.

Now lets see how to use the rebar3 and Erlang tools to compile and execute code in the project. In this class, we will run all code in a REPL.

Open a terminal in VS Code by selecting Terminal -> New Terminal from the menu bar.

Make sure your terminal is in the intro directory. If not, use the cd command to change directories into the intro directory. Then type rebar3 shell.

If you have everything set up correctly, you should see something like this:

a view of the running REPL

If you don't see this, contact your instructor for help. If you do, enter q(). to exit the REPL.

Note that the green text says Compiling intro and Booted intro. When you typed rebar3 shell, the rebar3 build tool found the source code contained underneath the intro directory and compiled it for you. Erlang is a compiled language, as opposed to Python, which is an interpreted language. This means that if you ever make a change to your source code, you will need to compile it again before running it or the changes you make will not take effect.

The course heavily uses Unit Tests and Test Driven Development (TDD). Your weekly tasks will be to write code that passes a set of pre-written Unit Tests for that week. Let's make sure the unit testing portion of rebar3, which is called EUnit, is set up correctly. To do this, stay in the intro directory in your terminal. Enter rebar3 eunit. You should see something like this:

a view of the terminal after running an example set of seven unit tests. It concludes by printing, seven tests, zero failures.

Make sure you see 7 tests, 0 failures. If you do, you are all set up. If you don't see this message, make sure you are in the intro directory when you run the rebar3 eunit command. If you still don't see this message, contact your instructor for help.

If you want to, you can also run code directly from the REPL. Here is an example.

Type rebar3 shell again. Once it has started and you see evidence of a successful setup, enter math:fib(10). Make sure you include the period at the end. When you do this, the 10th Fibonacci number is calculated amd you should see this.

a view of the terminal after running math colon fib open parentheses 10 close parenthesis period. It concludes by printing fifty five.

If you don't, contact your instructor for help.

Now for the final test. Enter "first:printout()." including the period, in the REPL. It should print out "It worked!!" in the REPL with "ok" on the line after.

If you don't see this, contact your instructor for help.

If you make a change to a source code file, you'll need to re-compile the source code before you try to run it. To compile a module, type c(module_name). in the REPL, like this:

1> c(math).

Note the period at the end of the line.

Workflow

The work you do using these two tools will flow like this. Each time you are asked to do an assignment, you will download a zip file containing an Erlang project. You will unzip it and then use your text editor, VS Code or some other of your choice, to open the project and write code. You will write the code is such a way that a set of Unit Tests passes. After you learn how to write functions, you will write functions as part of your task from scratch. Prior to learning to write functions, you will be given a stubbed-out function and all you will need to do is fill in the needed code.

Write the code to pass one test at a time! Do NOT try to write code that passes all of the tests at once. That will take you longer.

If you follow this pattern for each assignment, you will reduce your workload and increase your learning.

Creating a new project

Most of the work you do for this class will be in existing projects provided by your instructor. If you need to create a project from scratch, you can use rebar3 to setup up your project by typing the following in the terminal.

$ rebar3 new lib <name_of_project>

This command will create a new Erlang project. The source files for your new project will be stored in the src directory. You won't need to do that for this week, but keep it in mind for future projects or if you would like to experiment with Erlang.

Additional Help for Students Using Windows with WSL

These videos are optional walkthroughs of how to install Windows Subsystem for Linux (WSL) and the rebar3 build tool.

Installing Windows Subsystem for Linux (WSL) and rebar3

Opening VS Code in WSL Mode and Testing rebar3 Installation

WSL Filesystem Performance

Note that using WSL and keeping your files on the Windows filesystem can result in poor performance for filesystem intensive operations. That will not be a problem for this class, but if you write code that reads/writes frequently to the filesystem, it is recommended that you keep your files in the Linux filesystem, not the Windows filesystem.

If you would like to keep your files in the Linux filesystem, you should create a directory in your Linux home directory for your Erlang work. You can then access that directory from VS Code in WSL mode. To do this, follow these steps.

  1. Open a WSL command line by typing wsl in a Windows command prompt.
  2. Enter cd ~ to go to your home directory. Note that this will go to your Linux home directory under the Linux filesystem.
  3. Enter mkdir erlang to create a directory named erlang.
  4. Enter cd erlang to go to your erlang directory.
  5. Enter code . to open VS Code in WSL mode with your erlang directory as the current working directory.

C:\> wsl

$ cd ~
$ mkdir erlang
$ cd erlang
$ code .
	

You can now use VS Code to create new projects in your erlang directory.

When you are done, you can exit VS Code and then exit from the WSL command line.

When you want to work on your Erlang projects, open a WSL command line and enter cd ~/erlang. Then enter code . to open VS Code in WSL mode with your erlang directory as the current working directory.


C:\> wsl

$ cd ~/erlang
$ code .