Search

Ubuntu - Install Firefox and Thunderbird extensions from command line

Contents[Hide]

dropcap-mozilla-addon

If you are in charge of a park of Linux workstations, you may need to automate some installation procedures thru scripting.

Under Linux, most of the software installation procedures can be scripted very easily.

But when we are dealing with Firefox or Thunderbird, installation of extensions thru command line is not that easy. It is possible, but quite poorly documented.

This article explains how to install or remove Firefox and Thunderbird extensions through command line. Installations are possible in user or global mode. It also provides a script in charge of doing all the needed job.

This add-on installation script has been tested on Ubuntu 14.04 Amd64 LTS with Firefox 42 and Thunderbird 38.

It should work for any modern Linux distro.

1. Principle

Firefox and Thunderbird allow 2 installation modes for their extensions :

  • Global mode
    These extensions will be available for any user logged on the computer.
    They can only be installed or removed by root user.
  • User mode
    These extensions will only be available for current user.
    They can then be uninstalled by the user.

Every Firefox or Thunderbird extension is provided as a xpi file and is registered by an identifier string, called UID.

A xpi file is in fact a zip archive where the UID is found in a manifest file named install.rdf :

install.rdf
...
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:install-manifest">
    <em:id>{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}</em:id>
...

So, to install an extension for either Firefox or Thunderbird, you need to :

  1. Download the xpi archive
  2. Retrieve the UID string from the archive
  3. Select installation path according to installation mode (user or global)
  4. Copy the XPI archive to the installation path
  5. Extract it in a sub-folder named with the extension UID

After that, the extension will be automatically available either for current user or to any user logged on the workstation.

In case of a global extension, you can't remove it from the add-ons page.

ubuntu-mozilla-global-extension

2. Determine Extensions Folders

As every Firefox or Tunderbird extension provides an install.rdf file, you can easily find the extension directories on your system by searching these files.

Terminal
# locate install.rdf
/home/youraccount/.mozilla/firefox/f6by7e8n.default/extensions/fr-dicollecte @dictionaries.addons.mozilla.org/install.rdf
/home/youraccount/.mozilla/firefox/f6by7e8n.default/extensions/printPages2Pdf @reinhold.ripper/install.rdf
/home/youraccount/.thunderbird/8fuvikbr.default/extensions/fr-dicollecte @dictionaries.addons.mozilla.org/install.rdf
/home/youraccount/.thunderbird/8fuvikbr.default/extensions/{e2fda1a4-762b-4020-b5ad-a41df1933103}/install.rdf
/usr/lib/firefox-addons/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}/install.rdf
/usr/lib/thunderbird-addons/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}/install.rdf

We clearly see here that under Ubuntu 14.04 LTS, global extensions are located under :

Software Extension type Installation path
Firefox Global /usr/lib/firefox-addons/extensions
Firefox User /home/youraccount/.mozilla/firefox/f6by7e8n.default/extensions
Thunderbird Global /usr/lib/thunderbird-addons/extensions
Thunderbird User /home/youraccount/.thunderbird/8fuvikbr.default/extensions

 

In user mode, the xxxxxxxx.default directory is a random sub-directory. This random sub-directory name is available from a profiles.ini file located in the same folder.

/home/youraccount/.mozilla/firefox/profiles.ini
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=f6by7e8n.default

3. Script to install/remove extensions

The add-ons installation script will follow these steps :

  1. determine whether we are dealing with a Thunderbird or a Firefox extension
  2. download the Add-on
  3. determine the extension UID
  4. copy the archive content to the target directory (per-user or global)

You just need to download and install the script under /usr/local/sbin :

Terminal
# sudo wget -O /usr/local/sbin/mozilla-extension-manager https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/mozilla/mozilla-extension-manager
# sudo chmod +x /usr/local/sbin/mozilla-extension-manager

If your global extensions folders are differents, you will also need to adapt them in the script.

