Installing Asterisk on the Raspberry Pi?

The Raspberry Pi is a low cost, credit card sized computer that can serve many purposes. One great use case is to leverage the Raspberry Pi’s low power usage and small footprint to run Asterisk, an open source communication software, and turn the Pi into a full featured phone system and PBX.

Installing Asterisk on the Raspberry Pi?

Installing Asterisk on the Raspberry Pi does require some Linux system administration skills, but it is very doable with some patience. This guide will walk through the entire process to get Asterisk up and running on the Raspberry Pi, optimized for home or small office use.

For those new to Asterisk, here is a quick overview:

Asterisk is an open source software implementation of a telephone private branch exchange (PBX). It allows attached telephones to make calls to one another as well as connect to other telephone services like the public switched telephone network (PSTN) and Voice over Internet Protocol (VoIP) services.

Some of the key features of Asterisk include:

  • Call routing and switching
  • Voicemail services
  • Conference calling
  • Interactive voice response menus
  • Call queuing and automatic call distribution
  • Support for a wide range of telephony devices and protocols

Asterisk is highly customizable through its configuration files and powerful dialplan scripting language. It runs on Linux, BSD and Windows servers. The modular architecture allows it to be expanded through loadable modules.

Overall, Asterisk is a mature, robust communications platform used by many businesses, call centers, carriers and hobbyists worldwide. Running it on low cost Raspberry Pi hardware makes it very accessible for home and small office use.

Why Use a Raspberry Pi for Asterisk?

Here are some of the advantages of deploying Asterisk on the Raspberry Pi:

  • Low Cost – Raspberry Pi hardware starts at around $35. Very affordable way to get an Asterisk PBX.
  • Low Power – Raspberry Pi units draw just a few watts of power. Saves on electric costs.
  • Small Footprint – The Pi is very small, about the size of a deck of cards. Easy to tuck away or mount.
  • Linux Based – The Raspberry Pi runs Linux, which Asterisk is designed for. No need to run Asterisk on Windows.
  • Hardware Expandability – The Pi has USB, Ethernet and GPIO allowing you to connect phones, expansion modules and more.

For small office or home office use, the Raspberry Pi makes an inexpensive yet very functional Asterisk server. It is also a great platform for learning Asterisk, as the hardware costs are minimal.

Now let’s look at how to setup Asterisk on the Pi.

Hardware and Software Requirements

To install Asterisk onto a Raspberry Pi, you will need the following hardware and software:

Raspberry Pi – Any model of Raspberry Pi will work, but the Pi 2 or 3 is recommended for better performance.

Power Supply – A 5v microUSB power supply capable of at least 2.5A current.

SD Card – A 8GB or larger SD card to hold the operating system. Class 10 cards provide better performance.

Ethernet Cable – Physical wired network connection to your LAN/router.

Telephony Hardware – Phones, a TDM or FXO/FXS gateway to connect to the PSTN line. USB or GPIO devices work best.

Operating System – Raspbian (recommended), Ubuntu MATE or Fedora support Asterisk.

Asterisk Version – Asterisk 13, 14 or 15. Version 16+ is not yet recommended for the Pi.

With all the hardware and software ready, you can start the installation process.

Step 1 – Install the Operating System

First you’ll need to get a compatible operating system installed onto the Raspberry Pi.

The official Raspbian distribution is recommended as it is Debian based and specially optimized for the Pi’s hardware.

Download the latest Raspbian image from www.raspberrypi.org/downloads.

Use balenaEtcher or a similar tool to flash the OS image onto your microSD card.

With the newly flashed card inserted, connect ethernet, power and any peripherals you need and boot up your Pi.

Run through the initial configuration prompts to set your locale, timezone and WiFi settings as needed.

Update the OS packages to their latest versions:

sudo apt-get update

sudo apt-get upgrade

This will prepare you with an up to date base operating system ready to install Asterisk.

