# Installing and Building from Source

### Installing with apt

Motion is part of the Debian, Ubuntu and Raspbian repositories and can be installed with the apt tool. The version included with apt will be the version that was available when the distribution version was initially deployed and therefore may not represent the latest release.

To install via apt, open up a terminal window and type: `sudo apt-get install motion`

### Installing with a release deb package

Motion can also be installed from the release deb files which may provide a more recent version than what is available via apt.

Determine the deb file name that is appropriate for the distribution and platform from the Releases page and open up a terminal window and type:

`wget https://github.com/Motion-Project/motion/releases/{deb package name}`

Next, install the retrieved deb package. Below is a sample method to install that uses the gdebi tool.

`sudo apt-get install gdebi-core`

`sudo gdebi {deb package name}`

### Building a deb package

Users can also build their own deb package from source. It is often preferable to build and install a deb package rather than using the build from source options below since it is much easier to upgrade from and un-install Motion. The script that builds the deb packages released can be used to do this. Open a terminal window and type: `wget https://raw.githubusercontent.com/Motion-Project/motion-packaging/master/builddeb.sh` Since this is a script file, once it is downloaded open the file with a text viewer and review the script to see what it does. Never ever run a script without reviewing what it does!.

Within the script, you will see that it takes a few parameters which are optional. `Username, EmailAddress, branch, install, arch` If the parameters are not provided, the script will create defaults.

The general flow of the script is as follows:

- Validate input parameters and set defaults as needed.
- Validate the required packages are installed and notify which ones are missing.
- Create temporary directories in /tmp
- Clone the Motion source code and packaging code to the temporary directories
- Switch to the branch specified in the parameter (or master if not specified)
- Build the debian package
- Clean up temporary directories

Once you have reviewed the script and determined that it is OK to run, close out of the editor and then run the command `chmod +x ./builddeb.sh` to make the script executable.

Run the script `./builddeb.sh myname, myemail, master, n, any`

The script will validate the dependencies and report any additional dependencies which must be installed or if all the dependencies are installed, it will create a deb package. Note that this may take a bit of time on slower computers.

Once the deb file is created, install it via the gdebi tool as described above.

### Abbreviated Building Guide

If you are familiar with the building of applications, then the following is a basic build / install script. If errors occur during the process or you wish to customize the build, please review the extended building instructions further below.

Debian / Ubuntu / Raspbian Packages

`sudo apt-get install autoconf automake build-essential pkgconf libtool libzip-dev libjpeg-dev git libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev libwebp-dev gettext libmicrohttpd-dev`

`cd ~`
`git clone https://github.com/Motion-Project/motion.git`
`cd motion`
`autoreconf -fiv`
`./configure`
`make`
`make install`

FreeBSD

`sudo pkg install autoconf pkgconf automake gmake git ffmpeg gettext libmicrohttpd`

`cd ~`
`git clone https://github.com/Motion-Project/motion.git`
`cd motion`
`autoreconf -fiv`
`./configure`
`gmake`
`gmake install`

Mac OS X

`brew install ffmpeg pkg-config libjpeg automake postgresql gettext libmicrohttpd`

`cd ~`
`git clone https://github.com/Motion-Project/motion.git`
`cd motion`
`autoreconf -fiv`
`./configure`
`make`
`make install`

### Preparation For Building

In order to build Motion from source many shared libraries must be installed. The particular libraries needed will vary depending upon the features desired.

When you install software using pre-compiled binaries (Redhat type RPMs, Debian debs etc) you normally only get what is needed to run the programs themselves. In order to compile programs from source that use these libraries you also need to install the development packages. These are normally called the same name as the package suffixed by -devel or -dev. These development packages contain the header files (xxx.h) that Motion needs to build. If you build a library from sources you already have these header files. It is recommended to simply install the pre-compiled binary packages and their development brothers.

Open a terminal and run the following commands to install the packages.

#### Debian/Ubuntu/Raspbian

- Required

`sudo apt-get install autoconf automake build-essential pkgconf libtool git libzip-dev libjpeg-dev gettext libmicrohttpd-dev`

- Optional Packages

- FFMpeg Functionality(Required for creating movies, using network cameras, etc.)

`sudo apt-get install libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev`

- MySQL database functionality

`sudo apt-get install mysql-server libmysqlclient-dev`

or (depends upon your distribution and version)

