How to Compile Unity from Source

These instructions will help you build unity from source. However, there are a
few things to consider:

  • I recommend that you never copy anything you've built locally outside your home directory. Doing so is asking for trouble, especially as we're building the entire desktop shell. If you manage to ruin your system-wide desktop shell you'll be a very sad programmer!
  • I'm assuming that you're running the precise Ubuntu release (still in alpha at the time of writing, but very usable).
  • I'm also assuming that you want to build unity from trunk (that is, lp:unity).
Without further ado, let's get to it:

Getting the source code

If you don't already have Bazaar installed, install it now:

sudo apt-get install bzr

You may want to make yourself a folder for the unity code. I tend to do something like this:
mkdir -p ~/code/unity
cd ~/code/unity

Let's grab the code from launchpad:
bzr branch lp:unity trunk

This may take a while. If you prefer to use Bazaar checkouts instead of branches, that's fine to.

Installing Build Dependancies

We need to get the build-dependancies for unity. Thankfully, apt-get makes this trivial:

sudo apt-get build-dep unity

Compiling Unity

I have a set of bash functions that makes this step significantly easier. To use them, copy the following bash code into a file in your home directory called ".bash_functions":

function recreate-build-dir()
{
   rm -r build
   mkdir build
   cd build
}

function remake-autogen-project()
{
    ./autogen.sh --prefix=/home/thomi/staging --enable-debug
    make clean && make && make install
}

function remake-unity()
{
    recreate-build-dir
    cmake .. -DCMAKE_BUILD_TYPE=Debug -DCOMPIZ_PLUGIN_INSTALL_TYPE=local -DCMAKE_INSTALL_PREFIX=/home/thomi/staging/ -DGSETTINGS_LOCALINSTALL=ON
    make  && make install
}

function unity-env
{
 export PATH=~/staging/bin:$PATH
 export XDG_DATA_DIRS=~/.config/compiz-1/gsettings/schemas:~/staging/share:/usr/share:/usr/local/share
 export LD_LIBRARY_PATH=~/staging/lib:${LD_LIBRARY_PATH}
 export LD_RUN_PATH=~/staging/lib:${LD_RUN_PATH}
 export PKG_CONFIG_PATH=~/staging/lib/pkgconfig:${PKG_CONFIG_PATH}
 export PYTHONPATH=~/staging/lib/python2.7/site-packages:$PYTHONPATH
}

Note: You will need to replace all instances of "/home/thomi" with your own home directory path!

Now run this in a terminal:
echo ". ~/.bash_functions" >> ~/.bashrc

This ensures that the next time you open a bash shell the functions listed above will be available to you. To avoid having to close and re-open a terminal, we can read them manually just this once:
. ~/.bash_functions

You should now be able to run:
remake-unity

from the trunk/ directory we created earlier. That's it - you're building unity!

Not so Fast!

Chances are, while trying to build unity, you found that it needed a newer version of one of the several supporting projects than you had installed. At the time of writing, you can't compile unity without first building nux from sources first. Thankfully, that's pretty easy with the use of the functions you now have set up.
First we get the source code:

mkdir -p ~/code/nux
cd ~/code/nux
bzr branch lp:nux trunk
cd trunk

Then we need to get the build dependencies for nux.

sudo apt-get build-dep nux

Unfortunately there are a fewpackages missing, so you'll want to install them as well:

sudo apt-get install gnome-common libibus-1.0-dev libgtest-dev google-mock libxtst-dev

Then we use the functions above to build nux:
 
remake-autogen-project

That's it! You can then go back and build unity - hopefully this time with better success.

Build Notes

You may have noticed that the remake-* scripts do a complete rebuild every time. If you'd prefer to just build the files that have changed since last time, change to the trunk/build/ directory, and run:

make && make install

Running Unity

If you'd like to run the version of unity you've built, rather than the system-wide version, open a terminal and run the following commands:

unity-env
unity --replace &

The first line patches several environment variables such that unity will subsequently be launched from your local staging directory. These environment variables will remain changed until you close the terminal, so you need only run unity-env once.

10 comments:

Anonymous said...

Instead of staging it, how would I make the changes system-wide?

Thomi Richards said...

You really really really don't want to do that. If you do, you're playing with fire. What happens when you accidentally install a version of unity that breaks, or is otherwise unusable?

If you're REALLY sure you want this, you should be able to achieve it by removing the "-DCMAKE_INSTALL_PREFIX=/home/thomi/staging/" from the cmake command listed in the article.

Anonymous said...

Thanks. I started off staging but I'm working on a custom build of Ubuntu so I needed the changes system-wide.

Thanks for the info, also this is a very helpful post.

Anonymous said...

