How Would I Build Python Myself from Source Code on Ubuntu

How would I build python myself from source code on Ubuntu?

  1. At a shell prompt (in a terminal), run

    sudo apt-get install build-essential 

    This will fetch all the common packages you need to build anything (e.g. the compiler etc.).

  2. Then run

    sudo apt-get build-dep python2.7

    This will fetch all the libraries you need to build python.

  3. Then download the source code for python and decompress it into a directory.

  4. go there and run

    ./configure --prefix=/path/where/you/want/python/installed
  5. Then make and then make install to get it built and installed:

    make && make install

If you hit snags on the way, ask back here and I'll try to offer some guidance.

How to build Python 3 from source on Ubuntu

First, make sure your system is fully updated:

sudo apt update
sudo apt upgrade

Next, install the default GCC toolchain with:

sudo apt install build-essential

Next, we need to install a few prerequisites for building Python:

sudo apt install libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev

sudo apt install libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev libffi-dev

At the time of this writing, the latest stable version of Python is 3.7.1, if you want to use a newer version change the next instructions accordingly:

wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
tar xf Python-3.7.1.tar.xz
cd Python-3.7.1
./configure --enable-optimizations
make -j 8
sudo make altinstall

What flags to use for ./configure when building Python from source

Welcome to the world of Python build configuration! I'll go through the command line options to ./configure one by one.

--with-pydebug is for core Python developers, not developers (like you and me) just using Python. It creates debugging symbols and slows down execution. You don't need it.

--enable-optimizations is good for performance in the long run, at the expense of lengthening the compiling process, possibly by 3-fold (or more), depending on your system. However, it results in faster execution, so I would use it in your situation.

--with-ensurepip=install is good. You want the most up-to-date version of pip.

--enable-shared is maybe not a good idea in your case, so I'd recommend not using it here. Read Difference between static and shared libraries? to understand the difference. Basically, since you'll possibly be installing to a non-system path (/opt/local, see below) that almost certainly isn't on your system's search path for shared libraries, you'll very likely run into problems down the road. A static build has all the pieces in one place, so you can install and run it from wherever. This is at the expense of size - the python binary will be rather large - but is great for non-sys admins. Even if you end up installing to /usr/local, I would argue that static is better/easier than shared.

--enable-unicode=ucs4 is optional, and may not be compatible with your system. You don't need it. ./configure is smart enough to figure out what Unicode settings are best. This option is left over from build instructions that are quite a few versions out of date.

--prefix I would suggest you use --prefix=/opt/local if that directory already exists and is in your $PATH, or if you know how to edit your $PATH in ~/.bashrc. Otherwise, use /usr/local or $HOME. /usr/local is the designated system-wide location for local software installs (i.e., stuff that doesn't come with Ubuntu), and is likely already on your $PATH. $HOME is always an option that doesn't require the use of sudo, which is great from a security perspective. You'll need to add /home/your_username/bin to your $PATH if it isn't already present.

building Python from source with zlib support

The solution is to install the Ubuntu package dpkg-dev.

sudo apt-get install dpkg-dev

The reason is explained here.

In short, recent versions of Ubuntu don't store libz.so in the standard /usr/lib location, but rather in a platform specific location. For example, on my system is is in /usr/lib/x86_64-linux-gnu. This prevents Python's build system from finding it.

The dpkg-dev package installs the dpkg-architecture executable, which enables Python to find the necessary libraries.

The original question was about Python 3.2.3. I also downloaded Python 2.7.3 and confirmed that the same problem exists, and this solution is applicable to it as well.

how to build a software with source code in linux?

The project appears to be pure python. So there is no compilation required to install it. As a python package, the installation procedure consists in making the sources available to your python interpreter. This can be done either by appending yourself the path or by using an installation script (called setup.py) provided by the package maintainer.

There is no setup.pyso you cannot install it the usual way. If you want to be able to access the class from everywhere, you have to either append your path or create a setup file.

export PYTHONPATH=path_to_code:$PYTHONPATH 

From there, follow the example in the test directory:

from fortran_tools import Fixed2Free
import os

path = '.'
filename = 'myfile_without_extension'

input_path = os.path.join(path, 'input', filename + '.f')
output_path = os.path.join(path, 'output', filename + '.f90')

Fixed2Free.from_argv(['', input_path, output_path, '--style'])

with input_path the path to the file you want to convert and output_path the path of the converted file.

Building Python and more on missing modules

Here is how to build Python and fix any dependencies. I am assuming that you want this Python to be entirely separate from the Ubuntu release Python, so I am specifying the --prefix option to install it all in /home/python27 using the standard Python layout, i.e. site-packages instead of dist-packages.

1. Get the .tar.gz file into your own home directory.
2. tar zxvf Py*.tar.gz
3. cd Py*1
4. ./configure --prefix=/home/python27
5. make
6. make install

Step 5 is the important one. At the end, it will display a list of any modules that could not be built properly. Often you can fix this by installing an Ubuntu package, and rerunning make.

a. sudo apt-get install something-dev
b. make

It is pretty common to have a problem because you are missing the -dev addon to some module or other. But sometimes you should start over like this:

a. make clean
b. ./configure --prefix=/home/python27
c. make

Starting over never hurts if you are unsure. An important note about step 6. I am not using sudo on this command which means that you will need to have the /home/python27 directory already created with the appropriate ownership.

Don't hesitate to try out ./configure --help |less before building something because there may be interesting options that you could use. One time on a minimal distro I had to do --with-dbmliborder=gdbm:bdb in order to get gdbm working. When you run ./configure, the last few lines will tell you where it put the information that it learned. In the case of Python, Modules/Setup has been useful to figure out how to get a module to build.

Another useful thing is to make clean and then run make >make.out 2>&1 to capture all the output from the full make process. Then, after it is complete, use less or an editor to look for the details on a problem module such as _sqlite. For instance, check all the -I options that are passed to gcc. If the correct include directory is not on the list that would cause a problem. You can edit setup.py to change the list of include directories.

In the past it was more common to have library problems that would be fixed by logging out, logging in again, and running "sudo ldconfig" before doing a complete rebuild.

How do I configure mod_wsgi with python that has been built from source code?

Instead of

./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/local/share/man/man1/python2.7.1

I would assume you would want to use:

./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/local/bin/python2.7.1

I think you want to tell mod_wsgi to use the path to the python executable rather than the man page



Related Topics



Leave a reply



Submit