/usr/local/sbin/mozilla-extension-manager
#!/bin/bash
# -------------------------------------------------------
#  Command line script to handle Firefox and Thunderbird extensions
#  Manage installation and uninstallation
#  Handle global and per-user extensions
#  
#  Depends on unzip and wget
#
#  Manual available at http://bernaerts.dyndns.org/linux/74-ubuntu/271-ubuntu-firefox-thunderbird-addon-commandline
#
#  Parameters :
#    --install : install extension
#    --remove  : remove extension
#    --user    : extension as per-user
#    --global  : extension global to system
#    https://addons.mozilla.org/... : URL of extension
#
#  26/03/2013, V1.0 - Creation by N. Bernaerts
#  08/11/2015, V2.0 - Complete rewrite
#                     Add install/remove
#                     Add global/per-user
#  28/12/2016, V2.1 - Add Fedora compatibility thanks to Cedric Brandenbourger
#  07/02/2017  V2.2 - Remove zenity, as running zenity under root with wayland/mir is not working
# -------------------------------------------------------

# -------------------------------------------------------
#   Check tools availability
# -------------------------------------------------------

command -v unzip >/dev/null 2>&1 || { echo "Please install unzip"; exit 1; }
command -v wget >/dev/null 2>&1 || { echo "Please install wget"; exit 1; }

# ------------------------------------------------------
#   Constants
# ------------------------------------------------------

# set default add-ons URL base
URL_FIREFOX="https://addons.mozilla.org/firefox/"
URL_THUNDER="https://addons.mozilla.org/thunderbird/"

# installation path for user extensions
PATH_USER_FIREFOX="$HOME/.mozilla/firefox"
PATH_USER_THUNDER="$HOME/.thunderbird"

# ------------------------------------------------------
#   Set installation path according to distribution
# ------------------------------------------------------

# detect architecture
ARCHITECTURE=$(arch)

# set installation path for debian based distribution (Debian, Ubuntu, ...)
if [ -f /etc/debian_version ]
then
  PATH_GLOBAL_FIREFOX="/usr/lib/firefox-addons/extensions"
  PATH_GLOBAL_THUNDER="/usr/lib/thunderbird-addons/extensions"

# else, set for Fedora 64
elif [ "${ARCHITECTURE}" = "x86_64" ]
then
  PATH_GLOBAL_FIREFOX="/usr/lib64/firefox/extensions"
  PATH_GLOBAL_THUNDER="/usr/lib64/thunderbird/extensions"
  
# else set for Fedora 32 
else
  PATH_GLOBAL_FIREFOX="/usr/lib/firefox/extensions"
  PATH_GLOBAL_THUNDER="/usr/lib/thunderbird/extensions"
fi

# -------------------------------------------------------
#   Default values
# -------------------------------------------------------

# extension family (firefox or thunder)
EXT_FAMILY=""

# extension .xpi URL
EXT_URL=""

# extension installation path
EXT_PATH=""

# extension type (global or per user)
EXT_TYPE="user"

# action to be performed (install or remove)
EXT_ACTION="install"

# -------------------------------------------------------
#   Parameters
# -------------------------------------------------------

for PARAM in "$@"
do
  # action according to parameter
  case "${PARAM}" in
    "--user")    EXT_TYPE="user" ;;
    "--global")  EXT_TYPE="global" ;;
    "--install") EXT_ACTION="install" ;;
    "--remove")  EXT_ACTION="remove" ;;
    *)
      # get extension URL
      EXT_URL="${PARAM}"

      # determine if we are dealing with firefox extension
      RESULT=$(echo "${PARAM}" | grep "${URL_FIREFOX}")
      [ "${RESULT}" != "" ] && EXT_FAMILY="firefox"  
  
      # determine if we are dealing with thunderbird extension
      RESULT=$(echo "${PARAM}" | grep "${URL_THUNDER}")
      [ "${RESULT}" != "" ] && EXT_FAMILY="thunder"
      ;;
  esac
done

# -------------------------------------------------------
#   Product family
# -------------------------------------------------------

# firefox extension 
if [ "${EXT_FAMILY}" = "firefox" ]
then
  PATH_GLOBAL="${PATH_GLOBAL_FIREFOX}"
  PATH_USER="${PATH_USER_FIREFOX}"

# thunderbird extension
elif [ "${EXT_FAMILY}" = "thunder" ]
then
  PATH_GLOBAL="${PATH_GLOBAL_THUNDER}"
  PATH_USER="${PATH_USER_THUNDER}"
  
# none, error 
else
  echo "Given parameter is neither a firefox nor a thunderbird extension"
  exit 1
fi

# -------------------------------------------------------
#   User or Global extension
# -------------------------------------------------------

# if global extension, set installation path
if [ "${EXT_TYPE}" = "global" ]
then
  EXT_PATH="${PATH_GLOBAL}"

