How To Install the Lita Chat Bot for IRC on Ubuntu 14.04
Many modern DevOps teams are putting more and more infrastructure around the chatroom. There are lots of chatrooms available, from commercial options (like HipChat and Slack) to DIY options (like IRC or Jabber/XMPP). One of the main motivations behind this chatroom infrastructure is because of chatroom bots, which DevOps teams use to help get their work done faster and more efficiently. This movement is sometimes called ChatOps.
This article will outline how to install the bot Lita, which is heavily inspired by GitHub's Hubot. Lita is written entirely in Ruby and works in many kinds of chatrooms, like HipChat, Campfire, and IRC. Its utility comes largely from the plugins you can add to it, which can do things like performing a Google search or deploying a website.
In this tutorial, we'll walk through the steps to install Lita on your own Ubuntu server, connect it to a IRC channel, and start installing plugins.
To follow this tutorial, you will need:
One Ubuntu 14.04 Droplet.
A sudo non-root user, which you can set up by following this tutorial.
Because we will be setting up Lita for use on IRC, you'll also need to connect to IRC yourself and join a channel.
There are several ways to access IRC. You can set up your own IRC server, but if you're just trying Lita out, it's easier to use a public server (like freenode, IRCnet, or EFnet).
Freenode in particular has a simple web chat available so you can connect without even having to download an IRC client. To use it, pick a unique nickname and channel name, then click Connect. This tutorial will assume you're using Freenode.
Step 1 — Installing Lita
In this step, we will install Lita and its dependencies.
We'll need to install Ruby, the language that Lita is written in. Although Ruby is included in the default Ubuntu repositories, the version of Ruby available in 14.04 is too old for Lita to work.
So first, load a PPA (Personal Package Archive) that has a more current version.
- sudo apt-add-repository ppa:brightbox/ruby-ng
Then update the packages on your server.
- sudo apt-get update
Next, install Ruby and some additional dependencies that Lita requires.
- sudo apt-get install ruby2.2 ruby2.2-dev build-essential libssl-dev redis-server
Finally, we'll install Lita using RubyGems, which is a package manager for ruby (much like APT is a package manager for Ubuntu). APT uses the command
apt-get; RubyGems uses the command
Install Lita and the Lita IRC adapter.
- sudo gem install lita
- sudo gem install lita-irc
Step 2 — Configuring Lita
The software for Lita is now installed, so in this step, we'll create a bot and set it up to connect to our IRC channel.
The following command will create a new bot. Replace
your_bot_name with a unique nick for your bot.
- lita new your_bot_name
That command will also create a directory in your homedir,
~/your_bot_name, which contains two files:
Gemfile tells Ruby which plugins to load.
lita_config.rb is where all the plugin configuration settings are stored. For the
lita_config.rb will have information on the IRC server, channel, nickname, etc.
Before we can launch Lita, we need to edit these config files so your bot knows how to connect to IRC. First, change into the new directory.
- cd ~/your_bot_name/
Gemfile for editing using nano or your favorite text editor.
- nano Gemfile
Find the line
# gem "lita-irc" and remove the
# ` at the beginning of the line, which uncomments it.
. . . # Uncomment to use the IRC adapter gem "lita-irc" . . .
Then save and close the file.
Next, open the
- nano lita_config.rb
config.robot.name parameter toward the top of the file from
Lita to your bot's nickname.
Lita.configure do |config| # The name your robot will use. config.robot.name = "your_bot_name" . . .
Slightly farther down, change the
config.robot.adapter parameter from
. . . # The adapter you want to connect with. Make sure you've added the # appropriate gem to the Gemfile. config.robot.adapter = :irc . . .
Then, at the bottom of the file, add the following lines (highlighted below) before the last line,
Make sure you copy the indentation correctly, and also replace the placeholder variables in the
config.adapters.irc.user lines with your channel name and your bot name, respectively. If you're using an IRC server other than Freenode, you should modify the
config.adapters.irc.server appropriately as well.
. . . ## Example: Set configuration for any loaded handlers. See the handler's ## documentation for options. # config.handlers.some_handler.some_config_key = "value" config.adapters.irc.server = "irc.freenode.net" config.adapters.irc.channels = ["#your_channel_name"] config.adapters.irc.user = "your_bot_name" config.adapters.irc.cinch = lambda do |cinch_config| cinch_config.max_reconnect_delay = 123 end end
Save and close the file.
For a complete list of what options can be passed to the 'lita-irc' plugin, check out the lita-irc plugin's GitHub page.
Step 3 — Connecting to a Channel
In this step, we will start the bot and set an admin.
Make sure you're connected to IRC using your IRC client of choice or a web client, and that you're in the channel that you want Lita to join. Then, start your bot.
- lita start
You will see a lot of output. After a moment, you will see your bot join the IRC channel you specified.
At this point, we haven't told our bot how to do anything, so it isn't very useful yet. Before we add some extra features, let's define our user as an admin. To do this, you'll need to ask your bot what your ID is. Send the following line to the IRC channel.
your_bot_name users find your_irc_nickname
Your bot will respond with something like this:
your_irc_nickname<(ID: ff97cx41-b7fd-4x9b-x76d-e76xf443c65x, Mention name: your_irc_nickname)
Copy the ID, then stop Lita by entering
CTRL+C in the terminal of your server.
- nano lita_config.rb
At the end of the file, before the final
end, add the following line. Replace the ID here with the ID you copied.
... config.adapters.irc.cinch = lambda do |cinch_config| cinch_config.max_reconnect_delay = 123 end config.robot.admins = ["f97cx41-b7fd-4x9b-x76d-e76xf443c65x"] end
Save and close the file.
Step 4 — Adding Plugins
At this point, your bot can connect to IRC, but that's about all it can do. In this step, we'll install an example plugin to add some useful functionality.
While you can write your own plugins in Ruby, there are hundreds of premade plugins to choose from. We'll go over installing the lita-dig plugin (which is a DNS record lookup plugin) as an example, but the process is very similar for others.
First, let's install the
- sudo gem install lita-dig
Gemfile for editing again.
- nano Gemfile
Add the following line at the very end of the file.
Save and close the file, then restart the bot.
- lita start
That's it! When your bot reconnects, send the following line to the IRC channel.
your_bot_name dig digitalocean.com
The bot will respond with several lines, beginning with something like
;; Answer received from 220.127.116.11:53 (114 bytes). This means your plugin is working!
Step 5 — Writing an Upstart Script
In every step so far, we've had to manually start the bot. This is fine for testing, but there's a better solution. In this step, we'll create an Upstart script to manage the bot, making it easy to start, stop, and restart it. For a more detailed look into Upstart scripts, check out this Upstart tutorial.
First, create a file called
sudo nano /etc/init/lita-your_bot_name.conf
Paste the following into the file, replacing
your_username with the username of your sudo non-root user on your server.
start on runlevel  stop on runlevel [^2345] chdir /home/your_username/your_bot_name env HOME=/home/your_username setuid your_username setgid your_username respawn respawn limit 10 5 exec lita start
Here's a quick explanation of what this script does:
start on and
stop on lines tell Ubuntu when to start and stop the bot. The
chdir line tells Upstart which directory to be in when executing
exec command, which actually starts the bot.
env line is required so the Ruby application knows where its settings files are located. The
setgid tells Upstart to start the bot as a certain user; if these lines are omitted, Upstart would launch the bot as root.
respawn line tells Upstart to relaunch the bot if it gets interrupted or crashed. The
respawn limit prevents Upstart from respawning if the bot crashes 10 times in 5 seconds, which is indicative of a bigger problem.
We can check that the syntax on this script is correct by using a built-in linter, which is a syntax checking program, using the following command.
sudo init-checkconf /etc/init/lita-your_bot_name.conf
If the syntax is correct, you should see the following output.
File /etc/init/lita-your\_bot\_name.conf: syntax ok.
Once you've checked the syntax of the config file, start the bot with the Upstart script by entering the following command.
- sudo start lita-your_bot_name
You should see a line which looks like this:
lita-your_bot_name start/running, process 1234
Your bot should join the IRC channel shortly; you can now log out of your server and the bot will continue running.
In this tutorial, we installed and configured Lita with a plugin and an Upstart script. With unique bot names and Upstart scripts, you can even run multiple bots in different chatrooms with different plugins.
Entire companies run their entire IT infrastructure from chatbots much like this one. From custom hooks into popular CI build services like Jenkins to controlling your GitLabs repositories, using a chatbot can simplify, streamline, and automate your work.