Do you know how I would be able to run remake-unity as root?

sudo remake-unity doesn't work for some reason.

brickmack said...

When I try to run remake-unity, it doesnt work. Here is the part of the output that seems to have the important information:

-- [WARNING] One or more dependencies for compiz plugin unityshell not found. Skipping plugin.
-- Missing dependencies :
CMake Error at plugins/unityshell/CMakeLists.txt:13 (add_dependencies):
add_dependencies Adding dependency to non-existent target: unityshell


CMake Error at plugins/unityshell/CMakeLists.txt:14 (target_link_libraries):
Cannot specify link libraries for target "unityshell" which is not built by
this project.


-- Configuring incomplete, errors occurred!
make: *** No targets specified and no makefile found. Stop.


Any idea whats wrong, and how do I fix it?

Thomi Richards said...

Hi brickmack.

Yeah - CMake sucks at printing error messages. What's happened is that a dependency for unityshell is missing. You need to scroll up until you see an error that says exactly *which* dependency is missing.

brickmack said...

Is that the part where it says "checking for module ..."? It says it couldnt find packages libgeis and unity-protocol-private>=5.93.1, how do I get those?

Thomi Richards said...

That's the one - get libgeis by installing libgeis-dev. unity-protocol-private is obtained by making sure you have an up-to-date libunity (you may need to build it from source if it's not been packaged yet)

Anonymous said...

Fresh install/update of Ubuntu 12.04 Precise on Sept 8th, 2012. Followed the above directions and got the following.

I normally wouldn't complain except I want to try to fix a few bugs I've experienced and frankly don't have the time to fix this problem first (3 kids, wife, things to do around the house). So if someone out there has experienced it and could post instructions getting me to the point I can remake-unity, I would greatly appreciate it. If not, no biggie. I just wanted to dig into these bugs in my spare time

Thanks,
Jon

...etc...

-- checking for modules 'compiz>=0.9.8.0;nux-3.0>=3.0.0;libbamf3;dee-1.0;gio-2.0;gio-unix-2.0;gmodule-2.0;dbusmenu-glib-0.4;x11;libstartup-notification-1.0;gthread-2.0;indicator3-0.4>=0.4.90;atk;atk-bridge-2.0;unity-misc>=0.4.0;dbus-glib-1;gtk+-3.0>=3.1;sigc++-2.0;json-glib-1.0;libnotify;xfixes;unity-protocol-private>=5.95.1;libgeis;xrender>=0.9'
-- package 'compiz>=0.9.8.0' not found
-- package 'nux-3.0>=3.0.0' not found
-- package 'atk-bridge-2.0' not found
-- package 'unity-protocol-private>=5.95.1' not found
-- package 'libgeis' not found
CMake Error at /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake:266 (message):
A required package was not found
Call Stack (most recent call first):
/usr/share/cmake-2.8/Modules/FindPkgConfig.cmake:320 (_pkg_check_modules_internal)
CMakeLists.txt:134 (pkg_check_modules)


-- checking for module 'unity-protocol-private>=5.95.1'
-- package 'unity-protocol-private>=5.95.1' not found
CMake Error at /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake:266 (message):
A required package was not found
Call Stack (most recent call first):
/usr/share/cmake-2.8/Modules/FindPkgConfig.cmake:320 (_pkg_check_modules_internal)
CMakeLists.txt:135 (pkg_check_modules)


... etc ...

-- checking for module 'unity-protocol-private>=5.95.1'
-- package 'unity-protocol-private>=5.95.1' not found
-- checking for module 'libgeis'
-- package 'libgeis' not found
-- checking for module 'xrender>=0.9'
-- found xrender, version 0.9.6
-- [WARNING] One or more dependencies for compiz plugin unityshell not found. Skipping plugin.
-- Missing dependencies :
CMake Error at plugins/unityshell/CMakeLists.txt:13 (add_dependencies):
add_dependencies Adding dependency to non-existent target: unityshell


CMake Error at plugins/unityshell/CMakeLists.txt:14 (target_link_libraries):
Cannot specify link libraries for target "unityshell" which is not built by
this project.


-- Configuring incomplete, errors occurred!
make: *** No targets specified and no makefile found. Stop.

Thomi Richards said...

Hi,


It looks like you're missing a bunch of development library packages. Did you run:

sudo apt-get build-dep unity

before trying to compile Unity?


Also, the Unity trunk version is made to be built on quantal, so if you want to fix bugs, that's probably what you should be focussing on (perhaps run quantal inside a virtual machine?). If you just want to get the source code for the precise version of unity, you can run:

apt-get source unity


and you'll end up with a directory containing all the source code for the version of unity currently packaged in Precise.

Post a Comment