# else determine installation path for user extensions
else
  # set profile.ini path
  PROFILE_INI="${PATH_USER}/profiles.ini"
  
  # get profile path
  PROFILE_PATH=$(grep "Path=" ${PROFILE_INI} | head -n 1 | cut -d'=' -f2)
  
  # set user profile path or exit if it doesn't exist
  [ "${PROFILE_PATH}" != "" ] && EXT_PATH="${PATH_USER}/${PROFILE_PATH}/extensions" || { echo "User profile doesn't exist"; exit 1; }
fi   

# -------------------------------------------------------
#   Installation / Removal
# -------------------------------------------------------

# download extension
rm -f addon.xpi 
wget -O addon.xpi "${EXT_URL}"

# get extension UID from install.rdf
EXT_UID=$(unzip -p addon.xpi install.rdf | grep "<em:id>" | head -n 1 | sed 's/^.*>\(.*\)<.*$/\1/g')

# if .xpi file could not be read, exit
[ "${EXT_UID}" = "" ] && { echo "Could not retrieve extension file from server"; exit 1; }

# action : installation
if [ "${EXT_ACTION}" = "install" ]
then
  # check if extension not already installed
  [ -d "${EXT_PATH}/${EXT_UID}" ] && { echo "${EXT_TYPE} extension ${EXT_UID} is already installed"; exit 1; }

  # installation of global extension
  if [ "${EXT_TYPE}" = "global" ]
  then
    # copy .xpi to global extension path
    sudo cp -f addon.xpi "${EXT_PATH}/${EXT_UID}.xpi"

    # extract extension to global extension path
    sudo unzip addon.xpi -d "${EXT_PATH}/${EXT_UID}"

  # else, installation of extension for current user 
  else
    # copy .xpi to user profile extension path
    cp -f addon.xpi "${EXT_PATH}/${EXT_UID}.xpi"

    # extract extension to user profile extension path
    unzip addon.xpi -d "${EXT_PATH}/${EXT_UID}"
  fi

  # end message
  echo "${EXT_TYPE} extension ${EXT_UID} installed"

# action : removal
else
  # check if extension is installed
  [ -d "${EXT_PATH}/${EXT_UID}" ] || { echo "${EXT_TYPE} extension ${EXT_UID} was not installed"; exit 1; }

  # removal of a global extension
  if [ "${EXT_TYPE}" = "global" ]
  then
    # remove global extension directory
    sudo rm "${EXT_PATH}/${EXT_UID}.xpi"
    sudo rm -R "${EXT_PATH}/${EXT_UID}"

  # else, removal of extension for current user 
  else
    # remove user xpi file and extension directory
    rm "${EXT_PATH}/${EXT_UID}.xpi"
    rm -R "${EXT_PATH}/${EXT_UID}"
  fi

  # end message
  echo "${EXT_TYPE} extension ${EXT_UID} removed"
fi

# remove downloaded file
rm -f addon.xpi

4. Install your first Add-On

Now that the script is installed, we can test it.

Go to any add-on page of https://addons.mozilla.org/ and copy the https://... link provided by the Add to Firefox or Download Now button.

ubuntu-firefox-add-button   ubuntu-thunderbird-add-button

Then from a console, just type :

Terminal
# mozilla-extension-manager --install https://addons.mozilla.org/firefox/downloads/latest/665872/addon-665872-latest.xpi
...
user extension 6asa42dfa4784fsf368g @youtubeconverter.me installed

Please note that if you install an extension as a global one, some commands will be executed as root (sudo).

Terminal
# mozilla-extension-manager --install --global https://addons.mozilla.org/firefox/downloads/latest/665872/addon-665872-latest.xpi
[sudo] password for youraccount:
...

global extension 6asa42dfa4784fsf368g @youtubeconverter.me installed

You can launch Firefox or Thunderbird and check in the Add-ons menu.

Note that under Firefox, if you add a user extension thru command line, you'll get a validation message on the first launch following installation :

firefox extension confirmation

5. Remove an extension

To remove an extension installed by the script, just add --remove to the command line.

Please note that an extension installed as per-user or global should be removed with the same option.

Terminal
# mozilla-extension-manager --remove --global https://addons.mozilla.org/firefox/downloads/latest/665872/addon-665872-latest.xpi
[sudo] password for nicolas:
...

global extension 6asa42dfa4784fsf368g @youtubeconverter.me removed

 

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus