How to compile and install programs from source

90,834

Solution 1

Normally, the project will have a website with instructions for how to build and install it. Google for that first.

For the most part you will do either:

  1. Download a tarball (tar.gz or tar.bz2 file), which is a release of a specific version of the source code
  2. Extract the tarball with a command like tar zxvf myapp.tar.gz for a gzipped tarball or tar jxvf myapp.tar.bz2 for a bzipped tarball
  3. cd into the directory created above
  4. run ./configure && make && sudo make install

Or:

  1. Use git or svn or whatever to pull the latest source code from their official source repository
  2. cd into the directory created above
  3. run ./autogen.sh && make && sudo make install

Both configure and autogen.sh will accept a --prefix argument to specify where the software is installed. I recommend checking out Where should I put software I compile myself? for advice on the best place to install custom-built software.

Solution 2

I just want to add that there are package managers that compile packages from source, and handle all package dependencies, flags, etc..

In BSD systems it's ports: Using the Ports Collection

In Debian, the apt-get package manager can install from source too: APT HOWTO: Working with source packages (Same goes for Ubuntu, Linux-mint and everything else based on Debian)

The Gentoo distribution uses the portage package manager, which compiles the whole system from source only: Portage Introduction.

Slackware can compile packages but I don't know if there's any package manager for this in there.. =)

Anyway you can always compile packages manually like Sandy mentioned above =) Also it must be possible to use apt-get or portage package managers in any other distro...

Solution 3

I think it's just best to read the documentation coming with that specific program or application that you're wanting to install. Usually there are readmes/READMEs inside the tarballs (the application source archive which you can usually download) or maybe even INSTALL files to read and learn about what is the preferred way of installing said application. In short: RTFM ;)

Solution 4

A summary for using the Ports Collection in FreeBSD:

Find Port

Ports are organized by category so if you don't know what category the port is in you have to find it first:

cd /usr/ports
make search name=myport

Sometimes there are too many entries that way. I personally prefer:

find /usr/ports -name myport* -print -depth 2

Use the * when searching since there are often multiple versions of a port available. The depth argument ensures your return results aren't needlessly cluttered with matches you are unlikely to want.

Configuration

Often, you'll want to do some configuration; software such as Apache and Postgres practically require it. There are three main choices: command line, environment and make configuration files. To get started with the command line:

make showconfig

this will list the default configuration options. If you like the defaults you are ready to compile and install. If not,

make config