Step 2 – Install Asterisk Dependencies

Asterisk requires a number of supporting libraries and packages to function properly.

Run the following commands to install the required dependencies:

sudo apt-get install libxml2 libncurses5-dev openssl libssl-dev libsrtp2-dev libsqlite3-dev pkg-config libjansson-dev libiksemel-dev libiksemel-utils libiksemel3 libpopt-dev unzip uuid-dev git

This will install all necessary system libraries that Asterisk needs to compile and run.

Step 3 – Create Asterisk User

It is best practice to run the Asterisk service as its own unprivileged user account for security reasons.

Execute these commands to create a system user named asterisk:

sudo useradd -m -r -U asterisk

sudo usermod -a -G audio,dialout asterisk

The dialout group membership is needed for accessing telephony hardware like serial ports or USB devices.

You now have a dedicated OS user for running the Asterisk service.

Step 4 – Download and Install Asterisk

Now you’re ready to actually install the Asterisk software itself.

Change to the /usr/src directory:

cd /usr/src

And download the latest Long Term Support release of Asterisk (version 16 as of this writing):

sudo wget http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-16.6.2.tar.gz

Extract the source tarball:

sudo tar xvf asterisk-16.6.2.tar.gz

This will extract the Asterisk 16 source code into a subdirectory like asterisk-16.6.2.

Change into this newly created directory:

cd asterisk-16.6.2

Now you’re ready to configure and compile the Asterisk source code.

Run the configure script with these compilation flags:

./configure –libdir=/usr/lib/arm-linux-gnueabihf

The libdir flag tells configure the architecture specific library path for ARM based platforms like the Pi.

Once configure completes successfully, compile Asterisk with a make command:

make

And lastly install the compiled binaries and files with this:

sudo make install 

sudo make samples

This installs the Asterisk program binaries as well as various configuration file samples into the appropriate system folders.

Asterisk 16 is now fully installed on your Raspberry Pi!

Step 5 – Configure Asterisk

With Asterisk installed, it’s time to configure it to actually function as a PBX.

Create Directories

First create some directories for Asterisk to use for its runtime files:

sudo mkdir -p /var/lib/asterisk/sounds /var/lib/asterisk/moh /etc/asterisk /var/log/asterisk /var/spool/asterisk

sudo chown -R asterisk. /var/lib/asterisk /etc/asterisk /var/log/asterisk /var/spool/asterisk

These directories will hold audio prompts, music on hold files, configurations, logs and voicemails.

Copy Sample Configuration Files

Asterisk comes with various sample configuration files. Copy these to use as a starting point:

sudo cp /usr/src/asterisk-16.6.2/configs/modules.conf.sample /etc/asterisk/modules.conf

sudo cp /usr/src/asterisk-16.6.2/configs/asterisk.conf.sample /etc/asterisk/asterisk.conf

sudo cp /usr/src/asterisk-16.6.2/configs/extconfig.conf.sample /etc/asterisk/extconfig.conf

Edit these as needed to turn on and configure addons like MP3/codec support, Voicemail, DAHDI Telephony modules etc.

Create /etc/asterisk/sip.conf

To configure SIP devices, create a new /etc/asterisk/sip.conf file using your preferred text editor.

Paste in a basic SIP configuration like this:

[general]

context=default

udpbindaddr=0.0.0.0

tcpenable=no

tcpbindaddr=0.0.0.0  

[example-trunk]

type=friend

context=from-trunk

host=sip-server.example.com

disallow=all

allow=ulaw

qualify=yes

[codecs]  

ulaw = yes

alaw = yes

This sets up a sample SIP trunk and codec support. Customize with your own SIP trunk details.

Create /etc/asterisk/extensions.conf

Asterisk dialplan logic goes into /etc/asterisk/extensions.conf.

For starters, paste in this example context:

[default]

exten => 1000,1,Dial(SIP/example-device,30)

exten => 2000,1,Voicemail(u2000)

