Installing packages into local directory?

This is, in general, not doable, because you would mess with the apt dependencies system.

There are two solutions:

  1. Install the source package, change into the source directory, configure and install the package irrespective of the packaging systems manually to a directory of your choice.

    apt-get source <package>
    

    This does not need root, downloads the package source, unpacks it in a directory within the current directory. You can then change to that directory, make modifications to the source, configure the installation to another target etc.

    Configuring to which installation directory the programs should go depends, however, on the particular program. Many programs use the ./configure --prefix localdir to target the installation to localdir; but this is by far not always the case.

  2. Create a chroot environment into which you will install the packages:

    debootstrap precise myfancyinstall
    

    Now you have created a dummy installation in the myfancyinstall/ directory

    chroot myfancyinstall
    

    You can use apt-get install within the chroot cage to install whatever you wish.


using a bash shell, and acquiring the "package.deb" file (assuming pack name is "package") you can run the following command to accomplish what you want - installing the package so that your home directory is treated the same way "/" would be treated in a normal install.

This is the command:

apt-get download package; dpkg -i --force-not-root --root=$HOME package.deb

You might face some errors, such as $HOME/var/lib/dpkg/lock is missing so just create all the missing files you will get from the errors and then the install should work without sudo.

notice that if "apt-get download" doesn't work, you can try "apt download" or "apitutde download package".

if neither methods work, you can just download the package manually from http://packages.ubuntu.com/

another method would be to run the chroot command with the parameter $HOME and then installing the same way as above only without --root=$HOME. that command would bring you in a shell where "/" is your current $HOME. to return to normal mode don't forget to "exit"

good luck.


If you're on a shared web-hoster with ssh access but no apt-get no root etc. or a similarly restricted system the following may work for you. It worked for me on a system where uname -a returned something like SMP Debian 4.9.65-3+deb9u2~bpo8+1 (2017-01-05) x86_64 GNU/Linux

# examples tried on a shared hoster with ssh access but no apt-get no root etc.
# http://mirrors.kernel.org/ubuntu/pool/main/g/gawk/gawk_4.1.3+dfsg-0.1_amd64.deb
# https://github.com/dvorka/hstr/releases/download/1.25/hstr_1.25-1_amd64.deb

debURL="http://mirrors.kernel.org/ubuntu/pool/main/g/gawk/gawk_4.1.3+dfsg-0.1_amd64.deb"
# get the filename only, remove all till last slash "/"
# see http://wiki.bash-hackers.org/syntax/pe#substring_removal
debFile=${debURL##*/}
# change to your desired directory for installation/unpacking; here: $HOME
cd $HOME
# get the .deb file (no dependencies checked or resolved here)
curl -OL $debURL 
# unpack only the data part from the .deb file
# see https://en.wikipedia.org/wiki/Deb_%28file_format%29
ar p $debFile data.tar.xz | tar xJv --strip-components=2 -f - 
rm -v $debFile # clean up
echo "Done unpacking $debFile into $(pwd)"