will bring up a dialog box where you can select which options you want. (Don't become confused with this and make configure which configures your port with your chosen options!) This is often sufficient but for some software, like Apache, there is often complex configuration that a simple dialog won't handle. For this, you also should look at the Makefile(s) which will sometimes give you some additional targets for make that will give you more information. To continue the Apache example

make show-modules
make show-options
make show-categories

will give you information on setting up you chosen modules, thread options and the like. If your port's defaults are mostly fine and you just want to change a few things, you can also just pass key=value pairs like environment variables:

make MYVBL1=MYVAL1 ... install clean

Also, you can set switch options via the -D option:

make -D MYVAR -D MYOTHERVAR ... install clean

For complex configuration however the command line won't work well and you're better neither of the first two methods will be effective. In this case you can make a configuration file and pass that to make with the __MAKE_CONF variable. FreeBSD has a default configuration file: /etc/make.conf which usually contains information on previously installed ports and other system settings. To begin, create a file with your ports options, call it ~/myport.mk and then combine that file with /etc/make.conf:

cat /etc/make.conf ~/myport.mk >> ~/make.myport.conf

you can then double check your configuration:

make showconfig __MAKE_CONF=~/make.port.conf

and if everything looks good:

make install clean __MAKE_CONF=~/make.myport.conf

BEWARE! If you need to adjust your configuration settings after make configure or an installation in whole or part you absolutely must clear your configuration first:

make rmconfig

Failure to do so will result in unexpected interactions between the ports subsystem, your port's make defaults and your desired configuration.

That's kind of a lot for a summary, but the complexity of configuration is mostly about the app, not the port. Bash for example, doesn't really have any options.

Installation

This is the easy part:

make install clean

or you can

make build
make install
make clean

which is just more typing.

That's pretty much it. Obviously there is more you can do such as recursively listing dependencies and configuration options, update with patches and so on. Here I will refer you to the Ports section of the Handbook, the port subsystem's man page (good info on additional make targets) and the make man page.

Share:
90,834

Related videos on Youtube

Nitrodist
Author by

Nitrodist

Updated on September 17, 2022

Comments

  • Nitrodist
    Nitrodist over 1 year

    This is an issue that really limits my enjoyment of Linux. If the application isn't on a repository or if it doesn't have an installer script, then I really struggle where and how to install an application from source.

    Comparatively to Windows, it's easy. You're (pretty much) required to use an installer application that does all of the work in a Wizard. With Linux... not so much.

    So, do you have any tips or instructions on this or are there any websites that explicitly explain how, why and where to install Linux programs from source?

    • Admin
      Admin over 13 years
      "Comparatively to Windows, it's easy. You're (pretty much) required to use an installer application that does all of the work in a Wizard. With Linux... not so much." There's the weak point in the question. With Windows, you rarely get the source code, so you're at the mercy of whomever made the package. If you think about this, it's not that much different from saying "there was no Linux package, so I have to build from source", ie. there wasn't a way to get it to begin with. Building from source is usually a last resort in *nix-land, but rarely an option in Windows-ville.
    • Admin
      Admin over 13 years
      That being said... +1 bump for asking a common question that should be answered for all newcomers to *nix systems. :) Building from source sometimes means the difference between fixing a nasty bug and just suffering until the next software release. It's really not that bad, and as many here have pointed out, once you know what to look for and how to do it, fairly painless.
  • Matt Simmons
    Matt Simmons over 13 years
    Typically people who want the "bleeding edge" software will pull the release from the version control software (like git, subversion, or CVS). This is "bleeding edge" because it's probably untested, unlike a released tarball, which has (probably) been vouched for as working (at least mostly). Make sense?
  • Godeke
    Godeke over 13 years
    @Matt, sure it makes sense but I think the comment was directed more at the fact that Sandy now made it appear as if there's an autogen.sh in every git'd/svn'd trunk.
  • Matt Simmons
    Matt Simmons over 13 years
    And a chicken in every pot...or something like that. Heck, every once in a while, I come across software without a configure script, too. If there were an easy, unified way of doing it, we wouldn't need packages ;-)
  • Sandy
    Sandy over 13 years
    Matt is absolutely right, this is why I said "for the most part", and first advocated looking up the project's website. The autogen.sh/configure advice will hold for pretty much every GNOME module, and a ton of other projects, too. Some projects don't use automake, and will only have Makefile, and you will just run make && sudo make install. Some Python projects will only have a setup.py, which you will call to install (since there is no real compile setup). There are plenty of other build/install systems out there, too. Hopefully README or INSTALL files will explain exactly what to do.
  • Sandy
    Sandy over 13 years
    I had added a comment here about what autogen.sh does, but I think it's much better answered by Miguel in Louis's question here: unix.stackexchange.com/questions/158/…
  • Alen Milakovic
    Alen Milakovic over 12 years
    To say "the apt-get package manager can install from source" is incorrect. The quoted section describes how one can create a Debian package from Debian sources. Not arbitrary upstream sources.
  • vonbrand
    vonbrand about 11 years
    @LouisSalin, what is being described is GNU conventions (used by many outsiders too). By those conventions, several files that are generated by autogen.sh are shipped in tarballs (to reduce dependencies for building to a minimum of commonly available tools), if you get the version from a version control system (git, svn, whatever) those files aren't under version control so you have to regenerate them yourself.
  • jakob-wenzel
    jakob-wenzel about 6 years
    I came here because the the README gave all the requirements to build under Linux, but only has Windows install instructions. The accepted answer worked for me and probably works most of the time. I think it's almost better advice to try the ./configure route and go to the README if it doesn't work (i.e. missing a dependency and you can't read the compiler output).