exten => 1234,1,Playback(hello-world)

This dials SIP device example-device, checks voicemail for user 2000, and plays a sound clip.

Obviously expand and modify this to suit your needs. Read up on the Asterisk dialplan syntax to build out your call routing logic.

With some initial .conf files set up, you are ready to start Asterisk.

Step 6 – Start the Asterisk Service

To launch Asterisk, switch to the asterisk system user:

sudo su – asterisk

Now start the Asterisk server itself:

asterisk -cvvvv

The -c flag puts Asterisk into the console, with -v increasing verbosity. Use Ctrl-C to exit.

Once running, you should see console output like:

Asterisk Ready

 rx Failed to bind to 0.0.0.0:5036: Address already in use

 Manager ‘sip’ listening on all interfaces

 SIP Settings:

   Listening on :5060

   Listening on :5061

RTP Settings:

   RTP Start 5000, RTP End 23999, RTP Timeout 1800

   RTP Checksums 2, RTCP Interval 5000

   RTCP 0:0x7fffebc104c8 

 CallerID: off

Parsing ‘/etc/asterisk/sip.conf’: Found

Parsing ‘/etc/asterisk/extensions.conf’: Found

Parsing ‘/etc/asterisk/phoneprov.conf’: Found

This shows Asterisk loading configuration files, opening ports and getting ready to route calls.

Try dialing extension 1000 from a test SIP phone to confirm calls are working properly.

To start Asterisk automatically on each boot, create a systemd service file:

sudo nano /etc/systemd/system/asterisk.service

Paste this as the contents:

[Unit]

Description = Asterisk PBX Service

After = syslog.target network.target

[Service] 

Type = simple

User = asterisk

ExecStart = /usr/sbin/asterisk -fc

Restart = always

[Install]

WantedBy = multi-user.target

Then enable it so it starts on boot:

sudo systemctl enable asterisk.service

Reboot your Pi and Asterisk should launch automatically, ready to start routing calls!

Key Takeaways

After following this guide you should have a fully functional Asterisk system ready to go on your Raspberry Pi.

Some key takeaways:

  • The Raspberry Pi makes an inexpensive yet capable hardware platform for running Asterisk.
  • You will need a 5V microUSB power supply capable of at least 2.5A current for reliability.
  • Use Raspbian Linux for the easiest experience, or alternate distributions like Ubuntu MATE or Fedora.
  • Create a dedicated asterisk user account for security best practices.
  • Install required dependencies before compiling Asterisk from source code.
  • Use sample configuration files in /usr/src/asterisk-*/configs as a starting point.
  • Focus on sip.conf, extensions.conf and modules.conf to get going initially.
  • Start Asterisk at first from the CLI to check for errors before running as a service.
  • Consider RPi alternatives like the Pine64 or Banana Pi for higher performance.

With Asterisk up and running smoothly on the Raspberry Pi, you can now focus on tweaking configuration, loading addons, expanding the dialplan and connecting more devices to build a fully customized PBX system!

Frequently Asked Questions

Q: Which Raspberry Pi models are compatible?
A: All models of Pi will work, but the Pi 2 or 3 (and up) are recommended for best performance. Avoid the Pi Zero as it is too underpowered.

Q: Can I install Asterisk using apt-get instead of compiling from source?
A: Yes, but the Asterisk version in the Raspbian repositories lags behind the latest release. Compiling from source is recommended.

Q: Do I need any additional hardware besides the Pi itself?
A: You’ll need a microSD card, power supply, and telephony hardware like phones, a gateway device, etc. Ethernet cable for networking.

Q: How do I connect analog phones and lines?
A: Use a telephony interface like the PI TDM Board or a USB FXO/FXS adapter. Consult the Asterisk wiki for recommended hardware.

Q: Can the Raspberry Pi handle multiple concurrent calls?
A: Yes, though performance depends largely on the Pi model. Optimize your kernel and configure Asterisk appropriately.