`sudo apt-get install default-libmysqlclient-dev`

- PostgreSQL database functionality

`sudo apt-get install libpq-dev`

- SQLite3 database functionality

`sudo apt-get install libsqlite3-dev`

- JPEG Turbo

`sudo apt-get install libjpeg-turbo8 libjpeg-turbo8-dev`

- Webp Image Support

`sudo apt-get install libwebp-dev`

#### openSUSE

- Required

`sudo zypper install autoconf automake libtool git`

`sudo zypper install --type pattern devel_basis`

`sudo zypper install libjpeg8-devel gettext libmicrohttpd`

`sudo zypper install -t pattern devel_C_C++`

- Optional Packages

- FFMpeg Functionality(Required for creating movies, using network cameras, etc. SEE NOTE BELOW!)

`sudo zypper ar -f -n packman-essentials http://packman.inode.at/suse/openSUSE_13.1/Essentials/ packman-essentials`

`sudo zypper ar -f -n packman-multimedia http://packman.inode.at/suse/openSUSE_13.1/Multimedia/ packman-multimedia`

`sudo zypper install libffmpeg-devel`

- MySQL database functionality

`Not known by author`

- PostgreSQL database functionality

`Not known by author`

- SQLite3 database functionality

`Not known by author`

- Webp Image Support

`sudo zypper install libwebp-devel`

Important ffmpeg note: The ffmpeg libraries indicated above are provided by a external repository. This may change in the future. Validate that the repository is still valid when doing the install on openSUSE systems. The default for the configure is to require that ffmpeg is installed. Use the configure option to compile without the ffmpeg functionality.

#### FreeBSD

- Required packages

`sudo pkg install autoconf pkgconf automake gmake git gettext libmicrohttpd`

- Optional packages

- FFMpeg Functionality(Required for creating movies, using network cameras, etc.)

`sudo pkg install ffmpeg`

- MySQL database functionality

`sudo pkg install mysql57-client openssl`

- PostgreSQL database functionality

`sudo pkg install postgresql95-contrib`

- SQLite3 database functionality

`sudo pkg install sqlite3`

- Webcam Support

`sudo pkg install v4l_compat webcamd`

- Add cuse_load="YES" in /boot/loader.conf
- Add webcamd_enable="YES" in the /etc/rc.conf
- chmod 666 /dev/video0

- Webp Image Support

`sudo pkg install libwebp`

- Temorary bktr Support

`sudo kldload bktr_mem.ko`

`sudo kldload bktr.ko`

`sudo sysctl hw.bt848.card=1` ( Miro pctv )

`sudo sysctl hw.bt848.tuner=10` ( PHILIPS_FR1216_PAL )

`sudo sysctl hw.bt848.format=0` ( PAL )

- Persistent bktr Support

- Add `bktr_mem_load="YES"` in /boot/loader.conf
- Add `bktr_load="YES"` in /boot/loader.conf
- Add `hw.bt848.card=1` in /etc/sysctl.conf
- Add `hw.bt848.tuner=10` in /etc/sysctl.conf
- Add `hw.bt848.format=0` in /etc/sysctl.conf

#### CentOS 7

- Required

`sudo yum groupinstall 'Development Tools'`

`sudo yum install libjpeg-turbo libjpeg-turbo-devel gettext libmicrohttpd-devel`

`sudo yum install libzip-devel`

- Optional Packages

- FFMpeg Functionality(Required for creating movies, using network cameras, etc. SEE NOTE BELOW!)

`sudo yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm`

`sudo yum install ffmpeg ffmpeg-devel`

- MySQL database functionality

`Not known by author`

- PostgreSQL database functionality

`Not known by author`

- SQLite3 database functionality

`sudo yum install sqlite-devel`

- Webp Image Support

`Not known by author`

Important ffmpeg note: The ffmpeg libraries indicated above are provided by a external repository. This may change in the future. Validate that the repository is still valid when doing the install on CentOS systems. The default for the configure is to require that ffmpeg is installed. Use the configure option to compile without the ffmpeg functionality.

#### MacOSX

- Required

- Install brew as described on https://brew.sh

`brew upgrade ffmpeg pkg-config jpeg automake gettext libmicrohttpd-dev`

`brew install ffmpeg pkg-config libjpeg automake`

- Optional Packages
- PostgreSQL database functionality

`brew upgrade postgresql` `brew install postgresql`

