How can we install all the necessary dependencies for a package so that we can build it?

Well, after I installed freecad on my Ubuntu 12.04 with the help of this blog I started asking about compiling and building from source.

So, when we are trying to install a package some times the terminal or the software center says that some packages are missing. The sudo apt-get build-dep package_name is a general command that finds and installs dependencies right?

My main questions here are:

  1. From where is it finding the missing packages, repository?
  2. Can we use this command for any application? Or only those that the packages are in our repositories?

  1. Packages explicitly list what they depend on in a special file in the package itself (more information here). That's what the package manager uses to figure out the full set of dependencies. And yes, it downloads them from the repository.
  2. You can only use it for valid package names that are known to your package manager (since it needs to access the file that lists the dependencies), i.e. found in the repositories that you have enabled on your system.