Q: Where are log files written so I can troubleshoot issues?
A: Log files are written to /var/log/asterisk. Enable full logging in /etc/asterisk/logger.conf.

Q: What realtime databases does Asterisk support on the Pi?
A: MySQL and PostgreSQL are most common. SQLite works but is not great for performance.

Q: How do I connect SIP trunks from providers like Twilio or VoIP.ms?
A: Configure the trunk in sip.conf with the provider’s server details and credentials. Also open your firewall.

Q: Can I connect the Pi to my Google Voice account?
A: Yes, with a gateway service like Simonics or the RPi GVSIP module. You register each Google Voice number as a SIP trunk.

Q: Where can I find more sample configurations and dialplans?
A: Check the Asterisk wiki, documentation and forum for more configuration tips and sample code.

Q: Is there a web GUI I can use to manage Asterisk on the Pi?
A: Yes, install the FreePBX distro on top of Raspbian. Or lookup other options like ASTGUI or PyFrePBX.

Q: Can I install Asterisk without needing to compile from source code?
A: Yes, you can install Asterisk from binary packages, but these usually lag behind the latest releases. Compiling is recommended to stay up-to-date.

Q: Is SIP the only protocol that will work on the Pi version of Asterisk?
A: No, you can also configure IAX2, PJSIP, and chan_dongle for various devices. The Raspberry Pi can support most protocols Asterisk offers.

Q: How do I backup and restore my Asterisk configuration on the Pi?
A: Regularly backup /etc/asterisk folder contents. Also back up CDRs, voicemails, database data as needed. Restore by overwriting newer files with backups.

Q: Can I connect traditional landline phones to the Raspberry Pi running Asterisk?
A: Yes, use an FXO gateway or telephony card to connect POTS lines and analog phones. You configure them as trunks in Asterisk.

Q: Is Asterisk on the Pi powerful enough to use for a small business phone system?
A: Absolutely, as long as you size your Pi model appropriately for your concurrent call volume. It can reliably serve small companies.

Q: Can I connect the Raspberry Pi Asterisk system to my cell phone?
A: Yes, using a SIP client app on your smartphone. This lets you make and receive calls through your Asterisk PBX while mobile.

Q: Should I use MySQL or PostgreSQL for the Asterisk database back-end on the Pi?
A: Either works well. MySQL is easier to set up initially. PostgreSQL offers more advanced features for large deployments.

Q: Where can I find pre-built Asterisk images for the Raspberry Pi?
A: Distributions like RasPBX, PBX in a Flash, and IncrediblePBX offer pre-configured Asterisk images for the Pi.

Q: Can I run Asterisk on a Raspberry Pi 4 Model B?
A: Yes, the extra horsepower of the Pi 4 works very well. Just be sure to set vm.mmap_min_addr to 0 in /etc/sysctl.conf.

Conclusion

Installing Asterisk on the Raspberry Pi provides an inexpensive way to create a fully featured PBX system. It takes some upfront effort to assemble the hardware and get Asterisk configured properly. But the result is a very capable communications server that can anchor your telephony needs at home or a small office.

With the Pi’s GPIO pins and USB ports, you can easily expand the system by adding on telephony cards, phones, intercom modules and more. The Raspbian OS gives you the flexibility of a Linux system that is easy to customize for your specific needs.

Getting comfortable with editing the various Asterisk configuration files and building your dialplan logic opens up tremendous possibilities. Whether building out an IVR system, setting up complex call routing rules, creating conferences or bridges, integrating text-to-speech and more – an Asterisk PBX allows you to do it all.

So consider the Raspberry Pi + Asterisk combination for an inexpensive but powerful DIY telephony platform. Initial setup takes some effort and Linux admin skills, but the long term payoff is rewarding. Both the Pi and Asterisk communities are very active in providing support, tutorials and add-ons to enhance functionality.

Leave a Comment