- None known by author

### Configure Script

Motion uses a set tools called the "autotools" in order to generate the required scripts in order to compile and install Motion. The next step after installing all of the required and desired libraries is to have the autotools create a configure script. To do this open a terminal and change to the directory with the source code and type

`autoreconf`

If the 'configure' file exists and contains a valid script, the tool will return immediately since no additional work needs to be completed. If the script needs updating, then it will take a moment to return. Once it has been executed, a file called 'configure' will exist in the directory. Note that if the command is run as

`autoreconf -f`

The -f parameter instructs it to force a new configure file to be created. This can be preferable in certain situations so that the configure script gets updated with the correct version number. Once the 'configure' file is created, we can execute it. What the script does is interrogate the system and look for all the needed items in order to compile Motion. In this process it looks to determine which optional components have been installed on the system and if found sets flags to indicate for them to be included. If a particular library is required by Motion and is not found, the configure script will issue an error. The error means that the library was not found because it was either not installed or that it was installed into a location that the script could not find. With the Motion configure script, once it has ended it also lists out all of the optional components that were located. Note that if you KNOW that a particular component is installed yet the configure script reports it as not installed, then it may be necessary to use one or more of the configure options described below to tell the script where to find the particular component.

To run the configure your current directory must be the motion directory. You type

`./configure`

You can add the parameter `./configure --help` to get help on the different switches.

When the configure script finishes you should validate that the options desired were correctly identified by the configure. In particular, the ffmpeg option is occasionally not found even if it is actually installed. Various users have indicated this to be a particular problem with the PI. If using a PI and have this issue, you can use the following option

`./configure --with-ffmpeg=/usr/lib/arm-linux-gnueabihf`

For a long term option, you can edit the file `$HOME/.bashrc` and within it place the following two lines at the end `PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf:$PKG_CONFIG_PATH` `export PKG_CONFIG_PATH` This option will however only become effective the next time you open the terminal shell.

The following options can be specified with the configure script to customize how Motion is built.

