How do I remove CMake after installing it from source?

TL;DR: Run sudo make uninstall in the directory where you ran sudo make install before.

You followed this method of installing a different version of CMake. This is to say that you uninstalled the version provided by Ubuntu's package manager and manually downloaded, compiled, and installed it yourself from source code.

Assuming you were able to follow those instructions successfully, the version of CMake provided by Ubuntu's package manager (via the cmake package) is already uninstalled. Because the version that you have installed now is not provided by Ubuntu's package manager, but instead the version you compiled and installed yourself, running sudo apt-get purge cmake again does not remove it.

Since you installed cmake by compiling it and then running sudo make install, the solution is for you to:

  1. Use cd to go back to the directory where you ran that command.
  2. Run sudo make uninstall.

Not all software that can be installed by running make install has a corresponding uninstall target letting you run make uninstall to remove it. But CMake does support this.1

If you have deleted the folder in which you ran sudo make install, or otherwise changed it, then your best bet is to rebuild and reinstall it (be sure to use exactly the same version) to get back the ability to run sudo make uninstall. That is, you would follow the same installation steps, starting from the same .tar.gz file and passing all the same options you used (if any) to ./bootstrap. If you just ran ./bootstrap with no arguments after it, do that again. After running sudo make install again, which would (harmlessly) overwrite the files that were already installed with copies of themselves, you would then be able to use sudo make uninstall.

Another possible approach, which I strongly discourage you from doing, would be to try to figure out exactly what files and directories were created in the installation, and to try to remove just those files and directories but no others. If you did not pass the --prefix to ./bootstrap when you compiled CMake then it installed files in /usr/local, but it is not the only program that uses that directory. It would have installed files in the various subdirectories of /usr/local, such as bin and lib (and others), so aside from the situation where you know nothing else was installed into /usr/local, this option requires a lot of work.


1 It's easy to become confused about whether or not you can uninstall CMake by running sudo make uninstall because, by default, when you use CMake to create build scripts for your own software, no uninstall target is generated unless you explicitly ask for one. However, CMake itself is designed to be easily uninstalled, and its source code does define those uninstall targets for itself. In the Makefile generated from running ./bootstrap you can see:

#=============================================================================
# Target rules for targets named uninstall

# Build rule for target.
uninstall: cmake_check_build_system
    $(MAKE) -f CMakeFiles/Makefile2 uninstall
.PHONY : uninstall

# fast build rule for target.
uninstall/fast:
    $(MAKE) -f CMakeFiles/uninstall.dir/build.make CMakeFiles/uninstall.dir/build
.PHONY : uninstall/fast

You can also search for uninstall support in its CMakeLists.txt file.

To be sure, I tested this with CMake 3.9.0, and it does work. The uninstall target is generated and running sudo make uninstall works to uninstall CMake.