Erlang is a general-purpose programming language or runtime environment designed at the Ericsson Computer Science Laboratory. In the Erlang runtime environment the code is compiled to bytecode and runs inside a virtual machine. So Erlang much like Java can run anywhere. It was originally designed for soft real-time applications where you need to handle connections or routing for a very high number of users. It was mainly developed for the large telecommunication systems but now it is also used in several other fields like banking, instant messaging and computer telephony as well.
There are some applications and systems where Erlang has a significant role:
RabbitMQ, message-oriented middleware is written in the Erlang programming language. WhatsApp uses Erlang to run messaging servers. Many large companies like Amazon, Yahoo!, Facebook, Motorola and Github are using Erlang.
Erlang is defined as a functional programming language. Erlang uses the actor model, and each actor is a separate process in the virtual machine. In Erlang runtime environment multiple tasks can be run at the same time. Erlang is used widely because of it’s ability to recover from errors. It has integral support for distribution and concurrency.
Several other key features that Erlang has:
- Hot code replacement, to upgrade the code you don’t need to stop and restart your system.
- Reliability-oriented standard library.
- Lightweight user-space threads.
- It is free and can be used for any open-source, freeware or commercial projects.
Some open-source projects based on Erlang are ejabberd, Flussonic, Riak, RabbitMQ, and Dynomite. - Cross-platform, like Java it can run on different platforms.
- Fault-tolerant and scalable.
This tutorial will help you to install Erlang on Ubuntu 16.04 (Xenial Xerus) operating system. There is an example; a simple Hello World program is explained in this tutorial that will help you to understand how to create, compile and run a program in Erlang programming language.
Requirements
- A server running Ubuntu 16.04.
- In this tutorial, we’ll use a non root account with sudo privileges configure on our server hence we’ll use sudo before the commands.
Updating the Base System
This tutorial assumes that we have a clean Ubuntu Server 16.04. Before installing Erlang, let’s update our base system to the latest update:
$ sudo apt-get update
$ sudo apt-get -y upgrade
Adding Repository entry
You can simply download Erlang repository package from its official website and install on your system.
To add Erlang Solutions repository (including Erlang Solutions public key for apt-secure) to your system, call the following commands:
$ wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
$ sudo dpkg -i erlang-solutions_1.0_all.deb
Install Erlang on Ubuntu
To retrieve the updated package lists and refresh the repository cache run following command:
$ sudo apt-get update
Install either the erlang package or the esl-erlang package. You can install erlang package using the following command:
$ sudo apt-get install erlang
It will ask you if you want to continue with the installation, press y and then enter.
Alternatively, you can do the complete Erlang installation. It includes the Erlang/OTP platform and all of its applications by running the following command:
$ sudo apt-get install esl-erlang
It will ask you if you want to continue with the installation, press y and then enter.
Make sure that you install only one of the above-mentioned packages.
erlang package
The Erlang/OTP platform is a complex system composed of many smaller applications (modules). If you want to download and install a particular selection of modules, then you don’t need to install complete OTP ( Open Telecom Platform) Suit, because it is possible to install these smaller packages of OTP without launching the erlang package.
The OTP libraries provide a complete set of easy-to-use components for building robust distributed applications.
esl-erlang package
The esl-erlang package is a file containing the complete installation: it includes the Erlang/OTP platform and all of its applications.
Erlang Hello World Program
Erlang is easy to learn. An experienced programmer can start writing useful code after a couple of days of learning Erlang. The syntax may look a little different if you’re coming from other programming languages like Java, Ruby, or Python, but it doesn’t take long to get used to.
So let’s take an example and start with a simple “hello world!” program on Erlang.
Create
First, create a file helloworld.erl using vi text editor.
To do this run the following command:
$ vi helloworld.erl
Now in the vi text editor go to the Insert mode by pressing letter ‘i’.
add the following content:
% hello world program
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite("Hello World!\n").
Don’t forget the full-stop (“period” in American English) at the end of each command, as shown in the example.
The ModuleName needs to be same as the file name (do not include the extension .erl), as shown in the example.
Syntax-module(ModuleName).
Example:-module(helloworld).
Switch to the Command mode by pressing ‘Esc’ key.
Now type “:wq” and press enter to save the changes and quit vi editor.
The following explanation will help you to understand the syntax of the above program:
- To add the comments in the program use % sign.
The module statement is like adding a namespace as in any programming language. In this program, we are mentioning that this code is a part of a module called helloworld.
We are using the export function to use any function defined within the program. In the above program we are defining a function called start, and to use the start function, we have to use the export statement.
The slash “/” symbol is used along with the function to define the number of parameters which is accepted by the function. In the above example , the /0 means that our function ‘start’ accepts 0 parameters.
io is an another module which has all the required Input Output functions in Erlang. We are using the fwrite function to output “Hello World” to the console.
Compile
Compile the hello world program using below command.
$ erlc helloworld.erl
The above command will create a file helloworld.beam in the current directory.
Run
Now, run your program using below command.
$ erl -noshell -s helloworld start -s init stop
Output
You will get the following output of your program on your terminal:
ubuntu@ip-172-31-28-82:~$ erl -noshell -s helloworld start -s init stop
Hello World!
Installing HiPE
This is an optional part. HiPE stands for High-Performance Erlang Project. It is a native code compiler for Erlang. In most cases, it positively affects performance. If you want to download it, call the following command:
$ sudo apt-get install erlang-base-hipe
This will replace the Erlang/OTP runtime with a HiPE supported version. Other Erlang applications do not need to be reinstalled.
If you want to come back to the standard runtime, call the following command:
$ sudo apt-get install erlang-base
Conclusion
In this tutorial, we have learned how to install Erlang on Ubuntu 16.04. We have also seen an example to understand how to create, compile and run a program in Erlang.