Options | Description Defaults for the options are specified in brackets [ ] | Editors comment
-h, --help | display this help and exit |
--help=short | display options specific to this package | This command shows the options special to Motion.
--help=recursive | display the short help of all the included packages |
-V, --version | display version information and exit | Provides the version number of the source code and autotools
-q, --quiet, --silent | do not print `checking...' messages | Illustrates only the results of the script.
--cache-file=FILE | cache test results in FILE. [disabled] | No function
-C, --config-cach | alias for `--cache-file=config.cache' | No function
-n, --no-create | do not create output files | Used for testing if other switches produce error - without writing anything to the disk
--srcdir=DIR | find the sources in DIR. [configure dir or `..'] | DIR is a directory path.
Installation directories: |  |
--prefix=PREFIX | install architecture-independent files in PREFIX [/usr/local] | The default /usr/local means that The executable binary "motion" is installed in /usr/local/bin The manual page in /usr/local/man/man1 The document files in /usr/local/docs/motion The configuration file in /usr/local/etc/motion The example config files in /usr/local/motion/examples If you are experimenting with many parallel versions it may be desirable to set the PREFIX to e.g. /usr/local/motion and then add /usr/local/motion/bin to your search path (or simply cd /usr/local/motion/bin before execution). This way you can change version just by changing the symbolic link in /usr/local/motion. If you are installing the software on a machine where you do not have access to the /usr/local but have write access to a home directory, then you should change this to point to a directory within your home tree. Example: --prefix=$HOME
--exec-prefix=EPREFIX | install architecture-dependent files in EPREFIX [PREFIX] | This defines an alternative installation directory for the executable binary. Note: The executable binary will be placed in a directory "bin" below the directory specified by this option Author recommends leaving this as default (i.e. not setting it).
--bindir=DIR | user executables [EPREFIX/bin] | With this option you can control exactly which directory the executable binary is installed. The previous option automatically adds the bin directory. Here you are in full control of the directory.
--sbindir=DIR | System admin executables [EPREFIX/sbin] | Not used by Motion.
--libexecdir=DIR | program executables [EPREFIX/libexec] | Not used by Motion.
--datadir=DIR | read-only architecture-independent data [PREFIX/share] | Not used by Motion.
--sysconfdir=DIR | read-only single-machine data [PREFIX/etc] | This is where Motion both installs the default configuration file and also where it will later search for it. Motion searches for the configuration file "motion.conf" in the following order: 1. Current directory from where Motion was invoked 2. $HOME/.motion 3. The motion subdirectory inside the sysconfig directory set by this switch. If not defined the default is /usr/local/etc/ Editor recommends leaving this at default. Be careful if you run "make install" again. This will overwrite the motion-dist.conf file that you may have edited.
--sharedstatedir=DIR | modifiable architecture-independent data [PREFIX/com] | Not used by Motion.
--localstatedir=DIR | modifiable single-machine data [PREFIX/var] | Not used by Motion.
--libdir=DIR | object code libraries [EPREFIX/lib] | Not used by Motion.
--includedir=DIR | C header files [PREFIX/include] | Not used by Motion.
--oldincludedir=DIR | C header files for non-gcc [/usr/include] | Not used by Motion.
--infodir=DIR | info documentation [PREFIX/info] | Not used by Motion.
--mandir=DIR | man documentation [PREFIX/man] | Editor recommends the default.
Optional Packages: |  |
--- | --- | ---
--without-bktr | Exclude bktr video subsystem devices | ONLY used in *BSD
--without-v4l2 | Exclude using v4l2 (video4linux2) subsystem. Makes Motion so it only supports network cameras. | Can be used if you do not need support or lack some of the libraries for it.
--without-webp | Compile without webp image support |
--with-ffmpeg=DIR | Specify the path for the directory prefix in which the library and headers are installed. If not specified configure will search in /usr/ and /usr/local/ | DIR is the directory PREFIX in which the ffmpeg shared libraries and their headers are installed. If you install ffmpeg from sources and use the default directories or if ffmpeg is installed as a binary package (RPM or deb) you may not need to specify the directory prefix. Configure should find the libraries automatically. If you installed ffmpeg from sources and specified a different --prefix when building f fmpeg you must use the same value for the DIR ( --with-ffmpeg=DIR) or export that location to be included in the PKG_CONFIG_PATH The option of --with-ffmpeg is the default for Motion. If the required libraries are not located, the configure script will stop at the ffmpeg section and report which libraries need to be installed. Once the required libraries are installed, run the script again. As noted previously, make sure to install the -dev versions. For more information on FFmpeg see the documentation for the FFmpeg project.
--without-ffmpeg | Do not compile with ffmpeg | Use this if you do not want to compile with ffmpeg. If ffmpeg is not installed you must specify this option for Motion to build without ffmpeg.
--without-mysql | Do not compile with MySQL support | Use this if you do not want to include MySQL support in the package. This can also be useful if you get compilation errors related to MySQL and you actually do not need the feature anyway.
--without-pgsql | Do not compile with PostgreSQL support | Use this if you do not want to include PostgreSQL support in the package. This can also be useful if you get compilation errors related to PostgreSQL and you actually do not need the feature anyway.
--without-sqlite3 | Disable sqlite3 support in motion. | Use this if you do not want to include SQLite3 support in the package. This can also be useful if you get compilation errors related to SQLite3 and you actually do not need the feature anyway.
--without-optimizecpu | Exclude autodetecting platform and cpu type. This will disable the compilation of gcc optimizing code by platform and cpu. | Use this if the optimization causes problems. Not typically needed.
--with-developer-flags | Add additional warning flags for the compiler. | This option is for developers only. It checks to ensure that the code is robust.

### Make

Once the configure script has successfully finished and indicates all options desired, a 'makefile' is created. The makefile builds the Motion program and links in all of the required libraries. To run the makefile on most distributions, type: `make` For FreeBSD distributions use `gmake`

The makefile will go through each of the files and compile them. Depending upon the source code obtained, there may be warnings or usually none.

If the notifications indicate undefined references, then it is most likely that an additional library needs to be added in via the configure switches. Many of these additional missing libraries issues may be related to the version of ffmpeg and how it was built or installed. The following is a sample of some of the extra ffmpeg libraries that may need to be added to pkg-config `.pc` files that are located in the ffmpeg /lib/pkgconfig directory.

`-lavformat -lswscale -lavcodec -lavutil -lfdk-aac -lswresample -lm -lopus -lz -lva -lvpx -lx264 -lmp3lame -lbz2 -ldl -lvorbisenc -lvorbis -ltheoraenc -ltheoradec`

Once the makefile has completed correctly, it will report 'build complete'.

If you have run `make` before, you should run a `make clean` before running `make` again. This cleans out all the object files that were generated the previous time you ran `make`. If you do not run `make clean` first before you rebuild Motion you may not get the additional feature included. For example: If you built Motion without ffmpeg support and then add it later and rebuild Motion without running `make clean` first the ffmpeg feature does not get compiled into the Motion binary.

The first time you build Motion run `./configure`, `make`, `make install`. If you need to build it again (to run with different configure options) run `./configure`, `make clean`, `make`, `make install`.

### Make Install

For most distributions type `make install` to install the files. For FreeBSD systems, use `gmake install`

These commands create the required directories and copy the files into the following locations. (default directories):

- /usr/local/bin
- usr/local/man/man1
- /usr/local/etc/motion
- /usr/local/share/doc/motion
- /usr/local/share/motion/examples

The contents of what gets copied into each directory is as follows: (assuming the default PREFIX /usr/local was used when running configure. Otherwise adjust to the actual directory specified during the configure step.)

- Executable binary "motion" to /usr/local/bin
- Manual page "motion.1" to /usr/local/man/man1
- Document files "CHANGELOG, COPYING, CREDITS, motion_guide.html, and README.md to /usr/local/share/doc/motion
- Example configuration files "*.conf" to /usr/local/share/motion/examples
- Configuration file "motion-dist.conf" to /usr/local/etc/motion

Note that any existing files are overwritten. The default configuration file motion-dist.conf is named like this so that you do not get your working motion.conf file overwritten when you upgrade Motion.

### Un-install

From the Motion base installation directory run `make uninstall`

And delete the base installation directory in /usr/local and any link pointing to it. If you have forgotten where you installed it or someone else did it for you, simply search for the files and directories starting with Motion. If the filenames and the directories match the names described in the "Make Install" section of this document, you can safely delete them.

### Additional Make Options

The make command can be run with several options. `make`, `make install` and `make uninstall` has already been described above.

make clean
  deletes all the binary files (object files) and the motion binary generated by make. It also deletes temporary files and any jpg files that motion has saved in the motion source directory. It is very important to always run `make clean` before you run `make` if you change the configuration (like adding features such as ffmpeg) and rebuild motion.

make distclean
  deletes the files: config.status, config.log, config.cache, Makefile, and motion.spec.

### Upgrading From Older Version

If you are upgrading from an older version of Motion many options have been renamed, added or removed. The author recommends that you start by copying the configuration files from the older version to a safe location for reference only. Then start with a clean copy of the new motion-dist.conf installed and make changes to it.

### Native Language Support(Translations)

Motion now supports limited translations of message printed to the log or displayed on the web control page using the `gettext` application.

Translations should appear automatically based upon the LANG environment variable set on the machine running Motion. To see the LANG code for your machine, open a terminal and type `locale`. If the log and web control still display in English, then this means that translations have not been completed for that language. The Motion developers rely on the community to provide these translations to the project. Even those that do not how to program can assist in this process. The following is an abbreviated description of the processes to update translations:

A .pot file is the base set of phrases and words to translate. To create a .pot file for Motion, first clone the Motion git to a local directory and in a terminal window type `xgettext --keyword=_ -o po/motion.pot *.c` This program scans all the files which end in `.c` looking for the keyword `_`(an underscore) which is the macro in the code to identify a item to translate. The application then outputs a file into the po directory called motion.pot. The motion.pot file is a text file which is a template of all the phrases that can be translated. Translators can add translations to that file and then save it with the particular language code and po extension. e.g. es.po for Spanish

A alternative is to use a common gui translation tool called poedit that is available on most distributions. This is a program that is designed to work with pot / po files. It allows users to open po and pot files and create/edit/merge translations.

It has been observed that it may be easiest to create the motion.pot file (as described above) and then use that file in the poedit application.

Note that in order to simply revise an existing translation, users can edit the po file directly without creating a motion.pot file.

The above process describes the items that have already been tagged within the code for translation. If a particular phrase is reported in the log in English and that phrase is not listed in the motion.pot file, then this means that the phrase needs to be tagged in the code. The process to get the phrase tagging:

1. Find the module in the source code that is printing the message.

2. Validate that the header file "translate.h" is included at the top of the module.

3. Find the particular phrase and surround it with a `_()`

Save the file and then re-create the motion.pot file.