Install Erlang on Ubuntu 22.04: Reliable Programming
This tutorial will guide you through the process of installing Erlang on Ubuntu 22.04. Erlang is a functional programming language and runtime environment renowned for its concurrency, fault tolerance, and distributed capabilities. If you’re wondering, "Why should you use Erlang in your project?", consider its impressive features, including:
- Concurrency: Erlang excels at handling numerous concurrent processes, making it ideal for real-time systems.
- Fault Tolerance: Built-in mechanisms allow Erlang applications to recover from failures gracefully.
- Distribution: Erlang facilitates the development of distributed systems that can span multiple machines.
- Hot Code Swapping: Erlang allows you to update code without stopping the entire system, minimizing downtime.
- Functional Programming: Erlang’s functional nature promotes code clarity and maintainability.
In this guide, you will learn to install the latest version of Erlang by importing its repository into your Ubuntu 22.04 system.
To successfully complete the Erlang setup on your Ubuntu 22.04 system, ensure you are logged in as a non-root user with sudo privileges. You can consult a guide on Initial Server Setup with Ubuntu 22.04 for assistance with this.
Now, let’s proceed with the following steps to set up the Erlang Programming Language on Ubuntu 22.04.
Step 1 – Add Erlang GPG Key on Ubuntu 22.04
To install the Erlang LTS (Long Term Support) version, you must first import the GPG key and its repository. Start by updating your system using the following command:
sudo apt update
Next, install the necessary packages using the command below:
sudo apt install software-properties-common apt-transport-https lsb-release
Now, use the following curl command to import the Erlang GPG key:
curl -fsSL https://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/erlang.gpg
Step 2 – Erlang Installation on Ubuntu 22.04
At this point, update your system again and proceed to install Erlang using the following command:
sudo apt update
sudo apt install erlang
Step 3 – How To Access Erlang Shell?
To access your Erlang shell on Ubuntu 22.04, simply run the following command:
erl
Upon execution, you should observe output similar to this:
Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit]
Eshell V12.2.1 (abort with ^G)
1>
Step 4 – Test Erlang Programming Language
Now, let’s create a test program to verify that your Erlang installation is working correctly.
Create a sample "Hello World" file using the following command:
vi hello.erl
Add the following Erlang code to the file:
% This is a test Hello World Erlang Code
-module(hello).
-import(io,[fwrite/1]).
-export([helloworld/0]).
helloworld() ->
fwrite("Hello, Erlang World!n").
Save and close the file when you’re finished.
Then, log in to your Erlang shell using the command below:
erl
From your Erlang shell, compile your program using the following command:
c(hello).
Next, run your program with the following command:
hello:helloworld().
You should observe output similar to this:
Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit]
Eshell V12.2.1 (abort with ^G)
1> c(hello).
{ok,hello}
2> hello:helloworld().
Hello, Erlang World!
ok
3>
As you can see, your Erlang installation is functioning correctly on your server. You have successfully completed the Erlang installation on Ubuntu 22.04.
Conclusion
At this point, you have learned to install Erlang LTS on Ubuntu 22.04, access the Erlang shell, and test whether your application is working correctly. Erlang offers a wealth of features that you can leverage to create powerful programs. The installation of Erlang opens doors to reliable programming.
Alternative Installation Methods for Erlang on Ubuntu 22.04
While the above method of using the Erlang Solutions repository is a standard approach, there are alternative methods to install Erlang on Ubuntu 22.04. These methods might be preferred depending on your specific needs and environment. Let’s explore two different ways:
1. Using Snap Package Manager
Snap is a package management system developed by Canonical, the company behind Ubuntu. It allows you to install applications in isolated environments, making them more secure and easier to manage. You can install Erlang using Snap with a single command.
Explanation:
Snap packages are self-contained and include all the necessary dependencies. This eliminates the need to manually manage dependencies, making the installation process simpler. Snaps are also automatically updated, ensuring that you always have the latest version of Erlang.
Code Example:
sudo snap install erlang
After the installation, you can access the Erlang shell using the same erl
command. Snap provides a sandboxed environment, which can be both an advantage and disadvantage. It enhances security but might require extra configuration for accessing system resources.
To verify the installation, run:
erl
This should open the Erlang shell.
To ensure the snap is up to date, run:
sudo snap refresh erlang
2. Building From Source
For advanced users who need fine-grained control over the installation process or want to use a specific Erlang version not available through package managers, building from source is an option.
Explanation:
Building from source allows you to customize the build options and optimize Erlang for your specific hardware. This method requires more technical expertise but offers the greatest flexibility. It can also provide a more recent version of Erlang than the package repositories, allowing access to new features and improvements.
Code Example:
First, you need to download the Erlang source code from the official Erlang website or GitHub. Then, you need to install the required dependencies:
sudo apt update
sudo apt install build-essential autoconf m4 libncurses5-dev libwxgtk3.0-gtk3-dev libgl1-mesa-dev libglu1-mesa-dev libpng-dev libssh-dev unixodbc-dev xsltproc fop libxml2-utils
Next, extract the downloaded source code and navigate to the extracted directory:
tar -zxvf otp_src_24.3.4.tar.gz # Replace with your downloaded version
cd otp_src_24.3.4
Configure and build Erlang:
./configure
make
sudo make install
After successful compilation and installation, verify the Erlang installation:
erl
This should open the Erlang shell.
Note: Building from source requires careful management of dependencies and understanding of the build process. It’s suitable for users who need specific configurations or are comfortable with troubleshooting build-related issues.
The installation of Erlang using any of these methods will enable you to start building robust and scalable applications on Ubuntu 22.04. The choice depends on your preferences and requirements.