6

As Python 3.7 was being prepared I installed Python 3.7.0b3 from source. Now 3.7 is out and I want to use the version packaged for Ubuntu. So I've done

apt-get install python3.7

but

$ which python3.7
/usr/local/bin/python3.7

$ /usr/local/bin/python3.7 --version 
Python 3.7.0b3

How does one uninstall a Python installed from source (on Ubuntu)? Or how can I replace it with the apt repo packaged version?

Tom Viner
  • 6,655
  • 7
  • 39
  • 40
  • 2
    Unless things have changed since the last time I looked (which was, admittedly, quite a long time ago), Python's makefile doesn't come with an uninstaller, and doesn't even track what it installs. So, the only way to do this is to uninstall manually: Figure out all the files it installed and remove them. (You probably _also_ want to manually uninstall anything that you added to `site-packages` after install, of course.) – abarnert Jul 14 '18 at 21:36
  • 1
    If you left the default locations alone, it's all going to be under `/usr/local`: the whole subtree `/usr/local/lib/python3.7`, plus multiple individual files in `/usr/local/bin` and `/usr/local/man`. This is covered in the `README.rst` in the source tree, but not covered all that well. – abarnert Jul 14 '18 at 21:39
  • You can directly run /usr/bin/python3.7 or update your PATH shell variable to have /usr/bin before /usr/local/bin. Leave the /usr/local version alone if you can live with it. – swiftg Jul 21 '18 at 00:54

3 Answers3

12

Since you opened a bounty, I can't vote to close as a duplicate, but this question would seem to provide a possible solution. Quoting from the accepted answer:

You can use checkinstall to remove Python. The idea is:

  1. Install checkinstall
  2. Use checkinstall to make a deb of your Python installation
  3. Use dpkg -r to remove the deb.

checkinstall basically wraps a make install command and creates a Debian .deb package based on what was installed. Then, you can just uninstall that package to reverse make install completely. To be perfectly safe you may want to uninstall the packaged Python 3.7 first and reinstall it afterwards to avoid any conflicts (I wouldn't expect any, though, since the packaged version lives in /usr while your source version lives in /usr/local).

If you don't have your source files around anymore, you can always download them again (https://www.python.org/downloads/release/python-370b3/) and rebuild Python. Specifically, the checkinstall commands would look something like this:

sudo apt install checkinstall
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0b3.tgz
tar xf Python-3.7.0b3.tgz
cd Python-3.7.0b3
./configure && make
sudo checkinstall -D --fstrans=no make install
sudo dpkg -r Python-3.7.0b3.deb

(-D creates a Debian package, --fstrans=no disables use of a temporary directory for installation).

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Did you forget the 2 steps before the install: `./configure` and `make`? See https://github.com/python/cpython/blob/master/README.rst#build-instructions – Tom Viner Jul 24 '18 at 23:35
  • 1
    Yes, I left them out. My bad - I've added them back in. – nneonneo Jul 25 '18 at 01:49
  • doesn't work for me. I give the name `python373` to my package, then `dpkg -r` but I get : `dpkg: warning: ignoring request to remove python373 which isn't installed` – snoob dogg Aug 16 '19 at 16:47
3

Since I was also moving to python 3.7, I came across this question and decided to answer it, as well as finish my installation. This are the two sources, which I used for installing python 3.7 on ubuntu 16.04: https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get https://superuser.com/questions/241865/updating-python-on-ubuntu-system

Apparently from the first source the deadsnakes PPA contain Python 3.7 - Link: https://github.com/deadsnakes/python3.7/tree/ubuntu/xenial/Python

So following from my first source, I used the following commands to install Python 3.7:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7

It installed Python 3.7 successfully but my Python 3.5.2 remains default. You can invoke/use Python 3.7 by running the commands:

python3.7 script.py

It works, so I decided to set Python 3.7 as default by removing python 3.5, but I came across my second source, and just decided to stick with using python3.7, when running my script in the terminal.

Apparently it says: Ubuntu policies extensively for writing end-user software. So basically, a large part of the system is written in Python. To switch to Python 3.7, there needs to be done a piece of work consisting of updating and re-testing all the scripts.

So to say you can't just switch to Python 3.7 and delete the older version.

Also from a comment from my first source it states deleting the older version might break the system. I haven't been around to deleting my older version in case it might break the system, but since you are asking for how to download Python 3.7, I think my first source and the first part of my answer should to the work.

I hope it helps :)

Mahir Islam
  • 1,941
  • 2
  • 12
  • 33
2

It seems that your Python built from source is under /usr/local, and your PATH variable has /usr/local/bin before /usr/bin, since running python3.7 gets you the one under /usr/local rather than the packaged one which would be /usr/bin/python3.7.

Look at your PATH to verify this.

echo $PATH

(When you run a program in bash, that particular running bash instance will remember the location and not rescan the directories in the PATH for that program again, so it will not notice a new file that has appeared somewhere earlier in the PATH. You can prevent this by running hash -r to reset the cache or by just exiting the shell and launching it again.)

I presume your goal is for python3.7 (or any of the other commands provided by Python) to run the versions from your packaged install in /usr.

Unfortunately the python build process does not provide an uninstall method -- the only automated way to remove just the files installed by a source Python install requires using other tools ahead of time (such as checkinstall).

So you have some choices :

  • Change your PATH so that /usr/local/bin is after /usr/bin. To do this, edit your ~/.profile file or whatever other script you have configuring your PATH and logout/login. This will also affect any other commands you run that are available in both /usr/local/bin and /usr/bin.

  • Remove /usr/local and reinstall anything else you want there. If a Python install is the only thing in your /usr/local, or if you can easily reinstall anything else you had there, this might be the way to go.

  • Painstakingly figure out what files under /usr/local/bin were part of Python and remove them. You might be able use the corresponding files in /usr/bin from your installed python3 packages as a starting point to figure out the similar names for /usr/local/bin.

One-liner to get the list of files in /usr/bin from installed python3* packages:

$ for pkg in $(dpkg -l 'python3*' | grep '^ii' | cut -f 3 -d' '); do dpkg -L $pkg | grep '^/usr/bin/'; done | sort

This should produce a list of files like:

/usr/bin/2to3-3.x
/usr/bin/chardet3

...

(I've tested this one-liner on Debian, I'm not sure if any changes are required for Ubuntu)

rakslice
  • 8,742
  • 4
  • 53
  • 57
  • If Python provided an `uninstall` target, like the majority of `configure && make && make install` source distributions, we wouldn't be in this mess; however they don't seem to care https://bugs.python.org/issue549764 – rakslice Oct 01 '18 at 02:13