How to Install Asterisk on Ubuntu 20.04

By 

Published on

4 min read

Install Asterisk on Ubuntu

Asterisk is a popular open-source PBX platform for developing communications applications such as conference servers and VoIP gateways. It is used by individuals, small businesses, large enterprises, and governments worldwide.

Asterisk features include voicemail, music on hold, conference calling, call queuing, call recording, interactive voice response, SMS messaging, and more.

This tutorial explains how to install Asterisk on Ubuntu 20.04.

Ubuntu repositories include an older Asterisk version. We’ll install the latest Asterisk from the source code.

Prerequisites

Install the following packages that are necessary to download and build Asterisk:

Terminal
sudo apt update
sudo apt install wget build-essential git autoconf subversion pkg-config libtool

Installing DAHDI and LibPRI

DAHDI is a set of drivers and utilities that allows Asterisk to communicate with analog and digital telephones. The LibPRI library allows Asterisk to communicate with ISDN connections. If you don’t need these libraries, you can skip this section.

Switch to the /usr/src directory and download and install DAHDI:

Terminal
cd /usr/src/
sudo git clone -b next git://git.asterisk.org/dahdi/linux dahdi-linux
cd dahdi-linux
sudo make
sudo make install
Terminal
cd /usr/src/
sudo git clone -b next git://git.asterisk.org/dahdi/tools dahdi-tools
cd dahdi-tools
sudo autoreconf -i
sudo ./configure
sudo make install
sudo make install-config
sudo dahdi_genconf modules

Run the following commands to build LibPRI:

Terminal
cd /usr/src/
sudo git clone https://gerrit.asterisk.org/libpri libpri
cd libpri
sudo make
sudo make install

Installing Asterisk

Clone the Asterisk source in the /usr/src directory:

Terminal
cd /usr/src/
sudo git clone -b 18 https://gerrit.asterisk.org/asterisk asterisk-18

At the time of writing, the latest version of Asterisk is 18.x. If there is a new version available, change the branch number in the command above.

Before continuing with the next steps, change to the Asterisk source directory:

Terminal
cd asterisk-18/

Download the MP3 sources which are required to build the MP3 module and use MP3 files on Asterisk:

Terminal
sudo contrib/scripts/get_mp3_source.sh

Run the install_prereq script to install the necessary dependencies:

Terminal
sudo contrib/scripts/install_prereq install

The configure script performs several checks to make sure all of the dependencies on your system are present. Run the script by typing:

Terminal
sudo ./configure

The next step is to select the modules you want to compile and install. Access menuselect, by typing:

Terminal
sudo make menuselect

Select the “format_mp3” option to tell Asterisk to build the MP3 module:

asterisk mp3

Once you are finished, switch to the “Save and Exit” button and press “Enter”.

Start the compilation process:

Terminal
sudo make -j2

The compilation may take some time, depending on your system. You can modify the -j flag according to the number of cores in your processor.

Once completed, install Asterisk and its modules by typing:

Terminal
sudo make install

You can install either the generic configuration files with reference documentation by typing:

Terminal
sudo make samples

Or install the basic PBX configuration files:

Terminal
sudo make basic-pbx

The last step is to install the Asterisk init script by typing:

Terminal
sudo make config

It is also a good idea to run ldconfig to update the shared libraries cache:

Terminal
sudo ldconfig

Creating Asterisk User

By default, Asterisk runs as the root user. We’ll create a new system user and configure Asterisk to run as the newly created user for security reasons.

Run the following command to create a new system user named asterisk:

Terminal
sudo adduser --system --group --home /var/lib/asterisk --no-create-home --gecos "Asterisk PBX" asterisk

To configure Asterisk to run as asterisk user, open the /etc/default/asterisk file and uncomment the following two lines:

Terminal
sudo nano /etc/default/asterisk
/etc/default/asteriskconf
AST_USER="asterisk"
AST_GROUP="asterisk"

Add the asterisk user to the dialout and audio groups:

Terminal
sudo usermod -a -G dialout,audio asterisk

We also need to change the ownership and permissions of all asterisk files and directories so the user asterisk can access those files:

Terminal
sudo chown -R asterisk: /var/{lib,log,run,spool}/asterisk /usr/lib/asterisk /etc/asterisk
sudo chmod -R 750 /var/{lib,log,run,spool}/asterisk /usr/lib/asterisk /etc/asterisk

Starting Asterisk

Now that you are all set up, start the Asterisk service with the following command:

Terminal
sudo systemctl start asterisk

To verify that Asterisk is running, connect to the Asterisk command-line interface (CLI) by typing:

Terminal
sudo asterisk -vvvr

You’ll see the default Asterisk CLI prompt:

output
Connected to Asterisk GIT-18-263f906af4 currently running on ubuntu2004 (pid = 91303)
ubuntu2004*CLI>

The last step is to enable Asterisk service to start on boot with:

Terminal
sudo systemctl enable asterisk

Configuring Firewall

The firewall will secure your server against unwanted traffic.

If you don’t have a firewall configured on your server, you can check our guide about how to set up a firewall with ufw on ubuntu

By default, SIP uses the UDP port 5060, to open the port run:

Terminal
sudo ufw allow 5060/udp

If you enabled the Real Time Protocol (RTP) then you also need to open the following port range:

Terminal
sudo ufw allow 10000:20000/udp

Feel free to adjust the firewall according to your needs.

Conclusion

We’ve shown you how to install the latest Asterisk version from the source on Ubuntu 20.04.

To learn more about how to configure and use Asterisk check the official documentation and

If you hit a problem or have feedback, leave a comment below.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page