10

I tried installing Python 2.7 without root on a remote linux machine. I ran the commands

./configure prefix=/  
make install DESTDIR=/xxx/yyy/ 

where /xxx/yyy/ is a directory for which I have read-write access.

I ran into a problem at the end. It said:

building dbm using gdbm INFO: Can't locate Tcl/Tk libs and/or headers

Python build finished, but the necessary bits to build these modules were not found: _tkinter bsddb185 dl imageop sunaudiodev To find the necessary bits, look in setup.py in detect_modules() for the module's name.

running build_scripts running install_lib creating /lib/python2.7 error: could not create '/lib/python2.7': Permission denied

Did I take the correct steps in installing it without root access? (i.e., my configure and make commands?) Can anyone tell me why it would not install properly?

Thanks,
ktm

ktm5124
  • 11,861
  • 21
  • 74
  • 119

5 Answers5

15

I just install python2.7.5 without admin right. I think the command should be:

./configure prefix=/xxx/yyy
make install

and then you should add the path /xxx/yyy/bin in .bashrc as:

PYTHONPATH=/home/songmeixu/python/bin
export PATH=$PYTHONPATH:$PATH
SoLoMoN
  • 151
  • 1
  • 3
10

You should have prefix=/xxx/yyy. With prefix=/, it tries to install the libraries to /lib/python2.7, rather than /xxx/yyy/lib/python2.7.

Steve Howard
  • 6,737
  • 1
  • 26
  • 37
  • I did this, but then it installed the bin/share/lib files in /xxx/yyy/xxx/yyy. I ended up renaming the paths, to reflect the directory structure that I wanted (certainly not that redundant path!) and I am hoping that it will cause no path conflicts. – ktm5124 Apr 28 '11 at 20:30
  • 3
    @ktm5124: Don't supply a `DESTDIR` if you've already configured `prefix`. In fact, [users shouldn't need to use `DESTDIR` at all](https://gnu.org/prep/standards/html_node/DESTDIR.html). – Rufflewind Jan 03 '15 at 13:27
4

Don't compile, get the pre-built binary from ActiveState.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
  • Why doesn't everyone do this? Is there a catch? – Gabriel Fair Aug 11 '12 at 22:25
  • 2
    @GabrielFair - the community license has certain restrictions... see http://www.activestate.com/activepython/license-agreement – Sridhar Ratnakumar Aug 13 '12 at 17:25
  • 1
    This is simplest! And with `pip`, I installed all my packages I needed. – Yin Zhu Jul 21 '13 at 07:21
  • It seems they do not supply the latest version. At time of writing, AS supplied 3.4.1 and newest version is 3.4.3 – hilcharge Aug 13 '15 at 07:10
  • If you need a standard build, and precompiled binaries are available for your platform, this is a nice solution; but the answer should probably outline when this might not be what you need (i.e. you can't find binaries for your specific platform; or you need a custom build for some reason, such as requising a newer one that the available precompiled binaries). – tripleee Mar 24 '19 at 12:19
2

I wrote a script that installs Python 2 (which is missing the convenience faculties provided by Python 3 for user installations) and Pip 2 into a user directory so that a standard user can be administrator over its modules etc.

#!/bin/bash
VERSION="2.7.11"
BUILDDIR=~/"build/python"
INSTALLDIR=~/"python/Python-$VERSION"

mkdir -p ${BUILDDIR}
cd ${BUILDDIR}
if [ ! -f $BUILDDIR/Python-$VERSION.tgz ]
then
wget https://www.python.org/ftp/python/$VERSION/Python-$VERSION.tgz
tar zxfv Python-$VERSION.tgz
fi
find $BUILDDIR -type d | xargs chmod 0755
cd Python-$VERSION


mkdir -p ${INSTALLDIR}
./configure --prefix=${INSTALLDIR}
make && make install

# Append to user PATH or create symbolic link to .local/bin
# [[ ":$PATH:" != *":$HOME/python/Python-$VERSION/bin:"* ]] && printf "export PATH=$HOME/python/Python-$VERSION/bin:$PATH\n" >> ~/.bashrc
if [ ! -d ~/.local/bin ]; then mkdir -p ~/.local/bin; fi
ln -s ~/python/Python-"$VERSION"/bin/python ~/.local/bin/

source ~/.bashrc

# Install local pip
cd ..
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py -O - | python - --user
[[ ":$PATH:" != *":$HOME/.local/bin:"* ]] && printf "export PATH=$HOME/.local/bin:$PATH\n" >> ~/.bashrc

# Install modules like this:
# pip install --target="$HOME/.local/lib/python$VERSION/site-packages"

# Add those modules to PYTHONPATH
[[ ":$PYTHONPATH:" != *":$HOME/.local/lib/python$VERSION/site-packages:"* ]] && printf "export PYTHONPATH=$HOME/.local/lib/python$VERSION/site-packages:$PYTHONPATH\n" >> ~/.bashrc

source ~/.bashrc

Caveat: This script is admittedly opinionated in that it will append a few lines to your ~/.bashrc for the PATH ENV variable. If this is not desired, simply comment the related lines in the script.


Case: The service file generator for Airprint service files for use in Avahi does not support Python 3. For the purpose of keeping the system clean, I just install a local version of Python 2 and run airprint-generate.py followed by deleting the whole install (saves space on a small Raspberry Pi Zero W).

Jonathan Komar
  • 2,678
  • 4
  • 32
  • 43
  • The quoting is wrong, but happens to work as long as your home directory path doesn't contain any shell metacharacters. The correct quoting would put `~` outside any double quotes, but then always use double quotes around the variable interpolations. See also [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Mar 24 '19 at 12:22
  • Also `\`pwd\`` is an expensive and roundabout way of saying `.` if you don't specifically need an absoulte path. – tripleee Mar 24 '19 at 12:24
  • You might want to put the `printf` statements to update `.bashrc` in a conditional, so as to avoid adding these lines again if you already ran this script before. – tripleee Mar 24 '19 at 12:26
  • @tripleee probably a good idea to open a chat for all of these comments: https://chat.stackoverflow.com/users/874188/tripleee – Jonathan Komar Mar 24 '19 at 12:27
  • Sorry, not in a place where I can conveniently access chat. If you can wait until Monday, I'll be happy to converse with you about these suggestions. – tripleee Mar 24 '19 at 12:29
  • The final `source` doesn't do anything useful if this is genuinely a standalone script. If this is supposed to be sourced by the user, maybe point this out separately (and then the shebang is superfluous and misleading). – tripleee Mar 24 '19 at 12:30
0

Instead of building from source manually, I'd suggest letting linuxbrew do the build for you. DigitalOcean has a nice tutorial on installing linuxbrew. Once that's complete, you can just say brew install python and have a nicely managed python installation, including pip.

Jeff Hammerbacher
  • 4,226
  • 2
  • 29
  • 36